42

How can I export GridView.DataSource to datatable or dataset?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Penguen
  • 16,836
  • 42
  • 130
  • 205

7 Answers7

50

Assuming your DataSource is of type DataTable, you can just do this:

myGridView.DataSource as DataTable
Kon
  • 27,113
  • 11
  • 60
  • 86
  • this is the best answer for this question, thank you – MBH May 21 '15 at 12:34
  • 1
    I set the `datagridview.DataSource = dataTable` but when I want to read that from `DataSource` by this ways , throw invalid cast exception by msg: `Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'.` – Behzad Jan 31 '16 at 10:59
27

You should convert first DataSource in BindingSource, look example

BindingSource bs = (BindingSource)dgrid.DataSource; // Se convierte el DataSource 
DataTable tCxC = (DataTable) bs.DataSource;

With the data of tCxC you can do anything.

19

Personally I would go with:

DataTable tbl = Gridview1.DataSource as DataTable;

This would allow you to test for null as this results in either DataTable object or null. Casting it as a DataTable using (DataTable)Gridview1.DataSource would cause a crashing error in case the DataSource is actually a DataSet or even some kind of collection.

Supporting Documentation: MSDN Documentation on "as"

Community
  • 1
  • 1
Tacoman667
  • 1,391
  • 9
  • 16
  • 3
    Although this didn't generate any errors, when I hover to check the value of my datatable it shows it as null even though my gridview shows populated data. DataTable dt = gvJobSearchEngine.DataSource as DataTable; – John Waclawski Mar 16 '17 at 19:12
8

Ambu,

I was having the same issue as you, and this is the code I used to figure it out. Although, I don't use the footer row section for my purposes, I did include it in this code.

    DataTable dt = new DataTable();

    // add the columns to the datatable            
    if (GridView1.HeaderRow != null)
    {

        for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
        {
            dt.Columns.Add(GridView1.HeaderRow.Cells[i].Text);
        }
    }

    //  add each of the data rows to the table
    foreach (GridViewRow row in GridView1.Rows)
    {
        DataRow dr;
        dr = dt.NewRow();

        for (int i = 0; i < row.Cells.Count; i++)
        {
            dr[i] = row.Cells[i].Text.Replace("&nbsp;","");
        }
        dt.Rows.Add(dr);
    }

    //  add the footer row to the table
    if (GridView1.FooterRow != null)
    {
        DataRow dr;
        dr = dt.NewRow();

        for (int i = 0; i < GridView1.FooterRow.Cells.Count; i++)
        {
            dr[i] = GridView1.FooterRow.Cells[i].Text.Replace("&nbsp;","");
        }
        dt.Rows.Add(dr);
    }
Joseph
  • 81
  • 1
  • 1
2

If you do gridview.bind() at:

if(!IsPostBack)

{

//your gridview bind code here...

}

Then you can use DataTable dt = Gridview1.DataSource as DataTable; in function to retrieve datatable.

But I bind the datatable to gridview when i click button, and recording to Microsoft document:

HTTP is a stateless protocol. This means that a Web server treats each HTTP request for a page as an independent request. The server retains no knowledge of variable values that were used during previous requests.

If you have same condition, then i will recommend you to use Session to persist the value.

Session["oldData"]=Gridview1.DataSource;

After that you can recall the value when the page postback again.

DataTable dt=(DataTable)Session["oldData"];

References: https://msdn.microsoft.com/en-us/library/ms178581(v=vs.110).aspx#Anchor_0

https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/

劉鎮瑲
  • 517
  • 9
  • 20
1

I have used below line of code and it works, Try this

DataTable dt =  dataSource.Tables[0];
Kalu Singh Rao
  • 1,671
  • 1
  • 16
  • 21
RSB
  • 359
  • 5
  • 10
0

This comes in late but was quite helpful. I am Just posting for future reference

DataTable dt = new DataTable();
Data.DataView dv = default(Data.DataView);
dv = (Data.DataView)ds.Select(DataSourceSelectArguments.Empty);
dt = dv.ToTable();
Muzoora Savior
  • 529
  • 5
  • 16