Datagridview full row selection but get single cell value

后端 未结 16 950
傲寒
傲寒 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:50

    dataGridView1.SelectedRows[0].Cells[0].Value;

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

    You can do like this:

    private void datagridview1_SelectionChanged(object sender, EventArgs e)
    {
      if (datagridview1.SelectedCells.Count > 0)
      {
        int selectedrowindex = datagridview1.SelectedCells[0].RowIndex;
        DataGridViewRow selectedRow = datagridview1.Rows[selectedrowindex];  
        string a = Convert.ToString(selectedRow.Cells["enter column name"].Value);           
      }
    }
    
    0 讨论(0)
  • 2020-12-02 12:51

    this get me the text value of exact cell in data grid view

    private void dataGridView_SelectionChanged(object sender, EventArgs e)
        {
            label1.Text = dataGridView.CurrentRow.Cells[#].Value.ToString();
        }
    

    you can see it in label and change # wih the index of the exact cell you want

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

    Just Use: dataGridView1.CurrentCell.Value.ToString()

            private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
            }
    

    or

     // dataGrid1.Rows[yourRowIndex ].Cells[yourColumnIndex].Value.ToString()
    
     //Example1:yourRowIndex=dataGridView1.CurrentRow.Index (from selectedRow );
     dataGrid1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString()
    
     //Example2:yourRowIndex=3,yourColumnIndex=2 (select by programmatically )
      dataGrid1.Rows[3].Cells[2].Value.ToString()
    
    0 讨论(0)
  • 2020-12-02 12:55

    DataGridView.CurrentRow.Cells[n]

    See: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentrow.aspx

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

    I just want to point out, you can use .selectedindex to make it a little cleaner

    string value = gridview.Rows[gridview.SelectedIndex].Cells[1].Text.ToString();
    
    0 讨论(0)
提交回复
热议问题