Reputation: 2458
How to prevent if the new enter barcode already in the datagridview and if is exist add the quantity or sum.
Upvotes: 0
Views: 8597
Reputation: 129
Try This Tested Code Call dataGridview_CellEndEdit Event :
private void GvOpStock_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex != 0)
for (int row = 0; row < GvOpStock.Rows.Count - 1; row++)
{
if (GvOpStock.Rows[row].Cells[1].Value != null &&
row != e.RowIndex &&
GvOpStock.Rows[row].Cells[1].Value.Equals(GvOpStock.Rows[e.RowIndex].Cells[e.ColumnIndex].Value))
{
MessageBox.Show("Duplicate");
return;
}
else
{
//Add To datagridview
}
}
}
catch (Exception ex)
{
}
}
Upvotes: 1
Reputation: 2458
I got an answer to my question
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;
}
Upvotes: 0
Reputation: 41
Put the barcode column like datakey and search if the value exists using Datagrid.Rows.Find ([barcode value]) when you want add new row
Upvotes: 1