Getting the datarow from the selected control in a group of controls

dualshck012

Active member
Joined
Jan 14, 2018
Messages
29
Location
Leyte, Philippines
Programming Experience
1-3
I would like to know the workaround to get the datarow from the selected control (combobox) in a group of controls(combobox) from the flowlayoutpanel and pass the value to a variable
Here is the code for the adding of controls based from the database. I tried using via messagebox to test the datarow ID's retrieved by each created controls.

C#:
private void RetreiveAllpriorityRequest()
        {
            string query210A = "select statdesc, jorstatID from  ihsasstat where statactv = 'A' ";
            mysqlconstring.conn2.Open();
            mysqlconstring.cmda = mysqlconstring.conn2.CreateCommand();
            mysqlconstring.cmda.CommandType = CommandType.Text;
            mysqlconstring.cmda.CommandText = query210A;
            mysqlconstring.cmda.ExecuteNonQuery();
            mysqlconstring.adapta.SelectCommand = mysqlconstring.cmda;
            mysqlconstring.adapta.Fill(mysqlconstring.table3);

            foreach (DataRow DatRow in mysqlconstring.table3.Rows)
            {
                CheckBox chk = new CheckBox();
                chk.Width = 240;
                chk.FlatStyle = FlatStyle.Flat;
                //font here
                chk.Text = DatRow["statdesc"].ToString();
                //chk.CheckedChanged += new EventHandler(CheckBox_Checked);
                Jor_icts_prior_flw_pnel.AutoSize = true;
                Jor_icts_prior_flw_pnel.Controls.Add(chk);       
                    
//MessageBox.Show(DatRow["jorstatID"].ToString());
            }
            mysqlconstring.conn2.Close();
        }
 
Every control, including both CheckBoxes and ComboBoxes, has a Tag property for arbitrary data storage. You can assign the corresponding DataRow to the Tag of each control when you create it, then get it back again there at any time. The property is type Object, so you'll need to cast as type DataRow.

The alternative would be to use your own custom control that inherits CheckBox and adds a DataRow property.
 
I tried this one, but its not getting the boolean true condition to get the messagebox working, unless i change the value to false.

C#:
foreach (DataRow DatRow in mysqlconstring.table3.Rows)
            {
                CheckBox chk = new CheckBox();
                chk.Width = 240;
                chk.FlatStyle = FlatStyle.Flat;
                chk.Text = DatRow["statdesc"].ToString();
                chk.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);

                Jor_icts_prior_flw_pnel.AutoSize = true;
                Jor_icts_prior_flw_pnel.Controls.Add(chk);  
            }

        }
        public void CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataRow DatRow in mysqlconstring.table3.Rows)
            {
                CheckBox chk = new CheckBox();
                if (chk.Checked == true)
                {
                    MessageBox.Show(DatRow["jorstatID"].ToString());
                }
            }
        }

But i only managed to show all the DataRow values in the messagebox not according to the selected checkbox
 
On line 18, you are creating a brand new instance of a checkbox. It's the same instance which you created on line 3.

By convention, the sender parameter is the object for which the event was firing. You just need to cast it to the right type.
 
On line 18, you are creating a brand new instance of a checkbox. It's the same instance which you created on line 3.

By convention, the sender parameter is the object for which the event was firing. You just need to cast it to the right type.
revised the checkbox_CheckedChanged event and finally worked.


C#:
        public void CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataRow DatRow in mysqlconstring.table3.Rows)
            {

                CheckBox chk = (sender as CheckBox);
                
            
                if (chk.Checked == true)
                {
                    MessageBox.Show(DatRow["jorstat"].ToString());
                }

            }
        }

However, I'm getting all the values in the datarow through the messagebox instead of just one value based from the selected checkbox control because of the loop.
 
Why do you need the foreach loop?
 
Re-read post #2.
 
Did you read post #2? Did you do what it says? Assign the DataRow to the Tag property of the control when you create it. In the event handler, get the control first, then get the DataRow from its Tag property. No loop.
 
My solution and now working..


C#:
public void CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = (sender as CheckBox);
            
            if (chk.Checked == true)  //if a checkbox control is checked, get id of checkbox control in database
            {

                string query210B = "select statdesc, jorstat from ihsasstat where statactv = 'A' and statdesc = '" + chk.Text.ToString() + "'";

                mysqlconstring.cmd = mysqlconstring.conn2.CreateCommand();
                mysqlconstring.cmd.CommandType = CommandType.Text;
                mysqlconstring.cmd.CommandText = query210B;
                mysqlconstring.conn2.Open();
                mysqlconstring.reader3 = mysqlconstring.cmd.ExecuteReader();
                while (mysqlconstring.reader3.Read())
                {
                    MessageBox.Show(mysqlconstring.reader3["jorstat"].ToString());
                }
                mysqlconstring.conn2.Close();
            }
        } //04/15/2021 10:37PM

My new problem now is the checkbox functionality itself, since i know i should be using radio buttons instead of checkbox controls to make my work easier, but is there a way for it, that once i click a certain checkbox control inside the flowlayoutpanel, the other checkboxes will be unchecked to avoid multiple selection. I'm trying something like this below, but i can't managed to integrate it from the code.

C#:
private void CheckBox_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            Checkbox chkgrp
            for (int chklst = 0; chklst < chkgrp.Items.Count; ++chklst)
                if (chklst != e.Index)
                   chkgrpSetItemChecked(chklst, false);
        }
 
Why are you apparently handling an ItemCheck event? CheckBox controls don't have any such event. That might apply to a CheckedListBox but not individual CheckBox controls. CheckBox controls have a CheckedChanged event and that is the event you should be handling. You can handle the event for all the controls with the same method and then get the specific control inside the event handler, e.g.
C#:
private void CheckBoxes_CheckedChanged(object sender, EventArgs e)
{
    var currentCheckBox = (CheckBox) sender;
    
    if (currentCheckBox.Checked)
    {
        // The current box has been checked so uncheck the others in the group.
        var otherCheckBoxes = someContainer.Controls
                                           .OfType<CheckBox>()
                                           .Except(new[] {currentCheckBox})
                                           .ToArray();
        
        foreach (var otherCheckBox in otherCheckBoxes)
        {
            otherCheckBox.Checked = false;
        }
    }
}
 
Why are you apparently handling an ItemCheck event? CheckBox controls don't have any such event. That might apply to a CheckedListBox but not individual CheckBox controls. CheckBox controls have a CheckedChanged event and that is the event you should be handling. You can handle the event for all the controls with the same method and then get the specific control inside the event handler, e.g.
C#:
private void CheckBoxes_CheckedChanged(object sender, EventArgs e)
{
    var currentCheckBox = (CheckBox) sender;
    
    if (currentCheckBox.Checked)
    {
        // The current box has been checked so uncheck the others in the group.
        var otherCheckBoxes = someContainer.Controls
                                           .OfType<CheckBox>()
                                           .Except(new[] {currentCheckBox})
                                           .ToArray();
        
        foreach (var otherCheckBox in otherCheckBoxes)
        {
            otherCheckBox.Checked = false;
        }
    }
}
i see thanks, that was from checkbox list item control
 
i see thanks, that was from checkbox list item control
If you're talking about a WinForms control then it's a CheckedListBox. CheckBoxList is an ASP.NET Web Forms server control and it has no such event. Correct nomenclature is important because there are lots of different things with the same or similar name. No harm done in this case but that will not always be so, so you should endeavour to always refer to things by their actual, correct name.
 
Back
Top Bottom