簡體   English   中英

從datagrid選擇行時獲取特定的單元格

[英]Get a specific cell when selecting a row from datagrid

我正在使用SQL數據庫來存儲數據。 我有一個datagrid,用於顯示數據庫中的數據。 問題是,當用戶在數據網格中選擇一行,然后單擊我的“刪除”按鈕時,我想獲取該行在我的列“ ContactID”下的該單元格的值。

我希望有人能幫幫忙。

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
    }
}

然后將其值放在其他位置即可。

 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
 }

希望這對您有所幫助,盡管如果您想將其附加到刪除按鈕上,可能需要對其進行一些移動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM