[c#] Datagridview full row selection but get single cell value

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.

This question is related to c# winforms datagridview

The answer is


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


Simplest code is DataGridView1.SelectedCells(column_index).Value

As an example, for the first selected cell:

DataGridView1.SelectedCells(0).Value

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!");
      }
     }

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()

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.


for vb.net 2013 i use

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

where i is the cell number


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


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



string value = dataGridVeiw1.CurrentRow.Cells[1].Value.ToString();

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();

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();
    }
}

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();

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

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


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to winforms

How to set combobox default value? Get the cell value of a GridView row Getting the first and last day of a month, using a given DateTime object Check if a record exists in the database Delete a row in DataGridView Control in VB.NET How to make picturebox transparent? Set default format of datetimepicker as dd-MM-yyyy Changing datagridview cell color based on condition C# Inserting Data from a form into an access Database How to use ConfigurationManager

Examples related to datagridview

How to refresh or show immediately in datagridview after inserting? Delete a row in DataGridView Control in VB.NET Looping each row in datagridview How to get cell value from DataGridView in VB.Net? Changing datagridview cell color based on condition Index was out of range. Must be non-negative and less than the size of the collection parameter name:index how to bind datatable to datagridview in c# DataGridView AutoFit and Fill How to export dataGridView data Instantly to Excel on button click? Populate a datagridview with sql query results