Domanda

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..

È stato utile?

Soluzione

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

Altri suggerimenti

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"];
DataTable dt = dv.ToTable();     
     for(int k=1;k<dt.Rows.Count;k++)  
     {  
        dt.Rows[k]["roomno"];
       //dv1.Table.Rows[k]["roomno"]);

     }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top