-
April 20th, 2010, 02:32 PM
#1
DataGridView
I have a datagridview on a form. the datagridview has multiple rows and columns. When i select a row, for example 3rd row, i want to get the value of specified column, for example 4th. I mean when i choose 3rd row, i want to get the value of the cell "3rd Row X 4th Column". How can i manage this.
-
April 20th, 2010, 03:57 PM
#2
Re: DataGridView
There are a number of events you could use. I normally use the CellClick event.
Code:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
string value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
}
That will give you the value of the cell that was clicked.
If you to get the 4th column of the 3rd row, you could do this...
Code:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
string value = dataGridView1.Rows[2].Cells[3].Value.ToString();
}
And last, if you want to get the data from the 4th column of the row that was selected..
Code:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
string value = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
}
Remember that the columns and rows are both 0-indexed. So a row with an index of 0 is actually the first row.
===============================
My Blog
-
April 20th, 2010, 04:25 PM
#3
Re: DataGridView
thank you very much for your detailed explanation..
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|