2017-07-29 62 views
0

如何防止新输入条形码是否已存在于datagridview中,如果存在则添加数量或总和。如何检查datagridview中是否存在项目

+2

请提供与您所查询的DataGridView相关的代码,以及用于检查条件的数据。你似乎在一段时间左右,你现在应该知道这一点! –

回答

0

我得到的回答了我的问题搜索

Boolean found = false; 

     if (!string.IsNullOrWhiteSpace(this.textBox1.Text)) 
     { 

      if (e.KeyCode == Keys.Enter) 
      { 
       string conbarcode = this.textBox1.Text; 

       conbarcode = this.textBox1.TextLength == 10 ? this.textBox1.Text : Convert.ToDouble(this.textBox1.Text).ToString("0000000000").ToString(); 

       foreach (DataGridViewRow row in this.dataGridView1.Rows) 
       { 
        if (row.Cells[0].Value.Equals(conbarcode)) 
        { 
         // row exists 
         found = true; 

         row.Cells["qty"].Value = Convert.ToInt32(row.Cells["qty"].Value) + 1; 
         row.Cells["qty"].Selected = true; 
         //MessageBox.Show("Row already exists"); 
         break; 
        } 
       } 

       if (found) 
       { 
        this.textBox1.BackColor = Color.LightGreen; 
        return; 
       } 
1

把条形码柱像datakey,如果存在的价值使用Datagrid.Rows.Find([条码值]),当你想添加新行

相关问题