filter a dropdownlist
how to filter a dropdownlist? i tried to make a code but its not working.. please check my code. tnx
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Source");
开发者_Go百科 string category;
category = drpCategory.SelectedValue;
DataRow[] foundRows;
foundRows = table.Select(category);
for (int i = 0; i < foundRows.Length; i++)
{
Console.WriteLine(foundRows[i][0]);
}
You can try using the DataTable.Select(string filterExpression) method.
Just few days ago I had to something similar. You have datatable's select method, you can use simple linq, or a lambda expression. Below is very small sample code. In your scenario, you simply pass in the category value as filter. Hope this helps.
DataTable Dt = new DataTable();
Dt.Columns.Add("Name");
Dt.Columns.Add("Age");
Dt.Rows.Add(new object[] { "Babar", 44 });
Dt.Rows.Add(new object[] { "Babul", 55 });
Dt.Rows.Add(new object[] { "Bahar", 66 });
Dt.Rows.Add(new object[] { "Baird", 3 });
Dt.Rows.Add(new object[] { "Cable", 77 });
// Linq option
var q = from r in Dt.AsEnumerable()
where r.Field<int>("Age") > 50
select r;
DataRow[] LinkFoundRows = q.ToArray<DataRow>();
// Lambda expression option (one liner)
DataRow[] FoundRows2 = Dt.AsEnumerable().Where(row => row.Field<int>("Age") > 50).ToArray<DataRow>();
DataRow[] StdSelect = Dt.Select("Age > 50");
// all three requests will result you DataRows containing Babar, Bahar, Cable
精彩评论