c# - Group cells in a DataTable -
i have datatable, holding decimal values in cells. in table, need have 3 value groups identical values in each group's cells spread on table.
with 3 numupdown controls, wish control values in each 1 of groups cells in table.
i can access , control value of each cell in table (mytable.rows[rowindex][colindex] = numupdown.value), did not find way define groups , distinguish them each other.
asaik, table cells have no properties can used distinguish them each other. 1 option may building of another, identical indexing table hold grouping information, looking more straight forward.
(there may workaround using actual initial identical values in group, far being safe). idea highly appreciated!
as suggested, solved adding columns table. each cell, there added cell color (or other) information. when datagrid opens, reads added cells , decides on basic cells color information in cells. here full working code of test program designed: (to run, build winform datagridview 'faregrid' , 3 numupdown controls) (i still have problem colors not work (see comments in code), i'll post separate question). small part of program calculates variable electrical energy cost per used time (day-of-week, hour, season)
using system; using system.data; using system.drawing; using system.web.ui.webcontrols; using system.windows.forms; //this test program following: //1. build empty data table of 26 rows 6 columns. //2. table datasource datagridview. //3. datagrid colomns 0-3 can filled decimals 1 of 3 numericupdown controls. //3. each cell in cols 0-2 there 1 in cols 3-5 holds info numupdn used. //4. each numupdn has specific color. corresponding cells have same backcolor. //5. when program starts, builds table settings, , displays table in datagrid. //6. cells in cols 0-2 supposed colors ad indicated in cols 3-5, not. namespace datagridviewtest { public partial class form1 : form { datatable faretable = new datatable(); int rowindex; int colindex; public form1() { initializecomponent(); buildfaretable("faretable"); // load table (if exists) if (properties.settings.default.faretable != null) faretable = properties.settings.default.faretable; paintfaretablecells(faretable); } //build initial data table of 26r x 6c //columns 3-5 used hold color information of cols 0-2 public void buildfaretable(string faretablename) { faretable.tablename = faretablename; faretable.columns.add("weekday", typeof(decimal)); faretable.columns.add("halfday", typeof(decimal)); faretable.columns.add("weekend", typeof(decimal)); //adding 3 indexing columns hold tables cell group faretable.columns.add("iweekday", typeof(string)); faretable.columns.add("ihalfday", typeof(string)); faretable.columns.add("iweekend", typeof(string)); (rowindex = 0; rowindex < 26; rowindex++) { faretable.rows.add(); } } public void paintfaretablecells(datatable faretable) { faredatagrid.datasource = faretable; (rowindex = 0; rowindex < 26; rowindex++) { (colindex = 0; colindex < 3; colindex++) { switch (faretable.rows[rowindex][colindex + 3].tostring()) //check color index columns 3-5 { case "low": faredatagrid[colindex, rowindex].style.backcolor = color.green; break; case "med": faredatagrid[colindex, rowindex].style.backcolor = color.yellow; break; case "high": faredatagrid[colindex, rowindex].style.backcolor = color.red; break; default: break; } // diagnostics: check cell's color. @ point, colors ok! color color = faredatagrid[colindex, rowindex].style.backcolor; } } } //update cells 3 numupdn controls. private void numericupdownfarehigh_valuechanged(object sender, eventargs e) { editfaretablecells(sender); } private void numericupdownfaremed_valuechanged(object sender, eventargs e) { editfaretablecells(sender); } private void numericupdownfarelow_valuechanged(object sender, eventargs e) { editfaretablecells(sender); } public void editfaretablecells(object sender) { foreach (datagridviewcell cell in faredatagrid.selectedcells) { if (cell.columnindex < 3) { cell.value = ((numericupdown)sender).value; cell.style.backcolor = ((numericupdown)sender).backcolor; faredatagrid[cell.columnindex + 3, cell.rowindex].value = ((numericupdown)sender).tag; //uses cols 3-5 hold cost level info. } } } //use settings preserve data private void form1_formclosing(object sender, formclosingeventargs e) { properties.settings.default.faretable = this.faretable; properties.settings.default.save(); } } }
Comments
Post a Comment