Fernando
Fernando

Reputation: 358

dataview rowfilter value to datatable convertion

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

Upvotes: 3

Views: 25649

Answers (4)

Sumant Singh
Sumant Singh

Reputation: 1020

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());

Upvotes: 0

Homer
Homer

Reputation: 7826

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;

Upvotes: 1

Manjunath K Mayya
Manjunath K Mayya

Reputation: 1118

You can use

   if (datavw.Count > 0)             
   {                 
     DT = datavw.ToTable(); // This will copy dataview's RowFilterd values to datatable              
   } 

Upvotes: 5

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

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

Upvotes: 3

Related Questions