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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.