Challenge manipulating combobox selectedvalue and selectedvaluechanged
-
Hi! i know people have asked a lot about comboboxes but it's not really about how to get the value or index or whatever.Let me explain.it's users access management application with users, roles and rights: i have a form editRight with 2 combobox cmbUsers to list users and cmbroles to list role of each users and 3 listboxes.let's pause here on load event i fill users combobox with users
private void EditRight_Load(object sender, EventArgs e) { try { //Load the user list in the dt_users dataset.dt_users and dt_roles are declare as global variables dt_users = UserManager.FindAll().Tables[0]; //set the combobox cmbUsers datasource cmbUsers.DataSource = dt_users.DefaultView; //set display member and value member cmbUsers.DisplayMember = "user_name"; cmbUsers.ValueMember = "user_id"; CallFillRoleCombo((int)cmbUsers.SelectedValue); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } private void CallFillRoleCombo(int uid) { try { //load users roles into dt_roles database dt_roles = UserRoleManager.FindByUser(uid).Tables[0]; //set combobox datasource cmbroles.DataSource = dt_roles.DefaultView; cmbroles.DisplayMember = dt_roles.Columns[2].ToString(); cmbroles.ValueMember = dt_roles.Columns[1].ToString(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } private void cmbUsers_SelectedValueChanged(object sender, EventArgs e) { CallFillRoleCombo((int)cmbUsers.SelectedValue); }
when i run the code its throws exception:Object reference not set to an instance of an object.It seems that its calling the cmbUsers_SelectedValueChanged eventhandler when setting the display member of the cmbUsers combobox. How to work around it?Thank you for reading this.eager to learn
-
Hi! i know people have asked a lot about comboboxes but it's not really about how to get the value or index or whatever.Let me explain.it's users access management application with users, roles and rights: i have a form editRight with 2 combobox cmbUsers to list users and cmbroles to list role of each users and 3 listboxes.let's pause here on load event i fill users combobox with users
private void EditRight_Load(object sender, EventArgs e) { try { //Load the user list in the dt_users dataset.dt_users and dt_roles are declare as global variables dt_users = UserManager.FindAll().Tables[0]; //set the combobox cmbUsers datasource cmbUsers.DataSource = dt_users.DefaultView; //set display member and value member cmbUsers.DisplayMember = "user_name"; cmbUsers.ValueMember = "user_id"; CallFillRoleCombo((int)cmbUsers.SelectedValue); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } private void CallFillRoleCombo(int uid) { try { //load users roles into dt_roles database dt_roles = UserRoleManager.FindByUser(uid).Tables[0]; //set combobox datasource cmbroles.DataSource = dt_roles.DefaultView; cmbroles.DisplayMember = dt_roles.Columns[2].ToString(); cmbroles.ValueMember = dt_roles.Columns[1].ToString(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } private void cmbUsers_SelectedValueChanged(object sender, EventArgs e) { CallFillRoleCombo((int)cmbUsers.SelectedValue); }
when i run the code its throws exception:Object reference not set to an instance of an object.It seems that its calling the cmbUsers_SelectedValueChanged eventhandler when setting the display member of the cmbUsers combobox. How to work around it?Thank you for reading this.eager to learn
In the following code for loading data in user combobox unsubscribe the event cmbUsers_SelectedValueChanged and after filling data subscribe again
private void EditRight_Load(object sender, EventArgs e)
{
try
{
//Load the user list in the dt_users dataset.dt_users and dt_roles are declare as global variables
dt_users = UserManager.FindAll().Tables[0];
//set the combobox cmbUsers datasource
cmbUsers.DataSource = dt_users.DefaultView;
//set display member and value member
cmbUsers.DisplayMember = "user_name";
cmbUsers.ValueMember = "user_id";CallFillRoleCombo((int)cmbUsers.SelectedValue);
}
}hope this works for you!
-
Hi! i know people have asked a lot about comboboxes but it's not really about how to get the value or index or whatever.Let me explain.it's users access management application with users, roles and rights: i have a form editRight with 2 combobox cmbUsers to list users and cmbroles to list role of each users and 3 listboxes.let's pause here on load event i fill users combobox with users
private void EditRight_Load(object sender, EventArgs e) { try { //Load the user list in the dt_users dataset.dt_users and dt_roles are declare as global variables dt_users = UserManager.FindAll().Tables[0]; //set the combobox cmbUsers datasource cmbUsers.DataSource = dt_users.DefaultView; //set display member and value member cmbUsers.DisplayMember = "user_name"; cmbUsers.ValueMember = "user_id"; CallFillRoleCombo((int)cmbUsers.SelectedValue); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } private void CallFillRoleCombo(int uid) { try { //load users roles into dt_roles database dt_roles = UserRoleManager.FindByUser(uid).Tables[0]; //set combobox datasource cmbroles.DataSource = dt_roles.DefaultView; cmbroles.DisplayMember = dt_roles.Columns[2].ToString(); cmbroles.ValueMember = dt_roles.Columns[1].ToString(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } private void cmbUsers_SelectedValueChanged(object sender, EventArgs e) { CallFillRoleCombo((int)cmbUsers.SelectedValue); }
when i run the code its throws exception:Object reference not set to an instance of an object.It seems that its calling the cmbUsers_SelectedValueChanged eventhandler when setting the display member of the cmbUsers combobox. How to work around it?Thank you for reading this.eager to learn
got your problem try this
private void cmbUsers_SelectedValueChanged(object sender, EventArgs e)
{
if (comboBox2.SelectedValue.GetType() == typeof(int))
CallFillRoleCombo((int)cmbUsers.SelectedValue);
}TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia> -------------------------------------------------------- 128 bit encrypted signature, crack if you can
-
Hi! i know people have asked a lot about comboboxes but it's not really about how to get the value or index or whatever.Let me explain.it's users access management application with users, roles and rights: i have a form editRight with 2 combobox cmbUsers to list users and cmbroles to list role of each users and 3 listboxes.let's pause here on load event i fill users combobox with users
private void EditRight_Load(object sender, EventArgs e) { try { //Load the user list in the dt_users dataset.dt_users and dt_roles are declare as global variables dt_users = UserManager.FindAll().Tables[0]; //set the combobox cmbUsers datasource cmbUsers.DataSource = dt_users.DefaultView; //set display member and value member cmbUsers.DisplayMember = "user_name"; cmbUsers.ValueMember = "user_id"; CallFillRoleCombo((int)cmbUsers.SelectedValue); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } private void CallFillRoleCombo(int uid) { try { //load users roles into dt_roles database dt_roles = UserRoleManager.FindByUser(uid).Tables[0]; //set combobox datasource cmbroles.DataSource = dt_roles.DefaultView; cmbroles.DisplayMember = dt_roles.Columns[2].ToString(); cmbroles.ValueMember = dt_roles.Columns[1].ToString(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } private void cmbUsers_SelectedValueChanged(object sender, EventArgs e) { CallFillRoleCombo((int)cmbUsers.SelectedValue); }
when i run the code its throws exception:Object reference not set to an instance of an object.It seems that its calling the cmbUsers_SelectedValueChanged eventhandler when setting the display member of the cmbUsers combobox. How to work around it?Thank you for reading this.eager to learn
-
Thanks guys for replying this post.Really apreciated it.The method that worked for me is the one suggested by deep@Pune.Thanks
eager to learn
your most welcome!! :) :) yersterday I forgot to include actual lines in the code to subscribe and unsubscribe the event!! :) soru for that!!!!!