datagridcomboboxcolumn - State, County, and City cascading comboboxcolumns in Datagridview -


state, county, , city cascading comboboxcolumns. created simple form datagridview. datagridview contians 3 comboboxcolumns (state, county , city). when select state county combobox should show counties within state, when select county, city column should show cities within selected state , county. need able change state , county @ point, should reset other corresponding columns.

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms;  namespace statecountycitydatagridviewcomboboxcolumnissue {     public partial class form1 : form     {         private datatable dtstatecountycity = new datatable();          public form1()         {             initializecomponent();              dtstatecountycity = createdatatable_statecountycity();              var statelist = (from scc in dtstatecountycity.asenumerable()                              group scc scc.field<string>("state") g                              select new { state = g.key }).tolist();              dgvcmbcolstate.datasource = statelist;             dgvcmbcolstate.displaymember = "state";             dgvcmbcolstate.valuemember = "state";         }          public datatable createdatatable_statecountycity()         {             datatable dt = new datatable();             dt.columns.add("state", typeof(string));             dt.columns.add("county", typeof(string));             dt.columns.add("city", typeof(string));              dt.rows.add("michigan", "oakland", "royal oak");             dt.rows.add("michigan", "oakland", "birmingham");             dt.rows.add("michigan", "oakland", "troy");             dt.rows.add("michigan", "macomb", "sterling heights");             dt.rows.add("michigan", "macomb", "warren");             dt.rows.add("illinois", "cook", "chicago");             dt.rows.add("illinois", "cook", "alsip");             dt.rows.add("illinois", "cook", "oak park");             dt.rows.add("illinois", "clinton", "albers");             dt.rows.add("illinois", "clinton", "aviston");               return dt;                 }          private void countycombobox_dropdown(object sender, eventargs e)         {             //int64 xrefid = convert.toint64(dgvdepartmentworkerscomp.currentrow.cells[6].value);             if (dgvstatecountycity.currentrow.cells[0].value != null)             {                 string stateselected = dgvstatecountycity.currentrow.cells[0].value.tostring();                 datarow[] drtemprows = dtstatecountycity.select("state = '" + stateselected + "'");                  if (drtemprows != null && drtemprows.length > 0)                 {                     datatable dttemp = drtemprows.copytodatatable();                      var countylist = (from tblcounty in dttemp.asenumerable()                                       group tblcounty tblcounty.field<string>("county") g                                      select new { county = g.key }).tolist();                      bindingsource bs = new bindingsource(countylist, "");                      dgvcmbcolcounty.datasource = bs;                     dgvcmbcolcounty.displaymember = "county";                     dgvcmbcolcounty.valuemember = "county";                  }             }          }          private void citycombobox_dropdown(object sender, eventargs e)         {             //int64 xrefid = convert.toint64(dgvdepartmentworkerscomp.currentrow.cells[6].value);             if (dgvstatecountycity.currentrow.cells[1].value != null)             {                 string countyselected = dgvstatecountycity.currentrow.cells[1].value.tostring();                 datarow[] drtemprows = dtstatecountycity.select("county = '" + countyselected + "'");                  if (drtemprows != null && drtemprows.length > 0)                 {                     datatable dttemp = drtemprows.copytodatatable();                     bindingsource bs = new bindingsource(dttemp, "");                     dgvcmbcolcity.datasource = bs;                     dgvcmbcolcity.displaymember = "city";                     dgvcmbcolcity.valuemember = "city";                  }             }          }          private void stateselectionchanged(object sender, eventargs e)         {           }          private void dgvstatecountycity_editingcontrolshowing(object sender, datagridvieweditingcontrolshowingeventargs e)         {             int statecolumnindex = 0;             int countycolumnindex = 1;             int citycolumnindex = 2;              if (e.control combobox)             {                 if (dgvstatecountycity.currentcell.columnindex == countycolumnindex)                 {                     combobox countycombobox = e.control combobox;                     if (countycombobox != null)                     {                         countycombobox.dropdown += new eventhandler(countycombobox_dropdown);                     }                 }                  if (dgvstatecountycity.currentcell.columnindex == citycolumnindex)                 {                     combobox citycombobox = e.control combobox;                     if (citycombobox != null)                     {                         citycombobox.dropdown += new eventhandler(citycombobox_dropdown);                     }                 }                  //register selectedvaluechanged event , reset item combobox default if category changes                 if (dgvstatecountycity.currentcell != null && dgvstatecountycity.currentcell.columnindex == statecolumnindex)                 {                     combobox statecombobox = e.control combobox;                     if (statecombobox != null)                     {                         statecombobox.selectedvaluechanged += new eventhandler(stateselectionchanged);                     }                 }             }         }          //because filtering way can make cell have value not contained in          //the datagridviewcomboboxcolumn.items, have handle dataerror         private void dgvstatecountycity_dataerror(object sender, datagridviewdataerroreventargs e)         {             //we're interested in datagridviewcomboboxcolumn             if (dgvstatecountycity.columns[e.columnindex] datagridviewcomboboxcolumn)             {                 e.cancel = true;             }         }      } } 

any ideas on complete this? appreciate help!

i found answer hidden somewhere else. fixed code , here is. key have use datagridviewcomboboxcell. working sample. use , without warrenties.

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms;  namespace statecountycitydatagridviewcomboboxcolumnissue {     public partial class form1 : form     {         private datatable dtstatecountycity = new datatable();          public form1()         {             initializecomponent();              dtstatecountycity = createdatatable_statecountycity();              var statelist = (from scc in dtstatecountycity.asenumerable()                              group scc scc.field<string>("state") g                              select new { state = g.key }).tolist();              dgvcmbcolstate.datasource = statelist;             dgvcmbcolstate.displaymember = "state";             dgvcmbcolstate.valuemember = "state";         }          public datatable createdatatable_statecountycity()         {             datatable dt = new datatable();             dt.columns.add("state", typeof(string));             dt.columns.add("county", typeof(string));             dt.columns.add("city", typeof(string));              dt.rows.add("michigan", "oakland", "royal oak");             dt.rows.add("michigan", "oakland", "birmingham");             dt.rows.add("michigan", "oakland", "troy");             dt.rows.add("michigan", "macomb", "sterling heights");             dt.rows.add("michigan", "macomb", "warren");             dt.rows.add("illinois", "cook", "chicago");             dt.rows.add("illinois", "cook", "alsip");             dt.rows.add("illinois", "cook", "oak park");             dt.rows.add("illinois", "clinton", "albers");             dt.rows.add("illinois", "clinton", "aviston");               return dt;                 }           //because filtering way can make cell have value not contained in          //the datagridviewcomboboxcolumn.items, have handle dataerror         private void dgvstatecountycity_dataerror(object sender, datagridviewdataerroreventargs e)         {             //we're interested in datagridviewcomboboxcolumn             if (dgvstatecountycity.columns[e.columnindex] datagridviewcomboboxcolumn)             {                 e.cancel = true;             }         }          private void dgvstatecountycity_cellclick(object sender, datagridviewcelleventargs e)         {             if (e.columnindex == 1)             {                 if (dgvstatecountycity.currentrow.cells[0].value != null)                 {                     string stateselected = dgvstatecountycity.currentrow.cells[0].value.tostring();                     datarow[] drtemprows = dtstatecountycity.select("state = '" + stateselected + "'");                      if (drtemprows != null && drtemprows.length > 0)                     {                         datatable dttemp = drtemprows.copytodatatable();                          var countylist = (from tblcounty in dttemp.asenumerable()                                           group tblcounty tblcounty.field<string>("county") g                                           select new { county = g.key }).tolist();                          bindingsource bs = new bindingsource(countylist, "");                          // note: here changed previous code                         datagridviewcomboboxcell combocell = dgvstatecountycity[1, dgvstatecountycity.currentrow.index] datagridviewcomboboxcell;                         combocell.datasource = new bindingsource(bs, null);                         combocell.displaymember = "county"; //name of column indatatable display!!                         combocell.valuemember = "county"; // vlaue if needed                        }                 }             }              if (e.columnindex == 2)             {                 if (dgvstatecountycity.currentrow.cells[1].value != null)                 {                     string countyselected = dgvstatecountycity.currentrow.cells[1].value.tostring();                     datarow[] drtemprows = dtstatecountycity.select("county = '" + countyselected + "'");                      if (drtemprows != null && drtemprows.length > 0)                     {                         datatable dttemp = drtemprows.copytodatatable();                         bindingsource bs = new bindingsource(dttemp, "");                          // note: here changed previous code                         datagridviewcomboboxcell combocell2 = dgvstatecountycity[2, dgvstatecountycity.currentrow.index] datagridviewcomboboxcell;                         combocell2.datasource = new bindingsource(bs, null);                         combocell2.displaymember = "city"; //name of column indatatable display!!                         combocell2.valuemember = "city"; // vlaue if needed                       }                 }              }          }          private void dgvstatecountycity_editingcontrolshowing(object sender, datagridvieweditingcontrolshowingeventargs e)         {                    //register selectedvaluechanged event , reset item combobox default if category changes             if (dgvstatecountycity.currentcell.columnindex == 0 )             {                 combobox combobox = e.control combobox;                 combobox.selectedindexchanged -= new eventhandler(dgvcbstate_selectedvaluechanged);                 combobox.selectedindexchanged += new eventhandler(dgvcbstate_selectedvaluechanged);             }         }           private void dgvcbstate_selectedvaluechanged(object sender, eventargs e)         {             //if category value changed reset item default.             if (dgvstatecountycity.currentcell.columnindex == 0)             {                 if (((datagridviewcomboboxeditingcontrol)sender).editingcontrolvaluechanged == true)                 {                     dgvstatecountycity.currentrow.cells[1].value = "";                     dgvstatecountycity.currentrow.cells[2].value = "";                 }             }          }     } } 

Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -