Datagridview full row selection but get single cell value
Asked Answered
A

19

54

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 entire row.

Albertina answered 5/10, 2011 at 5:42 Comment(1)
possible duplicate of How do I get the selected row data from a data grid view using SelectedRows?Douglass
T
114

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 cellValue = Convert.ToString(selectedRow.Cells["enter column name"].Value);           
  }
}
Thetis answered 5/10, 2011 at 9:58 Comment(2)
@pratap K , Does DataGridView1_SelectionChanged event will fire at the time of form load ? but it is happening in my case. Please help me resolve this. If needed more details I will post it as a question.Scrope
I wanted the opposite behavior and your posted helped me realize what I needed to do. SelectedCells.Count == 1 :)Mantelet
C
25

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();
Clench answered 12/10, 2012 at 17:31 Comment(0)
R
10

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

Remscheid answered 22/5, 2018 at 20:30 Comment(1)
Only 6 and a half years late!Crichton
A
8

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

Axle answered 5/10, 2011 at 5:51 Comment(1)
This is more better answer, just need to change e.ColumnIndex with specific string column name or index since question ansked for it.Berth
K
5

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.

Kolk answered 5/10, 2011 at 5:53 Comment(0)
V
2

DataGridView.CurrentRow.Cells[n]

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

Verine answered 5/10, 2011 at 5:51 Comment(0)
D
2

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();
Darmstadt answered 2/1, 2014 at 3:3 Comment(0)
F
2
string value = dataGridVeiw1.CurrentRow.Cells[1].Value.ToString();
Fornix answered 10/9, 2016 at 13:19 Comment(0)
G
1

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()
Gruelling answered 7/7, 2015 at 7:45 Comment(0)
B
1

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

Bendick answered 29/5, 2017 at 23:45 Comment(0)
D
0

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();
    }
}
Delamination answered 30/6, 2014 at 11:29 Comment(0)
A
0

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);
            }
Alroi answered 10/12, 2014 at 10:10 Comment(0)
E
0

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!");
      }
     }
Elkeelkhound answered 15/5, 2016 at 10:44 Comment(0)
L
0

Simplest code is DataGridView1.SelectedCells(column_index).Value

As an example, for the first selected cell:

DataGridView1.SelectedCells(0).Value
Lickspittle answered 30/4, 2017 at 18:28 Comment(0)
P
0

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

Prothonotary answered 6/5, 2020 at 23:26 Comment(0)
G
0

for vb.net 2013 i use

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

where i is the cell number

Goodish answered 25/11, 2020 at 22:26 Comment(0)
D
0

Assigns cell 1 of dgwProduct lines to ProductId.

private void btnUpdate_Click(object sender, EventArgs e)
    {
            ProductId = Convert.ToInt32(dgwProduct.CurrentRow.Cells[0].Value);
    }

Here it puts all the cells in the row into the textbox.

private void dgwProduct_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        var row=dgwProduct.CurrentRow;
        tbxProductName.Text =row.Cells[1].Value.ToString();
        tbxUnitPrice.Text = row.Cells[2].Value.ToString();
        tbxQuantityPerUnit.Text = row.Cells[3].Value.ToString();
        tbxStockUpdate.Text = row.Cells[4].Value.ToString();
    }

In this way, we assign all the cells in the dataGridView to the textboxes.

Diseased answered 4/10, 2021 at 11:40 Comment(1)
You can also check here: msdn.microsoft.com/en-us/library/…Diseased
C
0
 private void DataGridView_SelectionChanged(object sender, EventArgs e)
    {
        if (dataGridViewX3.SelectedCells.Count > 0)
        {
            int selectedrowindex = dataGridViewX3.SelectedCells[0].RowIndex;
            DataGridViewRow selectedRow = dataGridViewX3.Rows[selectedrowindex];
            string cellValue = Convert.ToString(selectedRow.Cells[0].Value);
            Console.WriteLine(cellValue);
        }
    }
Cystotomy answered 13/6, 2022 at 8:10 Comment(1)
when select a row get value of datagridview and show.Cystotomy
B
0

To get a selected row and cell value in DataGridView:

foreach (DataGridViewRow row in this.dataGridView.Rows)
{
    if (row.Selected == true)
    {  
         foreach (DataGridView cell in rows.Cells)
         {
             string temp = cell.Value.ToString();
             // Do something with string value
         }
    }
}
Blear answered 14/4, 2024 at 10:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.