How to get the Gridview data into DataTable [modified]
-
Hi all, How to get the Gridview data (after binding the gridview) into DataTable ? I tried in the following way. But its giving Exception Unable Convert 'System.Web.UI.WebControls.GridViewRowCollection' to 'System.Data.DataRow' DataTable dt= new DataTable(); foreach(DataRow drow in Gridview1.Rows) { dt.Rows.Add(drow); } Please suggest me how to do that. Thanks in advance.
modified on Wednesday, October 29, 2008 2:32 AM
-
Hi all, How to get the Gridview data (after binding the gridview) into DataTable ? I tried in the following way. But its giving Exception Unable Convert 'System.Web.UI.WebControls.GridViewRowCollection' to 'System.Data.DataRow' DataTable dt= new DataTable(); foreach(DataRow drow in Gridview1.Rows) { dt.Rows.Add(drow); } Please suggest me how to do that. Thanks in advance.
modified on Wednesday, October 29, 2008 2:32 AM
-
Try this: foreach(GridViewRow drow in GridView1.Rows) { dt.Rows.Add(drow); }
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
Thanks for your suggestion. I will try that.
-
Hi all, How to get the Gridview data (after binding the gridview) into DataTable ? I tried in the following way. But its giving Exception Unable Convert 'System.Web.UI.WebControls.GridViewRowCollection' to 'System.Data.DataRow' DataTable dt= new DataTable(); foreach(DataRow drow in Gridview1.Rows) { dt.Rows.Add(drow); } Please suggest me how to do that. Thanks in advance.
modified on Wednesday, October 29, 2008 2:32 AM
Try this DataTable workTable= new DataTable(); DataRow workRow = workTable.NewRow(); foreach(DataRow workRow in Gridview1.Rows) { workRow["Fields"] = //your gridview rows dt.Rows.Add(workRow); }
-
Try this: foreach(GridViewRow drow in GridView1.Rows) { dt.Rows.Add(drow); }
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
Hello Meeram395, I tried. But i got exception System.ArgumentException: Input array is longer than the number of columns in this table. foreach(GridViewRow drow in GridView1.Rows) { dt.Rows.Add(drow); //Here i got the above exception }
-
Try this: foreach(GridViewRow drow in GridView1.Rows) { dt.Rows.Add(drow); }
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
Hello meeram395, I solved the that exception. But I am getting empty datarows. DataItem : null My logic: BindGridview(); DataTable dt = new DataTable(); dt.Columns.Add(); foreach (GridViewRow dr in GvReport.Rows) { dt.Rows.Add(dr); // I am getting the row count as 10. But DataItem is null }
-
Hello meeram395, I solved the that exception. But I am getting empty datarows. DataItem : null My logic: BindGridview(); DataTable dt = new DataTable(); dt.Columns.Add(); foreach (GridViewRow dr in GvReport.Rows) { dt.Rows.Add(dr); // I am getting the row count as 10. But DataItem is null }
I have tried as below and getting the results successfully:
DataTable dt = new DataTable();
dt.Columns.Add(); dt.Columns\[0\].ColumnName = "User Name"; dt.Columns.Add(); dt.Columns\[1\].ColumnName = "User Email"; foreach (GridViewRow row in SearchGrid.Rows) // SearchGrid is the name of the Grid. { DataRow dr = dt.NewRow(); dr\["User Name"\] = row.Cells\[1\].Text; dr\["User Email"\] = row.Cells\[2\].Text; dt.Rows.Add(dr); }
Hope this helps.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.