I was having a problem when I have the same datatable and one datagrid but I want to display different field on the grid for different report and I want to use AutoGenerateColumn = true in the datagrid. Remember, it is about filtering fields not Row(If you want to filter row then you can use dataview).
This is the way of filtering field in datatable
public static DataTable FilterTableRemoveColumns(tdsReports.ReportSelectDataTable inputTable, List fields) { //create a new data table DataTable newTable = new DataTable(); newTable.TableName = "newtable"; //iterate through each column foreach (DataColumn col in inputTable.Columns) { //cross match and filter fields/column if (fields.Contains(col.ColumnName)) { //create a new datacolumn with the same column name and same datatype DataColumn newCol = new DataColumn(col.ColumnName, col.DataType); newTable.Columns.Add(newCol); } } //you ignore the schema because you don't want to throw the exception //you merge the data with the new schema newTable.Merge(inputTable, true, MissingSchemaAction.Ignore); return newTable; }
this is how you use it. You pass a string list into the function. The string list contains your desired column
Private Function FilterDataTableColumn(ByVal dtReport As tdsReports.ReportSelectDataTable) As DataTable Dim dt As DataTable = New DataTable() Try Dim list As List(Of String) = New List(Of String) If (ReportType = Enums.ReportType.DispatchedOrdersReport) Then list.Add("OrderID") list.Add("PaymentType") list.Add("ProductCode") list.Add("Qty") list.Add("Price") list.Add("Total") list.Add("IncGst") ElseIf (ReportType = Enums.ReportType.MonthySalesReport) Then list.Add("Qty") list.Add("ProductCode") list.Add("ProductName") ElseIf (ReportType = Enums.ReportType.OrderReport) Then list.Add("ProductCode") list.Add("Qty") list.Add("ProductName") End If dt = objReportService.FilterTableRemoveColumns(dtReport, list) Catch ex As Exception End Try Return dt End Function
Leave a Reply