Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8629543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:50:16+00:00 2026-06-12T08:50:16+00:00

I have a GridView that I am trying to convert to a DataTable. I

  • 0

I have a GridView that I am trying to convert to a DataTable. I cannot use the GridView’s DataSource for this DataTable. I’ve found the method below to be the most common way most use to tackle this task, but for some reason, the below method keeps throwing an IndexOutOfRangeException of “Cannot find column 0”. Here’s the code:

    DataSet ds = new DataSet("Student");
    DataTable dt = new DataTable(String.Format("{0}For{1}", gv.ID.Substring(2, gv.ID.Length - 2), year));
    for (int j = 0; j < gv.Rows.Count; j++)
    {
        DataRow dr;
        GridViewRow row = gv.Rows[j];
        dr = dt.NewRow();
        for (int i = 0; i < row.Cells.Count; i++)
        {
            dr[i] = row.Cells[i].Text; //***EXCEPTION IS THROWN HERE***
        }
        dt.Rows.Add(dr);
    }
    ds.Tables.Add(dt);

There are 18 cells per row.

Thanks in advance for any help.

Solution
I took CptSupermrkt’s advice and implemented this code just before the first “for” loop, and the method continued w/o throwing the exception:

    DataColumn dc;
    foreach (var item in gv.Columns)
    {
        dc = new DataColumn(item.ToString());
        dt.Columns.Add(dc);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:50:17+00:00Added an answer on June 12, 2026 at 8:50 am

    The DataTable you’ve defined does not have any columns defined as well, so the row that you get from dt.NewRow() has no columns. Therefore trying to access the index of columns won’t work.

    I would think you need the DataTable you’re creating to have the same columns as your GridView.

    DataTable dt = new DataTable(String.Format("{0}For{1}", gv.ID.Substring(2, gv.ID.Length - 2), year));
    
    DataColumn idCol = new DataColumn("ID");
    DataColumn nameCol = new DataColumn("Name");
    //etc.
    
    dt.Columns.Add(idCol);
    dt.Columns.Add(nameCol);
    //etc.
    

    Then, when you create use NewRow() on the DataTable, the row will have the columns and you can access them as you try to do in the rest of your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an gridview that retrieves the data via a datatable like this: protected
I have a gridview that I'm trying to validate from code behind. In this
I have a ASP.net gridview that I am trying bind to. My DataSource has
I have a gridview that is bound to a datasource on a Windows Form
I have a gridview that is bound to a datasource (Windows Forms, VB.NET). One
I have a GridView on an ASP.NET page that I'm trying to bind to
I have a gridview that I am trying to loop through to get the
I have a Gridview that displays some data. What im trying to do is
I am trying to use the Search Box that is explained in this blog
I have a gridview that needs to be exported to Excel. I have managed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.