-1

I've bind the data to a datagridview from a json like this:

dynamic result = JsonConvert.DeserializeObject<List<productData>>(temp);
mproductDataGridView.DataSource = result;

Later i want to do some filtering:

mproductDataGridViewstring rowFilter = string.Format("[{0}] = '{1}'", "type_id", "configurable");
DataTable dt = (DataTable)mproductDataGridView.DataSource;
dt.DefaultView.RowFilter = rowFilter;

There is an error (i translated):

cannot convert system.Collections.Generic.List to System.Data.DataTable object

Anyone know what is the problem? how to fix this?

hkguile
  • 4,235
  • 17
  • 68
  • 139
  • 1
    The problem is that a `List` is not a `DataTable`. Maybe you'll want to convert the list to a DataTable before setting the DataSource – Pikoh Mar 13 '17 at 10:02
  • Datatable to do filtering – hkguile Mar 13 '17 at 10:05
  • 1
    When I have JSon string i usually convert it to DataTable like this `DataTable dtSerialized = (DataTable)JsonConvert.DeserializeObject(jsonString, (typeof(DataTable)));` – J.SMTBCJ15 Mar 13 '17 at 11:21

2 Answers2

0

add this function and call it, it will convert List to DataTable.

public static DataTable ToDataTable<T>(List<T> items)
{
        DataTable dataTable = new DataTable(typeof(T).Name);

        //Get all the properties
        PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        foreach (PropertyInfo prop in Props)
        {
            //Defining type of data column gives proper data table 
            var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType);
            //Setting column names as Property names
            dataTable.Columns.Add(prop.Name, type);
        }
        foreach (T item in items)
        {
           var values = new object[Props.Length];
           for (int i = 0; i < Props.Length; i++)
           {
                //inserting property values to datatable rows
                values[i] = Props[i].GetValue(item, null);
           }
           dataTable.Rows.Add(values);
      }
      //put a breakpoint here and check datatable
      return dataTable;
}
Ravi Kanth
  • 1,182
  • 13
  • 38
0

The code you posted has a problem as far as getting only the filtered data into a new DataTable. In the posted second group of code it appears to be creating new DataTable called dt from mproductDataGridView.DataSource. Then you apply a filter, to this new table. Granted the filter applies, but the DataTable as a whole still contains all the un-filter data. One easy approach from @Alex Bagnolini at How to pass DataTable.Select() result to a new DataTable?

This approach creates a new DataTable FROM the filter string. You obviously will need to adjust the filter string to your requirements.

string formatString = "FirstName LIKE'" + txtFirstName.Text.Trim().Replace("'", "''") + "%' AND " +
                      "LastName LIKE'" + txtLastName.Text.Trim().Replace("'", "''") + "%'";
DataTable dt2 = result.Select(formatString).CopyToDataTable();

Hope this helps

Community
  • 1
  • 1
JohnG
  • 9,259
  • 2
  • 20
  • 29