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

♀尐吖头ヾ 提交于 2020-01-05 09:31:23

问题


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:

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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/21450681/how-do-i-add-checked-items-in-my-checkedlistbox-to-a-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!