Datagridview full row selection but get single cell value

后端 未结 16 949
傲寒
傲寒 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 12:40

    I know, I'm a little late for the answer. But I would like to contribute.

    DataGridView.SelectedRows[0].Cells[0].Value
    

    This code is simple as piece of cake

    0 讨论(0)
  • 2020-12-02 12:40

    you can get the values of the cell also as the current selection is referenced under CurrentRow

    dataGridView1.CurrentRow.Cell[indexorname].FormattedValue
    

    Here you can use index or column name and get the value.

    0 讨论(0)
  • 2020-12-02 12:41

    for vb.net 2013 i use

    DataGridView1.SelectedRows.Item(0).Cells(i).Value
    

    where i is the cell number

    0 讨论(0)
  • 2020-12-02 12:46

    For those who could not fire the click event, they may use following code

    public Form1()
                {
                    InitializeComponent();
                    this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
                }
    
    0 讨论(0)
  • 2020-12-02 12:49

    If you want to get the contents of selected cell; you need the index of row and cell.

    int rowindex = dataGridView1.CurrentCell.RowIndex;
    int columnindex = dataGridView1.CurrentCell.ColumnIndex; 
    
    dataGridView1.Rows[rowindex].Cells[columnindex].Value.ToString();
    
    0 讨论(0)
  • 2020-12-02 12:49

    To get one cell value based on entire row selection:

    if (dataGridView1.SelectedRows.Count > 0)
          {
             foreach (DataGridViewRow row in dataGridView1.Rows)
               {
                   TextBox1.Text = row.Cells["ColumnName"].Value.ToString();
               }
          }
    else
          {
            MessageBox.Show("Please select item!");
          }
         }
    
    0 讨论(0)
提交回复
热议问题