Share via


How to get the rows in the DefaultView of DataTable?

Question

Monday, August 23, 2010 7:57 PM

Hi, All,

I've a DefaultView with some RowFilter like:

dt.DefaultView.RowFilter = "Name LIKE '%Andrew%'";

How can I get all rows in the DefaultView? 

 

Thanks,

Andrew Huang

[email protected]

All replies (5)

Tuesday, August 24, 2010 1:31 PM âś…Answered

public static void DataTest()
    {
      DataTable dt = new DataTable();
      DataColumn dc = new DataColumn("Name", Type.GetType("System.String"));
      DataColumn dc2 = new DataColumn("First", Type.GetType("System.String"));
      dt.Columns.Add(dc);
      dt.Columns.Add(dc2);
      for(int i = 0; i < 100; i ++)
      {
        DataRow dr = dt.NewRow();

        dr[0] = i.ToString();
        dr[1] = i.ToString();
        dt.Rows.Add(dr);
      }
      DataRow dr2 = dt.NewRow();

      dr2[0] = "Andrew";
      dr2[1] = "forty";
      dt.Rows.Add(dr2);
      int count1 = dt.Rows.Count;
      dt.DefaultView.RowFilter = "Name LIKE '%Andrew%'";
      
      int count2 = dt.DefaultView.Count;
      //count1 = 101, count2 = 1
    }

Monday, August 23, 2010 8:09 PM

Did you try to get the count? http://msdn.microsoft.com/en-us/library/system.data.dataview_members(v=VS.90).aspx

Count Gets the number of records in the DataView after RowFilter and RowStateFilter have been applied.

Monday, August 23, 2010 9:24 PM

Did you try to get the count? http://msdn.microsoft.com/en-us/library/system.data.dataview_members(v=VS.90).aspx

 

Count Gets the number of records in the DataView after RowFilter and RowStateFilter have been applied.

 

Thanks, Louis.

I mean, just like get the rows of the datatable:

foreach (datatableRow row in dt.Rows)
 row.Name="AAA";
int count=dt.Count(c=>c.Name=="Andrew");

Get all rows and using LINQ for the defaultView.

 

Thanks,

Andrew Huang

[email protected]


Tuesday, August 24, 2010 3:45 PM

Thanks, again.

DONE.

Thanks,

Andrew Huang

[email protected]


Tuesday, August 24, 2010 4:59 PM

Awesome! That was fun!