csharp_learner
asked on
Filtering a DataTable C#
Hi,
I have a DataTable where i would like to filter out a datarow.
Lets say my DataTable Column looks something like this:
ID , Name , Description , Model , Type
Can I do something like "Get Data from coulmn 5 where ID is 2"?
I have a DataTable where i would like to filter out a datarow.
Lets say my DataTable Column looks something like this:
ID , Name , Description , Model , Type
Can I do something like "Get Data from coulmn 5 where ID is 2"?
You can use DefaultView.RowFilter also
http://msdn.microsoft.com/en-us/library/system.data.datatable.defaultview(VS.71).aspx
http://msdn.microsoft.com/en-us/library/system.data.datatable.defaultview(VS.71).aspx
myDataTable.DefaultView.RowFilter = "ID = 2"
ASKER
So how can i get the value in column 5 after the
myDataTable.DefaultView.Ro wFilter = "ID = 2" statement?
myDataTable.DefaultView.Ro
DataRow[]Rows = DataTable.Select("ID = 2","");
Rows[0][5] would give the value in column five in row 0.
Rows[0][5] would give the value in column five in row 0.
using DataColumn Expression you can do something like
DataSet1.Tables("Orders"). Columns("O rderCount" ).Expressi on = "Count(OrderID)"
look at: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
DataSet1.Tables("Orders").
look at: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
ASKER
When i tried entering
DataRow[]Rows = DataTable.Select("ID = 2","");
the . Select is not in the list...the list consist of only Equals and ReferenceEquals
DataRow[]Rows = DataTable.Select("ID = 2","");
the . Select is not in the list...the list consist of only Equals and ReferenceEquals
try:
DataSet1.Tables("TableName ").Columns ("ID").Exp ression = "2
DataSet1.Tables("TableName
Change DataTable to your DataTable object, don't just use the class name:
DataRow[] Rows = myDataTable.Select("ID = 2","");
DataRow[] Rows = myDataTable.Select("ID = 2","");
ASKER
i tried
DataRow[] Rows = ordt.Select("ID = 2", "");
comboBox1.Text = ordt.Rows[0][5].ToString() ;
but again the same error came out" there is now row at position 0"
DataRow[] Rows = ordt.Select("ID = 2", "");
comboBox1.Text = ordt.Rows[0][5].ToString()
but again the same error came out" there is now row at position 0"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
to perform queries against a DataTable.
Decimal val = dt.Compute("Sum(quantity)"
http://msdn.microsoft.com/en-us/library/system.data.datatable.select.aspx
http://msdn.microsoft.com/en-us/library/system.data.datatable.compute.aspx