How do I add checked item(s) in my checkedlistbox

2019-07-30 15:54发布

I have a dropdownlist that selects category list from Database and display the product name onto the checkedlistbox. But how do I grabs the checked items from the checkedlistbox to the DataGridView? This is how my design looks like:

Winform design

Also, how do I make every medication that has been added to the Gridview has a default value of 1 Quantity?

2条回答
成全新的幸福
2楼-- · 2019-07-30 16:27

Add this code to your Add button click event:

private void button1_Click(object sender, EventArgs e)
{
    for (int i = 0; i < checkedListBox1.Items.Count; i++)
    {
        if (checkedListBox1.GetItemChecked(i))
        {
            dataGridView1.Rows.Add(checkedListBox1.Items[i], "1");
        }
    }
}
查看更多
老娘就宠你
3楼-- · 2019-07-30 16:28
  for (int i = 0; i <= checkedListBox1.SelectedItems.Count - 1; i++)
        {
            dataGridView1.Rows.Add();
            dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Value = checkedListBox1.SelectedItem.ToString();
          }
查看更多
登录 后发表回答