Datagridview full row selection but get single cell value

后端 未结 16 951
傲寒
傲寒 2020-12-02 12:12

I have a datagridview that is a full row select. How would I grab the data from only a certain cell no matter what cell in the row was clicked on since it highlights the ent

相关标签:
16条回答
  • 2020-12-02 13:02
    string value = dataGridVeiw1.CurrentRow.Cells[1].Value.ToString();
    
    0 讨论(0)
  • 2020-12-02 13:05

    Simplest code is DataGridView1.SelectedCells(column_index).Value

    As an example, for the first selected cell:

    DataGridView1.SelectedCells(0).Value
    
    0 讨论(0)
  • 2020-12-02 13:06

    In the CellClick event you can write following code

    string value =
          datagridviewID.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString();
    

    Using the bove code you will get value of the cell you cliked. If you want to get value of paricular column in the clicked row, just replace e.ColumnIndex with the column index you want

    0 讨论(0)
  • 2020-12-02 13:06

    Use Cell Click as other methods mentioned will fire upon data binding, not useful if you want the selected value, then the form to close.

    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 mCatagoryName = Convert.ToString(mSelectedRow.Cells[1].Value);
    
            SomeOtherMethod(mProductName); // Passing the name to where ever you need it
    
            this.close();
        }
    }
    
    0 讨论(0)
提交回复
热议问题