简体   繁体   中英

How do I add checked item(s) in my checkedlistbox to a DataGridView?

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设计

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

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");
        }
    }
}
  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();
          }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM