0
votes

I have two forms Form1(DGV1) and Form2(DGV2) with Datagridview control on both the forms.

On Form1 there is a button link to Form2 where user can add rows to Form1 datagridview(DGV1) ..In Form2 Datagridview there is a checkbox in the first column and the user can select only one row at a time.There are two button on Form2 1) Done 2) Add Items

When user clicks on Add Items by selecting the rows one after another,the rows must be added to the datagridview on Form1 and when Done button is clicked Form 1 should be displayed with all the rows that are added.

I have achieved this using the below code:

int inde x= objQM.gvItemDetails.Rows.Add(); 
DataGridViewRow row = (DataGridViewRow)objQM.gvItemDetails.Rows[index]; 
decimal UnitCos = Convert.ToDecimal(gvInventory.Rows[RowIndex].Cells[8].Value.ToString()); row[0] = false; 
row[1] = 1;
row[2] = gvInventory.Rows[RowIndex].Cells[6].Value.ToString();
row[3] = LoadSellQty();
row[4] = Convert.ToDecimal(gvInventory.Rows[RowIndex].Cells[8].Value.ToString());
row[5] = LoadSellQty() * UnitCost; 
row[7] = Convert.ToInt32(gvInventory.Rows[RowIndex].Cells[1].Value.ToString());

Now the problem is to validate if the same row is added into DGV1 on Form1 when AddItems button is clicked on Form2.I mean no duplicate rows must be added.

3

3 Answers

2
votes

I believe you have a unique name from your DataGridView rows, you can achieve this by using linq

bool IsExist = dataGridView1.Rows.Cast<DataGridViewRow>()
                                   .Count(c => !string.IsNullOrWhiteSpace(c.Cells[colIndex].EditedFormattedValue.ToString()) &&
                                   c.Cells[colIndex].EditedFormattedValue.ToString().Trim() == dgv[colIndex, rowIndex].EditedFormattedValue.ToString()) > 1;

if (IsExist)
{
   //do stuff
}

UPDATE base on ItemID

bool IsExist = dataGridView1.Rows.Cast<DataGridViewRow>()
                      .Count(c => 
                      c.Cells[colIndexOfItemId]
                      .EditedFormattedValue.ToString() == ItemID) > 1; //or (int)c.Cells[colIndexOfItemId].Value
0
votes

I would try two different way as follow and btw I prefer solution #2:

  1. Simply write a loop myself if the row count is small, just check value one by one, which makes the code looks not that good.

  2. If your are using DataSet or DataTable, try add a UniqueConstraint on to the DataTable which actually contains all columns, so if duplicate row been added it will throw exception and you know it. For more detail check here.

0
votes
 private void dgViewOrderList_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && dgViewOrderList.CurrentCell.Value != null)
            {
                var cellValue = dgViewOrderList.CurrentCell.Value.ToString();
                var isExist = dgViewOrderList.Rows.Cast<DataGridViewRow>().Count(c =>c.Cells[0].EditedFormattedValue.ToString() == cellValue) > 1;
                if (isExist)
                {
                    dgViewOrderList.CurrentCell.Value = null;
                }
            }
        }
    }