Wednesday, July 25, 2007

ADO.net DataSet : Sort Data in a DataSet: DataView

Dataset: Sort data in a Dataset: DataView

// Prerequisites: SqlConnection, SqlDataAdapter, DataSet, DataGrid objects
// Pass SQL connection with connection string
// initialize data adapter passing the query and connection object
// call fill method of data adapter with dataset object
// initialize dataview from tables propeties from dataset
// call Sort method of DataView
// Specify dataview object at the data source for datagrid
// call the bind method of datagrid

SqlConnection cn;
cn=new SqlConnection(ConnectionString);

SqlDataAdapter da;
DataSet ds=new DataSet();

da=new SqlDataAdapter("select emp_id, fname, lname from employee",cn);
da.Fill(ds);

DataView dtView = ds.Tables[0].DefaultView;
dtView.Sort = "fname ASC";

DataGrid1.DataSource=dtView;
DataGrid1.DataBind();

No comments:

Post a Comment