How to Filter the DataSet

Satyapriyanayak
Posted by Satyapriyanayak under C# category on | Points: 40 | Views : 783
These are three methods below...

Method-1

SqlConnection con=new SqlConnection("");
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds=new DataSet();
String str;
DataTable dt=new DataTable();

private void btndisplay_Click(object sender, System.EventArgs e)
{
con.Open();
str = "select * from student";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "student");
DataView dv = new DataView(ds.Tables["student"]);
dv.RowFilter = "smarks>70";
dv.Sort = "smarks asc";
listBox1.DataSource = dv;
listBox1.DisplayMember = "sid";
con.Close();
}


Method-2

private void btndisplay_Click(object sender, System.EventArgs e)
{
con.Open();
str = "select * from student";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "student");
dt = ds.Tables["student"];
String s1;
String s2;
s1 = "smarks>60";
s2 = "smarks asc";
DataRow []result;
result = dt.Select(s1, s2);
int ctr;
for( ctr =0;ctr<=result.Length - 1;ctr++)
listBox1.Items.Add(result[ctr]["smarks"].ToString());
}
}


Method-3

private void btndisplayall_Click(object sender, System.EventArgs e)
{
con.Open();
str="select * from student";
com=new SqlCommand(str,con);
sqlda=new SqlDataAdapter(com);
ds=new DataSet();
sqlda.Fill(ds,"student");
dataGrid1.DataSource=ds;
dataGrid1.DataMember="student";
con.Close();
}

private void btndisplay_Click(object sender, System.EventArgs e)
{
DataTable dt;
dt=ds.Tables["student"];
DataView dv=new DataView(dt);
dv.RowFilter ="sname='" + textBox1.Text + "'";
dataGrid1.DataSource =dv;

}

Comments or Responses

Login to post response