none
Datagridview filtering RRS feed

  • Question

  • Hello,

    I am quite new linq to sql. I have dynamic DataGridView on my form.Part of its data comes from the database .I am using linq to sql for this.And I also create another column in this DataGridView based on some calculations comes from other columns .So my question is when I try to filter my DataGridView I failed.I tried to bind my DataGridView to datatable but the column that I add dynamically always comes null and I need to filter my Datagridview based on this column.So any ideas how can I do this?

    Friday, May 13, 2016 5:46 PM

Answers

  • Hi dmmc_,

    I create a demo and reproduce your issue on my side by using your code, it seem that the type of myDataGridView.DataSource is BindList, so that you couldn't cast it to DataView, I would suggest that you could convert the List to datatable by using the following code.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    
    namespace DataGridViewDemo
    {
        public class Utils
        {
            // <T> is the type of data in the list.
            // If you have a List<int>, for example, then call this as follows:
            // List<int> ListOfInt;
            // DataTable ListTable = BuildDataTable<int>(ListOfInt);
            public static DataTable BuildDataTable<T>(IList<T> lst)
            {
                //create DataTable Structure
                DataTable tbl = CreateTable<T>();
                Type entType = typeof(T);
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
                //get the list item and add into the list
                foreach (T item in lst)
                {
                    DataRow row = tbl.NewRow();
                    foreach (PropertyDescriptor prop in properties)
                    {
                        row[prop.Name] = prop.GetValue(item);
                    }
                    tbl.Rows.Add(row);
                }
                return tbl;
            }
    
            private static DataTable CreateTable<T>()
            {
                //T –> ClassName
                Type entType = typeof(T);
                //set the datatable name as class name
                DataTable tbl = new DataTable(entType.Name);
                //get the property list
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
                foreach (PropertyDescriptor prop in properties)
                {
                    //add property as column
                    tbl.Columns.Add(prop.Name, prop.PropertyType);
                }
                return tbl;
            }
        }
    }
    

    usage:

     var query = db.Database.SqlQuery<my_stored_procedure_Result>("my_stored_procedure");
     dt = Utils.BuildDataTable<my_stored_procedure_Result>(query.ToList());

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 23, 2016 1:52 PM
    Moderator

All replies

  • Hi dmmc,

    >>So any ideas how can I do this?

    According to your description, I'm not sure what could cause the issue, could you please provide your LINQ to SQL statement, which we could reproduce your my issue on our side.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 16, 2016 1:27 AM
    Moderator
  • var query = DB_Context.Database.SqlQuery<my_type>("my_stored_procedure");
     
    var binding = new BindingList<my_type>(query.ToList());
                bsMasterPrimary.DataSource = binding;
                myDataGridView.DataSource = bsMasterPrimary; 
    First I tried to filter my DatagridView like this//filter
    DataView dv = new DataView(); 
    dv = myDataGridView.DataSource as DataView;//dv always comes null 
    dv.RowFilter = string.format("Calculation <0");
    
    Then I did this:
       foreach (DataGridViewColumn c in myDataGridView.Columns)
                 {
                     DataColumn dc = new DataColumn();
                     dc.ColumnName = c.Name;
                     dc.DataType = c.ValueType;
                     dt.Columns.Add(dc);
     
                 }
                foreach (DataGridViewRow r in MyGridView.Rows)
                 {
                     DataRow drow = dt.NewRow();
     
                     foreach (DataGridViewCell cell in r.Cells)
                    {
     
                
                         drow[cell.OwningColumn.Name] = cell.Value;
                     }
     
                    dt.Rows.Add(drow);
                 }
    
    //this is part of my codes.I failed to get data s from the //column that I create dynamically. Thank you for your //helps
    

    Tuesday, May 17, 2016 1:08 PM
  • Hi dmmc_,

    I create a demo and reproduce your issue on my side by using your code, it seem that the type of myDataGridView.DataSource is BindList, so that you couldn't cast it to DataView, I would suggest that you could convert the List to datatable by using the following code.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    
    namespace DataGridViewDemo
    {
        public class Utils
        {
            // <T> is the type of data in the list.
            // If you have a List<int>, for example, then call this as follows:
            // List<int> ListOfInt;
            // DataTable ListTable = BuildDataTable<int>(ListOfInt);
            public static DataTable BuildDataTable<T>(IList<T> lst)
            {
                //create DataTable Structure
                DataTable tbl = CreateTable<T>();
                Type entType = typeof(T);
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
                //get the list item and add into the list
                foreach (T item in lst)
                {
                    DataRow row = tbl.NewRow();
                    foreach (PropertyDescriptor prop in properties)
                    {
                        row[prop.Name] = prop.GetValue(item);
                    }
                    tbl.Rows.Add(row);
                }
                return tbl;
            }
    
            private static DataTable CreateTable<T>()
            {
                //T –> ClassName
                Type entType = typeof(T);
                //set the datatable name as class name
                DataTable tbl = new DataTable(entType.Name);
                //get the property list
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
                foreach (PropertyDescriptor prop in properties)
                {
                    //add property as column
                    tbl.Columns.Add(prop.Name, prop.PropertyType);
                }
                return tbl;
            }
        }
    }
    

    usage:

     var query = db.Database.SqlQuery<my_stored_procedure_Result>("my_stored_procedure");
     dt = Utils.BuildDataTable<my_stored_procedure_Result>(query.ToList());

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 23, 2016 1:52 PM
    Moderator