Forum guidelines
AuthorQuestion
convert data table to dataset in c#.net
Posted By venkata kumar on 14 Dec 2012
hi
can any one help me
how to convert data table to data set
or gridview to dataset



Thanks & Regards
Kumar

Thanks & Regards
Ravi Kumar

AuthorReply
Re: convert data table to dataset in c#.net
Posted By Santhosh Kumar Jayaraman on 14 Dec 2012  
You cannot convert datatable to dataset. Dataset is a container which can hold many datatables.

You can create new dataset and add your datatable to it.

DataSet ds=new DataSet();
ds.Tables.Add(dt);

here dt is my datatable.

Thanks & Regards,SanthoshPlease mark this answer as accepted answer if it resolves your problem.
Re: convert data table to dataset in c#.net
Posted By venkata kumar on 14 Dec 2012  
hi santhosh
can u help me how to assign datagridview data to data set or data table


Thanks & Regards
KUmar

Thanks & Regards
Ravi Kumar

Re: convert data table to dataset in c#.net
Posted By venkata kumar on 14 Dec 2012  
hi this is the way to convert datagridview to dataset DataSet ds2 = ((DataView)dataGridView1.DataSource).Table.DataSet;
Thanks & Regards
Ravi Kumar

Re: convert data table to dataset in c#.net
Posted By Vishnujeet Kumar on 14 Dec 2012  
data bind in gridview
  SqlConnection con; 
  SqlCommand com; 
  SqlDataAdapter DA; 
 
 
  con = new SqlConnection(); 
  con.ConnectionString =  ConfigurationManager.ConnectionStrings["AppConnect"].ConnectionString; 
  con.Open();
  com = new SqlCommand(); 
  com.CommandText = "SELECT T* FROM PRODUCTS"; 
  com.CommandType = CommandType.Text; 
  com.Connection = con; 
 
  DA = new SqlDataAdapter(); 
  DA.SelectCommand = com; 
      DataSet ds = new DataSet();
  DA.Fill(ds); 
 
  GridView1.DataSource = ds; 
  GridView1.DataBind(); 
 
  DA.Dispose(); 
  con.Dispose(); 
  con.Dispose(); 

Thanks & Regards, Vishnujeet
Re: convert data table to dataset in c#.net
Posted By venkata kumar on 14 Dec 2012  
this is wrong
Thanks & Regards
Ravi Kumar

Re: convert data table to dataset in c#.net
Posted By jak npie on 21 Jan 2014  
if dataset can be a container of some datatable so a datatable can has one of datatable by index as follow:

 
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
      
    
        SqlDataAdapter adaptor = new SqlDataAdapter(new  sqlcom);
        adaptor.Fill(ds);

        dt = ds.Tables[0];// or avery index of it
       
//and then.....
        GridView1.DataSource = dt;
        GridView1.DataBind();


have luck

Re: convert data table to dataset in c#.net
Posted By jak npie on 21 Jan 2014  
if dataset can be a container of some datatable so a datatable can has one of datatable by index as follow:


DataTable dt = new DataTable();
DataSet ds = new DataSet();


SqlDataAdapter adaptor = new SqlDataAdapter(new sqlcom);
adaptor.Fill(ds);

dt = ds.Tables[0];// or avery index of it

//and then.....
GridView1.DataSource = dt;
GridView1.DataBind();


have luck


Re: convert data table to dataset in c#.net
Posted By Biswa Pujarini Mohapatra on 21 Jan 2014  

You can try this
DataTable dt = new DataTable();
for (int i = 0; i < GridView1.Columns.Count; i++)
    {
        dt.Columns.Add("column"+i.ToString());
    }
foreach (GridViewRow row in GridView1.Rows)
    {
        DataRow dr = dt.NewRow();
        for(int j = 0;j<GridView1.Columns.Count;j++)
            {
                dr["column" + j.ToString()] = row.Cells[j].Text;
            }

            dt.Rows.Add(dr);
    }


If this post helps you “Mark it as Answer” Thanks Biswa Pujarini
Training for .Net Developers

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.