3

I want to convert dataview rowfilter value to datatable. I have a dataset with value. Now i was filter the value using dataview. Now i want to convert dataview filter values to datatable.please help me to copy it........

My partial code is here:

   DataSet5TableAdapters.sp_getallempleaveTableAdapter TA = new DataSet5TableAdapters.sp_getallempleaveTableAdapter();
   DataSet5.sp_getallempleaveDataTable DS = TA.GetData();
        if (DS.Rows.Count > 0)
        {
            DataView datavw = new DataView();
            datavw = DS.DefaultView;
            datavw.RowFilter = "fldempid='" + txtempid.Text + "' and fldempname='" + txtempname.Text + "'";
            if (datavw.Count > 0)
            {
                DT = datavw.Table; // i want to copy dataview row filter value to datatable 

            }
         }

please help me...

Fernando
  • 358
  • 3
  • 8
  • 21

4 Answers4

5

You can use

   if (datavw.Count > 0)             
   {                 
     DT = datavw.ToTable(); // This will copy dataview's RowFilterd values to datatable              
   } 
Manjunath K Mayya
  • 1,078
  • 1
  • 11
  • 20
3

You can use DateView.ToTable() for converting the filtered dataview in to a datatable.

DataTable DTb = new DataTable();
DTb = SortView.ToTable();
1

The answer does not work for my situation because I have columns with expressions. DataView.ToTable() will only copy the values, not the expressions.

First I tried this:

//clone the source table
DataTable filtered = dt.Clone();

//fill the clone with the filtered rows
foreach (DataRowView drv in dt.DefaultView)
{
    filtered.Rows.Add(drv.Row.ItemArray);
}
dt = filtered;

but that solution was very slow, even for just 1000 rows.

The solution that worked for me is:

//create a DataTable from the filtered DataView
DataTable filtered = dt.DefaultView.ToTable();

//loop through the columns of the source table and copy the expression to the new table
foreach (DataColumn dc in dt.Columns) 
{
    if (dc.Expression != "")
    {
        filtered.Columns[dc.ColumnName].Expression = dc.Expression;
    }
}
dt = filtered;
Homer
  • 7,594
  • 14
  • 69
  • 109
0

The below code is for Row filter from Dataview and the result converted in DataTable

                itemCondView.RowFilter = "DeliveryNumber = '1001'";
                dataSet = new DataSet();
                dataSet.Tables.Add(itemCondView.ToTable());
Sumant Singh
  • 904
  • 1
  • 14
  • 16