1. Create a table stud in database vijay as shown above
2. Design a form as shown above (Note: above displayed data will appear only while running)
3. For the DataGrid control right click on the toolbox and select choose items
4. From the list select the DataGrid – System.Windows.Forms
5. Write the following code.
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Gridview_ds_sort
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataSet gddataset()
{
SqlConnection cn = new SqlConnection("user id=sa;server=.;database=vijay");
SqlDataAdapter da = new SqlDataAdapter("Select * from stud",cn);
DataSet ds = new DataSet();
da.Fill(ds, "e");
return ds;
}
private void Form1_Load(object sender, EventArgs e)
{
DataSet ds = gddataset();
dataGrid1.SetDataBinding(ds,"e");
}
private void button1_Click(object sender, EventArgs e)
{
DataSet dt = (DataSet)dataGrid1.DataSource;
DataView dv = new DataView(dt.Tables[0]);
dv.Sort = "sname";
dataGrid1.DataSource = dv;
}
}
}