<\/script>')

Filter data from dataset in datatable

How to filter data from dataset in datatable? like code β†’

DataRow[] dr = DS.Tables[0] .Select("STAGENAME='Develop' AND DEVLAPSEDAYS IS NOT NULL"); 

How can i use datatable here?

The following code does not reflect changes β†’

 DataTable FilteredDataD = DS.Tables[0]; if (FilteredDataD.Rows.Count > 0) { FilteredDataD.DefaultView.RowFilter = "STAGENAME='Develop' AND DEVLAPSEDAYS IS NOT NULL"; FilteredDataD.DefaultView.ToTable(); } 

Is it possible to delete a column using the above filter, for example, "STAGENAME =" Development ", and DEVLAPSEDAYS NOT NULL" + FilteredDataD.column ("col_name") ... Suppose I have 5 columns displaying only 4, I can’t remove col_name from my query. Is there any way?

Reply

+6
source share
2 answers

Try using LINQ instead:

 var table = DS.Tables[0].AsEnumerable().Where( r => r.Field<string>("STAGENAME") == "Develop" && r.Field<int?>("DEVLAPSEDAYS").HasValue).AsDataView().ToTable(); 

EDIT Changed AsDataView to AsDataView() for syntactic precision.
EDIT Provided .NET 2.0 compatible solution

 DataTable table = DS.Tables[0]; if (table.Rows.Count > 0) { table.DefaultView.RowFilter = "STAGENAME = 'DEVELOP' AND DEVLAPSEDAYS IS NOT NULL"; table = table.DefaultView.ToTable(); } 
+11
source

You can write an extension method (using C # 3) as shown below:

 public static DataTable Filter(this DataTable dataTable, string selectFilter) { var filteredTable = dataTable.Clone(); var rows = dataTable.Select(selectFilter).ToList(); rows.ForEach(filteredTable.ImportRow); return filteredTable; } 

Then use it as shown below:

 DataTable dataTable = DS.Tables[0] .Filter("STAGENAME='Develop' AND DEVLAPSEDAYS IS NOT NULL"); 

Update , since you said you were using C # 2.0 (and therefore extension methods and LINQ are not an option), you can use this instead:

 public static DataTable GetFilteredTable( DataTable sourceTable, string selectFilter) { var filteredTable = sourceTable.Clone(); var rows = sourceTable.Select(selectFilter); foreach (DataRow row in rows) { filteredTable.ImportRow(row); } return filteredTable; } DataTable dataTable = GetFilteredTable( DS.Tables[0], "STAGENAME='Develop' AND DEVLAPSEDAYS IS NOT NULL"); 
+6
source

Source: https://habr.com/ru/post/914135/


All Articles