20161020001 DataGridView 选中的 DataGridViewCheckBoxCell 不添加重复项

Posted 我的梦会回来

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了20161020001 DataGridView 选中的 DataGridViewCheckBoxCell 不添加重复项相关的知识,希望对你有一定的参考价值。

private void btn_add_Click(object sender, EventArgs e)
        {
            string str_P_ID = "";
            string str_P_Type = "";
            string str_P_Name = "";

            int count = Convert.ToInt32(Dgv_Search.Rows.Count.ToString());
            for (int i = 0; i < count; i++)
            {
                //如果DataGridView是可编辑的,将数据提交,否则处于编辑状态的行无法取到
                Dgv_Search.EndEdit();

                DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)Dgv_Search.Rows[i].Cells["ckb_check"];
                Boolean flag = Convert.ToBoolean(checkCell.Value);
                if (flag == true)     //查找被选择的数据行
                {
                    //从 DATAGRIDVIEW 中获取数据项
                    str_P_Type = Dgv_Search.Rows[i].Cells["类型"].Value.ToString().Trim();
                    str_P_ID = Dgv_Search.Rows[i].Cells["ID"].Value.ToString().Trim();
                    str_P_Name = Dgv_Search.Rows[i].Cells["名称"].Value.ToString().Trim();

                    string str_Same = "0";
                    for (int i2 = 0; i2 < DataSet_tb.Tables[0].Rows.Count; i2++)
                    {
                        if (str_P_ID == DataSet_tb.Tables[0].Rows[i2]["ID"].ToString())
                        {
                            str_Same = "1";
                            continue;
                        }
                    }

                    if (str_Same != "1")
                    {
                        DataRow row = DataSet_tb.Tables[0].NewRow();
                        row["类型"] = str_P_Type.ToString();
                        row["ID"] = str_P_ID.ToString();
                        row["名称"] = str_P_Name.ToString();
                        DataSet_tb.Tables[0].Rows.Add(row);
                    }
                }
            }

            Dgv_Confirm.DataSource = DataSet_tb.Tables[0];
            Dgv_Confirm_Init();

        }

以上是关于20161020001 DataGridView 选中的 DataGridViewCheckBoxCell 不添加重复项的主要内容,如果未能解决你的问题,请参考以下文章

c#设置datagridview行标题

VB.net datagridview 折叠展开

怎么插入datagridview的行

datagridview怎么把一行数据复制到另一个datagridview [C# VS2005]

c#怎么把datagridview给填满

C#datagridview怎么设置行标题的文字?