c# - SelectedValue for ComboBox not getting set -
i have combobox name cmbcontacttype
. binded combo datasource displaymember valuename
(string type) , valuemember valueid
(number type). when user change value in cmbcontacttype cmbcontacttype.selectvalue set , able save document selectedvalue. facing problem while setting cmbcontacttype value database of type number. every time trying set value, null
value set cmbcontacttype.selectvalue
. following code binding datasource cmbcontacttype
, setting selectedvalue of cmbcontacttype
. using vs2010 , ms-access. please help.
//method bind datasource. public static void binddatasourcewithcombo(ref combobox cmb, string query, string valuemember, string displaymember) { datatable _tablesource = (new accessconnectionmanager()).getdatatablebysqlquery(query); var _datasource = (from datarow _row in _tablesource.rows select new { valuemember = _row[valuemember], displaymember = _row[displaymember].tostring() }).tolist(); cmb.displaymember = "displaymember"; cmb.valuemember = "valuemember"; cmb.datasource = _datasource; } // method set values in document. public void setdocumentproperties() { string _query = string.format("select contactcode,contactname,contacttype contactmaster contactcode = '{0}'", convert.tostring(cmbcontactcode.text)); datatable _table = accessconnectionmanagers.getdatatablebysqlquery(_query); if (_table.rows.count > 0) { datarow _row = _table.rows[0]; txtcontactcode.text = convert.tostring(_row["contactcode"]); txtcontactname.text = convert.tostring(_row["contactname"]); cmbcontacttype.selectedvalue = _row["contacttype"]; } else { txtcontactcode.text = string.empty; txtcontactname.text = string.empty; cmbcontacttype.selectedvalue = 1; } }
here code bind datasource cmbconacttype. method call on form_load event.
private void loadcontacttypecombo() { //table: picklistvalues(id,masterid,valueid,valuename) string _query = string.format("select valueid,valuename picklistvalues masterid = {0}", 1); binddatasourcewithcombo(ref cmbcontacttype, _query, "valueid", "valuename"); }
try method
int index; // search item matches string index=cmbcontacttype.findstring(_row["contacttype"]); // select item in combo cmbcontacttype.selectedindex=index;
Comments
Post a Comment