There is a DataGridView this kind:
Example

How can you sort the rows by the number of CheckBox.IsChecked == true values ​​in the string (the row with the largest number, then less, and so on)?

DB from Microsoft Office Access connect through "Data" -> "Add a new source." Then from the "Data Sources" put the table on the form.

  • As I understand it, there is an example for sorting by one column. And I need, roughly speaking, to add the number of true values ​​in each line and then sort by these values - bfdbndfnd
  • I'm new and do not quite understand what kind of code I need to put. Table in access, connect via data - add new source - bfdbndfnd
  • In grid accessibility table - bfdbndfnd
  • I connect the database from the access via Data - add a new source. Then from Iztochniki data I place the table on the form - bfdbndfnd
  • deleted irrelevant comments. I would try to make a separate table for display with an additional calculated column, by which to perform sorting. But it will turn out to be beautiful to do it, or hellish crutch will be not sure. - rdorn

1 answer 1

It turned out something like this:

 DataTable dt = new DataTable(); dt.Columns.Add("i1", typeof(int)); dt.Columns.Add("b1", typeof(bool)); dt.Columns.Add("b2", typeof(bool)); dt.Rows.Add(1, false, false); dt.Rows.Add(2, true, false); dt.Rows.Add(3, true, true); dt.Rows.Add(4, false, true); var rows = dt.Rows.Cast<DataRow>().ToList(); rows.Sort((x,y) => { int c = 0; foreach(var f in x.ItemArray) { if(f is bool && (bool)f) c++; } foreach(var f in y.ItemArray) { if(f is bool && (bool)f) c--; } return c; }); foreach(var r in rows) { Console.WriteLine(r["i1"]); } 

Sorts by the number of true values. And feed the resulting List<DataRow> to the DataGridView.DataSource . I think more you adapt to your goals if it is still relevant.