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

- 97,193
- 102
- 206
- 364

- 16,836
- 42
- 130
- 205
-
3what is the type of the object pointed to by GridView.DataSource? – Mitch Wheat Apr 24 '09 at 13:18
-
http://www.vbforums.com/showthread.php?t=474895 – TheTXI Apr 24 '09 at 13:21
7 Answers
Assuming your DataSource is of type DataTable, you can just do this:
myGridView.DataSource as DataTable

- 27,113
- 11
- 60
- 86
-
-
1I 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
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.

- 296
- 3
- 2
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"

- 1
- 1

- 1,391
- 9
- 16
-
3Although 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
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(" ","");
}
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(" ","");
}
dt.Rows.Add(dr);
}

- 81
- 1
- 1
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
-
@John Waclawski I have the same problem like yours. I found this solution, hope this can help you. – 劉鎮瑲 Jul 20 '18 at 00:46
I have used below line of code and it works, Try this
DataTable dt = dataSource.Tables[0];

- 1,671
- 1
- 16
- 21

- 359
- 5
- 10
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();

- 529
- 5
- 16
-
Data.DataView did not have any intellisense popup when I tried your code. – John Waclawski Mar 16 '17 at 19:20