Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a datagridview in which it has 2 columns 'Name' and 'ID' like below.

**Name--ID**
John--2454
Sammy--2343
Sarah--3456

Now , i want that when i select any name from 'Name' column i should get name corresponding ID. for example if i click on 'John' i should get '2454' in variable.

So how its Possible in C#.??
Note: My datagriview is populated from Arraylist.
Posted
Comments
Arasappan 7-Aug-15 0:40am    
show ur gridview structure
i have shown gridstructure in my question.
Arasappan 7-Aug-15 0:43am    
thats ur output structure i want design struct

use DataGridView.CellClick Event[^] and get the row using dataGridView1.Rows[e.RowIndex] and also you can get other cell values in the same row using cell index, for example
C#
dataGridView1.Rows[e.RowIndex].Cells[1]
 
Share this answer
 
Comments
The code u gave me returns Column number and Row index. I want that if i click on any cell i get value of cell next to it . such as if click on John i should get its corresponding value from next cell .
DamithSL 7-Aug-15 1:19am    
if you know the current cell index, can't you calculate next cell index? :)
No i am not able to calculate next cell index . please help.
DamithSL 7-Aug-15 1:26am    
if cell is not the last cell of the row

dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex+1]
Hello ,
First use DatGridview.CellClick Event that already described by DamithSL and then check the ColumnIndex of the clicked cell by this way .
var columnindex= dataGridView.CurrentCell.ColumnIndex;
//Now add 1 for get next  column index
var nextcolumnindex=columnindex+1 ;

//and ultimately get the value of next column by this way
var nextcolumnvalue =dataGridView.Rows[e.RowIndex].Cells[nextcolumnindex].Value;



thanks
 
Share this answer
 
v2
Comments
Your code is just returning Column and Row index , i want data stored in them.
Thanks your code has worked , i am so much thankful to u. I modified it just a bit and it performed well.
Animesh Datta 7-Aug-15 2:01am    
glad to help you .. :)

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