ASP.NET - how to convert datagrid in to a data set?

Asked By aman on 10-Oct-11 02:50 AM
hi all,

I want to convert datagrid in to a data set n have been tried lot but unable to do
please help me with some good example....

thanks and regards
Aman
Reena Jain replied to aman on 10-Oct-11 02:54 AM
Hi,

Its easy to convert datagrid to datatable

//If DataSource of GridView1 is a DataTable
  DataTable dt = (DataTable)GridView1.DataSource;
 
//If DataSource of GridView1 is a DataView
  DataView dv = (DataView)GridView1.DataSource;
//then add the datatable dt to dataset like this
 
dataset ds=new dataset();
ds.Add(dt);

Hope this will help you
Web Star replied to aman on 10-Oct-11 02:58 AM
Why you want to get dataset from datagrid you should bind datagrid with some datasoure like DataTable, DataSet  than better is you can directly store that Dataset to view or session just before DataBind and you will get when it needed .Even if you add new row in datagrid that will also add in stored datatable, dataset and again store it for future use.

Anoop S replied to aman on 10-Oct-11 03:01 AM
DataSet ds = new DataSet();
ds.Columns...

DataView dv = new DataView( ds.Tables[0] );

DataGrid grid = new DataGrid();
grid.DataSource = dv;

//Cast as DataView
DataView _newDV = (DataView) grid.DataSource
dipa ahuja replied to aman on 10-Oct-11 03:02 AM
try this way:

protected void CreatDataSet()
  {
    DataGrid grid1 = new DataGrid();
    DataTable dt = new DataTable();
 
    //adding column
    dt.Columns.Add("name"typeof(string));
    dt.Columns.Add("ID"typeof(int));
    foreach (DataGridItem di in grid1.Items)
    {
      //adding row
      DataRow row = dt.NewRow();
      row["ID"] = ((Label)di.FindControl("lblid")).Text.ToString();
      row["name"] = ((Label)di.FindControl("lblName")).Text.ToString();
 
      dt.Rows.Add(row);      
    }
    //creating dataset
    DataSet ds = new DataSet();
    ds.Tables.Add(dt);
  }
Devil Scorpio replied to aman on 10-Oct-11 03:13 AM
Hi Aman,

Use this small code to convert datagrid to a dataset

DataSet ds = new DataSet();
ds.Columns...

DataView dv = new DataView( ds.Tables[0] );

DataGrid grid = new DataGrid();
grid.DataSource = dv;

//Cast as DataView
DataView _newDV = (DataView) grid.DataSource
Rohan Dave replied to aman on 10-Oct-11 03:43 AM
try by below way..

DataSet objDS = new DataSet( );
DataTable objDt = GridView1.DataSource as DataTable;
objDS.Tables.Add(objDt);

that;s it now your DataSet holds gridview data within it...
Jitendra Faye replied to aman on 10-Oct-11 04:12 AM
Use this simple Code-

Dataset ds=new Dataset();
ds= DataGrid1.DataSource;


Try this and let me know.