How to Filter the DataSet?
Give me the Sample Coding about All type of Filtering of DataSet???
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted Jan 5, 2012, 3:33 AM
As per my knowledge Data set can not be filter and datatable and dataview can filter because dataset is collection of table.
for filtering datatable you may use.
DataTable dt = "some Method to retrive data"; // new DataTable
DataTable dt1 = dt.Select(filterExpression);
DataTable dt2 = dt.Select(filterExpression,sortingExpression);
hope this help.
Prabhu RajaPosted Nov 19, 2011, 6:41 AM
Referred from Article
Satyapriya NayakPosted Nov 19, 2011, 5:04 AM
These are three methods below...
Method-1
SqlConnection con=new SqlConnection("workstation id=\"HOME-Z8CKE1NER2\";packet size=4096;user id=sa;initial catalog=Dotn" +
"et;persist security info=False");
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;
}
Thanks