I have the following code
DataView dv = new DataView(dsModels.Tables[0]);
string strFilter = "MODEL = 'PISTON'";
dv.RowFilter = strFilter;
string strPN = dv.Table.Rows[0]["PN"].ToString();
The dataview dv
has a count of 35 rows prior to applying the filter.
After I apply the filter the dv.count is 1
But when i set the strPN to the value of the filtered DV i am getting the first row pn value. if i set string so with a 15; i get the 15 row pn value even though DV has a count of 1 ????
How do i get the value of the filter row where Model= PISTON
Taken from dataview.rowfilter
and dataview.count
:
The line dv.Table.Rows[0]["PN"].ToString();
is targeting the underlying datatable and not the current state of the dataview. You can copy the filtered rows using dv.ToTable()
This is a linqpad demo program. If you play around with it you will get the hang of it
void Main()
{
DataView dataview = GetTable().DefaultView;
dataview.RowFilter = "Name = 'John'";
DataTable dataTable = dataview.ToTable();
// dataview.Dump();
dataTable.Dump();
var row0 = dataview.Table.Rows[0];
var row1 = dataview.Table.Rows[1];
string.Join("|",row0.ItemArray).Dump();
string.Join("|",row1.ItemArray).Dump();
}
This is the helper method
static DataTable GetTable()
{
// DataTable with 4 columns
DataTable table = new DataTable("Students");
table.Columns.Add("Id", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Teacher", typeof(string));
table.Columns.Add("Birthday", typeof(DateTime));
// Add 5 rows
table.Rows.Add(1, "John", "Mr. Charles", DateTime.Parse("2001-01-01"));
table.Rows.Add(2, "Lennard", "Mr. Charles", DateTime.Parse("2002-02-02"));
table.Rows.Add(3, "John", "Lady Graham", DateTime.Parse("2003-03-03"));
table.Rows.Add(4, "Penny", "Mr. Charles", DateTime.Parse("2004-04-04"));
table.Rows.Add(5, "Sheldon", "Sir Winster", DateTime.Parse("2005-05-05"));
return table;
}
You might also take a look at roslynpad