简体   繁体   English

C# 从checkedlistbox 中插入自定义值选定项到dataviewgrid

[英]C# insert custom value selected item from checkedlistbox to dataviewgrid

Im working in a lab for covid.我在covid实验室工作。 We are sending result via excel.我们通过excel发送结果。 I made a template for easy edit excel file.我制作了一个模板,便于编辑excel文件。 I searched too much but i cant find solution for this request.我搜索了太多,但找不到此请求的解决方案。

https://i.stack.imgur.com/P9UWV.png

in this picture after selected items and push "tamamlandı" button, i want to change dataviewgrid second column change custom data在此图片中选择项目并按下“tamamlandı”按钮后,我想更改 dataviewgrid 第二列更改自定义数据

for example: if selected 1,5,13,48 in checkedlistbox, change 1,5,13,48 rows second column data to "POZ"例如:如果在checkedlistbox中选择1,5,13,​​48,将1,5,13,​​48行第二列数据更改为“POZ”

You can get all checked items via CheckedItems property.您可以通过CheckedItems属性获取所有选中的项目。 Then you can use the foreach statement to traverse it.然后就可以使用foreach语句遍历它了。

private void btntamamlandı_Click(object sender, EventArgs e)
{
    foreach(var i in checkedListBox1.CheckedItems)
    {
        // get the checked item value
        // rowindex starts from 0, so need to subtract 1
        int rowindex = Convert.ToInt32(i.ToString()) - 1;
        dataGridView1.Rows[rowindex].Cells[1].Value = "POZ";
    }
}

Update: delete selected rows更新:删除选定的行

private void btnDelete_Click(object sender, EventArgs e)
{
    // store checkedListBox1 selected index
    List<int> indexs = new List<int>();

    foreach (var i in checkedListBox1.CheckedItems)
    {
        indexs.Add(Convert.ToInt32(i.ToString()) - 1);
    }

    // selected row count
    int count = indexs.Count;

    for (int index = 0; index < count; index++)
    {
        dataGridView1.Rows.RemoveAt(indexs[0] - index);
        indexs.RemoveAt(0); // remove the first item
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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