Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
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
Updated 10-May-20 22:27pm
v2
Comments
Tarun.K.S 4-May-11 8:20am    
And the error is?
Ruaj 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?

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.
 
Share this answer
 
The following piece of code will do the trick for you:
C#
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.
 
Share this answer
 
Comments
paveeyavee 14-Jul-12 6:12am    
Thanx it was helpful..........
BlackMilan 26-Oct-12 9:44am    
... yes, until 1st click on row header or column header. ;-)
spencepk 24-May-13 9:26am    
Helped me too. Thanks Umair!
Member 10150435 10-Jun-14 7:23am    
Thanks its working
SelectedRows will return a collection of rows of type DataGridViewSelectedRowCollection and not DataGridViewRow.

So you can use it like this :

C#
DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows;
string val =(string)rows[2].Cells["Late_Time"].Value; //I have specified rowIndex as 2 as an example
 
Share this answer
 
Comments
Member 12700993 1-Apr-21 5:52am    
SelectedRows accepting argument showing error
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
 
Share this answer
 
Man u need the First Row selected, u can use the index :

C#
dgvrows[0].Cells("Late_Time").value.toString();
 
Share this answer
 
v2
Comments
saravana kumar.N 15-Aug-12 7:26am    
how to set a value to cell in java?
VB
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
 
Share this answer
 
Comments
Member 11791999 27-Nov-15 4:00am    
comment
krwlngx 31-Jul-21 10:30am    
Thank you so much, it's work for vb net
If you set some command argument then,-

int RowIndex = Convert.ToInt32((e.CommandArgument).ToString());
 
Share this answer
 
you can use this solution:

C#
DataGridViewCell cell = KdgvAsset.SelectedCells[0] as DataGridViewCell;
string value = cell.Value.ToString();

:)
 
Share this answer
 
v2
Comments
Member 10676130 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.
private void gv_packet_report_SelectionChanged(object sender, EventArgs e)
{
    try
    {
        sum += Convert.ToDouble(gv_packet_report.CurrentCell.Value.ToString());
        label_sum.Text = sum.ToString();
    }
    catch (Exception)
    {

    }
}
 
Share this answer
 
Comments
CHill60 11-May-20 4:54am    
Not relevant to the original question and very poor code - never use try-catch unless you actually do something with any exceptions raised. In other words, you should never have an empty catch statement

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900