user3116746
user3116746

Reputation: 41

how to get a value from a filtered values of dataview in C#?

I have a filtered data view, from which I have to extract the values. Problem is that when I do this, I am getting the values from non filtered data also.

dv1.RowFilter = "collegeno=" +i; 
for(int k=1;k<dv1.count;k++)  
 {  
   //inserting data in database; there is column in database table; I am inserting into it;
 dv1.Table.Rows[k]["roomno"]);

 }

For ex: The total no. of rows in DataView is 200;
When i=1 I have 30 records;
If I supply k=4, then I should get fourth row from this 30 records.
But I am getting 4th row of the 200 records..

Upvotes: 0

Views: 3445

Answers (3)

Suraj Singh
Suraj Singh

Reputation: 4069

DataTable dt = dv.ToTable();     
     for(int k=1;k<dt.Rows.Count;k++)  
     {  
        dt.Rows[k]["roomno"];
       //dv1.Table.Rows[k]["roomno"]);

     }

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292765

If I supply k=4, then I should get fourth row from this 30 records. But I am getting 4th row of the 200 records..

That's because you're querying the table, not the view. Instead you should do this:

dv1.RowFilter = "collegeno=" +i; 
object value = dv1[k]["roomno"];

Depending on what you need, you might want to use the DataTable.Select method instead of a DataView:

var rows = table.Select("collegeno=" +i);
object value = rows[k]["roomno"];

Upvotes: 2

Harsh Baid
Harsh Baid

Reputation: 7249

The code you have written is accessing the main table using the code block dv1.Table.

Instead try as this

dv1[k]["roomno"]

This code works on DataView, on which the filter is applicable.

If you use DataView.Table then it will access the non-filtered results.

Reference Link: MSDN - DataView.RowFilter

Upvotes: 3

Related Questions