C# – How to fill a dataset after getting a gridview

asp.netcdatasetgridviewsorting

I have a gridview which has many columns.. the columns are got separately and displayed in a gridview.

now i need to sort this gridview but i cannot do that…. i have found a way but i will need to get the gridview in a datatable or a dataset…. is there a a way to do this?

DataSet ds= new DataSet();
ds = Gridview1.????

please help..

Best Answer

One way to do this would be to use a DataGridView and use the DataSource property of this control:

OleDbConnection conn = new OleDbConnection(connection);

OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand("select * from my_table", conn);
DataSet dataset = new DataSet();

// fill the data set using the query defined in the adapter
adapter.Fill(dataset);

// fill the grid with the data set
DataGridView1.DataSource = dataset.Tables[0];