c# - Keep Sum column at bottom when Header is clicked on DataGrid in Windows Application -


i have datagridview takes datasource datatable. have added in new row datatable adds total values of columns , displays fine until header clicked , data sorted , totals row appears half way grid!

i have been looking solution hours , can't seem find concrete. i've seen people overriding sortcompare method, doesn't used when there datasource. i've seen mentions of footer, seems asp, have seen bits , pieces of broken code windows applications.

basically want have count column @ bottom of grid , stays there when click on headers! thought straight forward bit of work it's proving tricky.

my grid made of 4 or 5 main columns of data, name, position etc , rest of columns dates based on filter, dynamic.

your appreciated.

what want handle sorted event, pinning desired row bottom when sort occurs. note new empty row must last row, that's trivial.

first, you'll want add tag desired row when create it.

datagridviewrow row = new datagridviewrow(); row.createcells(this.datagridview1); /* code sum columns... */ row.tag = "totals"; row.readonly = true; this.datagridview1.rows.add(row); 

then handle sorted event. i've factored out code sorted event method, you'll see why.

private void datagridview1_sorted(object sender, eventargs e) {   this.lockbottomrow(); }  private void lockbottomrow() {   foreach (datagridviewrow row in this.datagridview1.rows)   {     if (row.tag != null && row.tag.tostring() == "totals")     {       int index = this.datagridview1.rows.count - 2;       this.datagridview1.rows.remove(row);       this.datagridview1.rows.insert(index, row);     }   } } 

this lock totals row below other filled rows. bonus, thought nice when typing in new row, if instead of being added after totals row , needing resorted, handled in rowsadded event.

private void datagridview1_rowsadded(object sender, datagridviewrowsaddedeventargs e) {   datagridviewrow row = this.datagridview1.rows[e.rowindex];    if (row.tag == null || row.tag.tostring() != "totals")   {      if (this.datagridview1.currentcell != null)     {       this.lockbottomrow();       this.datagridview1.beginedit(false);     }   } } 

hope helps! enjoyed experimenting work.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -