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

        }

转载于:https://www.cnblogs.com/hutie1980/p/5980288.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值