0

Im using a SQL database to store data. I have a datagrid where i show the data from the database. The problem is, when a user select a row in the datagrid, and click on my "Delete" button, i want to get the value for that cell under my column "ContactID" for that row.

I hope someone can help.

H.B.
  • 166,899
  • 29
  • 327
  • 400
Thunder
  • 117
  • 18

1 Answers1

0
private void dgvProducts_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (dgvProducts.SelectedCells.Count > 0) // Checking to see if any cell is selected
    {
        int mSelectedRowIndex = dgvProducts.SelectedCells[0].RowIndex;

        DataGridViewRow mSelectedRow = dgvProducts.Rows[mSelectedRowIndex];

        string mProductName = Convert.ToString(mSelectedRow.Cells[1].Value); //put your columb index where the 1 is 

        dgvProducts.CurrentCell.Value = null; // removes value from current cell

        SomeOtherMethod(mProductName); // Passing the name to where ever you need it

        UpdateDataBaseMethod(dgvProducts); // You can do that bit
    }
}

Then to put that value somewhere else you could do..

 private void SomeOtherMethod(string pProductName)
 {
    dgvProducts.Rows[dgvProducts.CurrentRow.Index].Cells["ContactID"].Value = pProductName; // where do you want it to go? Change the Column index to change the Column on the same row
 }

hope this helped, although you may want to move some of it about if you want to attach it to your delete button.

ravenx30
  • 406
  • 1
  • 6
  • 11