简体   繁体   中英

Dynamic DataGridView Filtering c#

I just started to work on c# and sql, I searched but I couldn't find any solution to my problem I hope someone will help me. I have a DataGridView on the form which is created by dynamically. I get 8 columns from database using LINQ to SQL. but I also create another column name as a "Calculation" which is using the other columns data. My question is I need to filter my table based on calculation column. Below part of my code:

// get datas from database.
var query = _context.Database.SqlQuery<my_Type>("my_stored_procedure");

var binding = new BindingList<my_Type>(query.ToList());
            bsMasterPrimary.DataSource = binding;
            myDataGridView.DataSource = bsMasterPrimary;


//filter

DataView dv = new DataView();

dv = myDataGridView.DataSource as DataView;//dv always comes null 



dv.RowFilter = string.format("Calculation <0");
  //  dt = (DataTable)((BindingSource)this.dgvAmazonFBAInventory.DataSource).DataSource;

I tried this too but it gave an error:

Unable to cast object of type 'System.ComponentModel.BindingList`1[MyProject.MyType]' to type 'System.Data.DataTable'. }

使用嵌套的for循环将数据网格视图的值分配给数据表更容易,或者可以将数据网格转换为数据视图,然后将数据视图转换为数据表

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM