Click here to Skip to main content
11,410,405 members (43,422 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i Want to get DataGridView selected Cell value on click event i use this code its generates a error

DataGridViewRow dgvrows = TrGrid.SelectedRows;
string c = dgvrows.Cells("Late_Time").value.toString();
Posted 4-May-11 3:11am
Edited 4-May-11 3:20am
Tarun.K.S42.1K
v2
Comments
Tarun.K.S at 4-May-11 8:20am
   
And the error is?
Ruaj at 2-Aug-13 3:22am
   
i want to delete and update my record database on datagrid by cellclick in each record how could i do?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The following piece of code will do the trick for you:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
    {
       MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
    }
}

Hope this will help you out.
  Permalink  
Comments
paveeyavee at 14-Jul-12 6:12am
   
Thanx it was helpful..........
BlackMilan at 26-Oct-12 9:44am
   
... yes, until 1st click on row header or column header. ;-)
spencepk at 24-May-13 9:26am
   
Helped me too. Thanks Umair!
Member 10150435 at 10-Jun-14 7:23am
   
Thanks its working
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

SelectedRows will return a collection of rows of type DataGridViewSelectedRowCollection and not DataGridViewRow.

So you can use it like this :

DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows;
string val =(string)rows[2].Cells["Late_Time"].Value; //I have specified rowIndex as 2 as an example
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

DataGridView has a CurrentCell property[^]. I think that is what you mean by 'selected cell'. If you want to look at all the selected cells, it has a SelectedCells property which is enumerable to give cell objects.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

You may have many troubles:
* cell value may be null: test value for null before using ToString()
* column name may be erronous (impossible in your code, I think)...
and after click:
* row index may be -1 eg. when u click column header, not cell: test RowIndex not to be -1 and to be between 0 and row count
* cell index may be -1 eg. when u click row header, not cell: test ColumnIndex as above
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Man u need the First Row selected, u can use the index :

dgvrows[0].Cells("Late_Time").value.toString();
  Permalink  
v2
Comments
saravana kumar.N at 15-Aug-12 7:26am
   
how to set a value to cell in java?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you set some command argument then,-

int RowIndex = Convert.ToInt32((e.CommandArgument).ToString());
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

you can use this solution:

DataGridViewCell cell = KdgvAsset.SelectedCells[0] as DataGridViewCell;
string value = cell.Value.ToString();
Smile | :)
  Permalink  
v2
Comments
Member 10676130 at 27-Mar-14 2:48am
   
Hello,I am Vanen,from Mauritius Island,new to c# and vs.

I am getting difficulties while saving values from my datagrid to database.
If possible,option for selecting all the rows and option for selecting which rows to save to database from the datagrid.

Can someone guide me?please!
Thanks in advance.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 10

Hello,I am Vanen,from Mauritius Island,new to c# and vs.

I am getting difficulties while saving values from my datagrid to database.
If possible,option for selecting all the rows and option for selecting which rows to save to database from the datagrid.

Can someone guide me?please!
Thanks in advance.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 11

Private Sub DataGridView_CellMouseClick1(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView.CellMouseClick
    Dim val As String = DataGridView.CurrentCell.Value.ToString()
 
        MsgBox(val)
 

End Sub
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 129
1 Sascha Lefévre 95
2 Maciej Los 68
3 BillWoodruff 60
4 OriginalGriff 60
0 Sergey Alexandrovich Kryukov 8,755
1 OriginalGriff 6,915
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web03 | 2.8.150414.5 | Last Updated 28 Jul 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100