Getting CheckBoxList Item values

前端 未结 8 715
野的像风
野的像风 2021-01-04 10:53

I have a CheckBoxList which I\'m populating with data. When I attempt to retrieve the checked items from the list I can only grab the item ordinal, I cannot get the value.

相关标签:
8条回答
  • 2021-01-04 11:22

    Try to use this :

     private void button1_Click(object sender, EventArgs e)
        {
    
            for (int i = 0; i < chBoxListTables.Items.Count; i++)
                if (chBoxListTables.GetItemCheckState(i) == CheckState.Checked)
                {
                   txtBx.text += chBoxListTables.Items[i].ToString() + " \n"; 
    
                }
        }
    
    0 讨论(0)
  • 2021-01-04 11:23

    This ended up being quite simple. chBoxListTables.Item[i] is a string value, and an explicit convert allowed it to be loaded into a variable. The following code works:

    private void btnGO_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < chBoxListTables.Items.Count; i++)
        {
              if (chBoxListTables.GetItemChecked(i))
            {
                string str = (string)chBoxListTables.Items[i];
                MessageBox.Show(str);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题