1
votes

I have 2 datagridview in WinForms dgv1 dgv2 dgv1 has some columns including 1 CheckBoxColumn ,dgv2 has Some columns but no CheckBoxColumn . I want functionality which enables me , when i check CheckBoxColumn ,data of that particular Row should be selected and go into Dgv2 . this is how i put selected rows from dgv1 to dgv2 . But if i cancel check (or unckeck) ,checked checkBox in dgv1 that particular row should be deleted from dgv2 . Help me to fix this . i'll be thankful of u guys ,looking forward to this. :)

here is my code consider datagridview6= dgv1 and datagridview10 = dgv2

private void dataGridView6_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    dataGridView10.Visible = true;           
         if (dataGridView6.CurrentCell.ColumnIndex == 7)
            {
                if (dataGridView6.CurrentCell.Value != null)
                {
                    bool checkstate = (bool)dataGridView6.CurrentCell.Value;

                    if (checkstate == false)
                    {
                        dataGridView6.CurrentCell.Value = true;                                           
                    }
                else
                 {//here help in logic to delete unchecked row
                    dataGridView6.CurrentCell.Value = false;
                     int j= int.Parse(dataGridView6.Rows[e.RowIndex].ToString());
                     dataGridView10.Rows.Remove(dataGridView10.Rows[j]);
                  }
               }
         else
         {dataGridView6.CurrentCell.Value = true;
           dataGridView10.Rows.Insert(i);          dataGridView10.Rows[i].Cells[1].Value=dataGridView6.CurrentRow.Cells[2].Value.ToString();
dataGridView10.Rows[i].Cells[2].Value = dataGridView6.CurrentRow.Cells[3].Value.ToString();
dataGridView10.Rows[i].Cells[3].Value = dataGridView6.CurrentRow.Cells[5].Value.ToString();
           }
     }

/* in this logic checked row is being selected in dgv2 but on unchecking it delete is not working.

both data grid are not bound with database , m fetching value using SQlCommand class .

2

2 Answers

1
votes

To doing this you need to store RowIndex of dgv2 in Checked Row's Tag property.

for example when you select any row from dgv1 at this time you are adding that records in dgv2 gridview. Now, you need to store that new row index of created row in dgv1.CurrentRow.

So, when the cell get unchecked you can find the added row from the dgv2 by using that row index.

///Inserting default records
///The fifth column value is a primary key value
///Application will check that value with second grid and performs remove and add rows
///we cannot use row index to remove row. because the row index will be changed if any row removed from dgv2.
///I have placed this code in form load event.
dgv1.Rows.Add(false, "Nimesh", "Gujarat", "India", 1);
dgv1.Rows.Add(false, "Prakash", "MP", "India", 2);
dgv1.Rows.Add(false, "Rohit", "Maharashtra", "India",  3);
dgv1.Rows.Add(false, "Jasbeer", "Panjab", "India",  4);
dgv1.Rows.Add(false, "Venkteshwar", "Karnatak", "India", 5);
dgv1.Rows.Add(false, "Rony", "Delhi", "India", 6);


///We cannot use CellChange event becauase it will be executed after cell end edit.
///We cannot use CellContentClick event coz it will not call when you double click or clicking quickly multiple times.
///So we are using CellMouseUp event 


private void dgv1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
    if (dgv1.CurrentCell == null)
        return;

    dgv2.Visible = true;
    if (dgv1.CurrentCell.ColumnIndex == 0)
    {
        bool bValue = (bool)dgv1.CurrentCell.GetEditedFormattedValue(e.RowIndex, DataGridViewDataErrorContexts.CurrentCellChange);
        if (bValue)
        {
            for (int iRow = 0; iRow < dgv2.Rows.Count; iRow++)
            {
                if (dgv2.Rows[iRow].Tag != null && dgv2.Rows[iRow].Tag.Equals(dgv1.CurrentRow.Cells[4].Value))
                    return;
            }
            int iNewRowIndex = dgv2.Rows.Add();
            dgv2.Rows[iNewRowIndex].Cells[0].Value = dgv1.CurrentRow.Cells[1].Value;
            dgv2.Rows[iNewRowIndex].Cells[1].Value = dgv1.CurrentRow.Cells[2].Value;
            dgv2.Rows[iNewRowIndex].Cells[2].Value = dgv1.CurrentRow.Cells[3].Value;
            dgv2.Rows[iNewRowIndex].Tag = dgv1.CurrentRow.Cells[4].Value;
        }
        else
        {
            if (dgv1.CurrentRow.Cells[4].Value != null)
            {
                for (int iRow = 0; iRow < dgv2.Rows.Count; iRow++)
                {
                    if (dgv2.Rows[iRow].Tag != null && dgv2.Rows[iRow].Tag.Equals(dgv1.CurrentRow.Cells[4].Value))
                    {
                        dgv2.Rows.RemoveAt(iRow);
                        break;
                    }
                }
            }
        }
    }
}
0
votes

Add hidden columns on the both DataGridView. When user check the checkboxcolumn assign some unique value to the hidden column. Copy all the cells of the row from the first DataGridView to the second. When user uncheck the check box, just find the row by the value of the hidden column. RemoveAt() the row.