Answered by:
How to get the selected row value in datagridview control?

Question
-
Hi all,
i have a datagridview control
i am displaying all the students information in datagrid view based on their class.
in database i have a student table it contains no.of columns like their sno,sname,age,marks etc but iam showing only sno and sname in datagridview control
sno sname
1 xyz
2 abc
however every thing is fine i have done the above task.
now i want do the following task
when the user clicks on the number in the datagridview i want show all the information of the student in a window
for example when the user clicks on "1" on the datagridview
i want to display all the details of the number 1 student in a new window or in messagebox.
for this purpose where should i write the code and how can i get the user selected number on the datagridview.
thanks,
murali.
programmer on .net
Friday, August 3, 2012 4:08 PM
Answers
-
private void gridview_CellClick(object sender, DataGridViewCellEventArgs e) { if (gridview.SelectedCells.Count > 0) { string id = gridview.SelectedCells[0].Value.ToString(); } }
Web Developer
- Marked as answer by programmer on .net Friday, August 3, 2012 5:05 PM
Friday, August 3, 2012 4:53 PM
All replies
-
Is this a web app? If so, just use OnRowCommand, and in this method you can do something like this:
string argument = Page.Request.Params.Get("__EVENTARGUMENT"); if (argument.StartsWith("Select$")) { int id = Convert.ToInt32(argument.Remove(0, 7)); int columm1 = Convert.ToInt32(gridview.Rows[id].Cells[0].Text); string columm2 = Convert.ToInt32(gridview.Rows[id].Cells[1].Text); }
Web Developer
Friday, August 3, 2012 4:16 PM -
Hi Norkk,
thanks for your reply
it is not a web app
it is a windows application and iam using c#.net 2010
programmer on .net
Friday, August 3, 2012 4:17 PM -
Hmm, so try this
private void gridView_SelectionChanged(object sender, EventArgs e) { if (gridView.SelectedRows.Count > 0) { int id = Convert.ToInt32(gridView.SelectedRows[0].Cells["ColummID"].Value.ToString()); string name = gridView.SelectedRows[0].Cells["ColummName"].Value.ToString(); } }
Web Developer
- Edited by Norkk Friday, August 3, 2012 4:23 PM
Friday, August 3, 2012 4:23 PM -
Hi Norkk,
i tried above code it shows the following error
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: indexand iam using datagridview control not gridview control
programmer on .net
Friday, August 3, 2012 4:35 PM -
Yes, this code is for datagridview control.
gridView.SelectedRows[0].Cells["ColummName"].Value.ToString();
In this line the index 0 should be FIX. The only change you need to make is the name of your columm. If do you have a columm called Age, and a columm called Name, you should do this:
private void gridView_SelectionChanged(object sender, EventArgs e) { if (gridView.SelectedRows.Count > 0) { int age = Convert.ToInt32(gridView.SelectedRows[0].Cells["Age"].Value.ToString()); string name = gridView.SelectedRows[0].Cells["Name"].Value.ToString(); } }
Web Developer
- Edited by Norkk Friday, August 3, 2012 4:40 PM
- Proposed as answer by Kapul Bhatnagar Friday, August 3, 2012 4:58 PM
Friday, August 3, 2012 4:40 PM -
Thanks Norkk
its working fine
but the problem is if i want to see the full data then evrytime i need to selecting the full row.
then there is any chance,if we select any cell in the row then
i want to show the information .
programmer on .net
Friday, August 3, 2012 4:47 PM -
private void gridview_CellClick(object sender, DataGridViewCellEventArgs e) { if (gridview.SelectedCells.Count > 0) { string id = gridview.SelectedCells[0].Value.ToString(); } }
Web Developer
- Marked as answer by programmer on .net Friday, August 3, 2012 5:05 PM
Friday, August 3, 2012 4:53 PM -
Thanks Norkk,
for sloving this issue.
now all things are working fine.
programmer on .net
Friday, August 3, 2012 5:05 PM -
Hi Norkk,
my datagridview has looks like
sno sname age
1 xxxxx 20
2 yyyyy 22
3 zzzzz 23
now when the user clicks any of the cell in a row i want to get his sno
for example when a user clicks on the "yyyyy" name i want to get his sno "2"
how to do this issue.?
programmer on .net
Saturday, August 4, 2012 12:36 PM -
Use code similar to what Norkk posted previously, but change it as follows:
private void gridview_CellClick(object sender, DataGridViewCellEventArgs e) { if (gridview.SelectedCells.Count > 0) { int i = gridview.SelectedCells[0].RowIndex; string sno = gridview.Rows[i].Cells[0].ValueToString(); } }
You should easily be able to discover this yourself with a little playing around with Intellisense.~~Bonnie Berent DeWitt [C# MVP]
geek-goddess-bonnie.blogspot.comSunday, August 5, 2012 11:26 PM -
If you are using a LINK BUTTON in your grid view, you can use the following code in the ROWCOMMAND method... This code with retrieve all the values in the particular selected row.
// to get the value of the link use the command argumentFaultId = Convert.ToInt32(e.CommandArgument);
// to get the other column values
UserId = Convert.ToInt32(((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[1].Text);Department = ((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[2].Text;
ProblemType = ((GridViewRow)(((LinkButton)e.CommandSource).NamingContainer)).Cells[3].Text;
- Edited by Suganya Sugumar Saturday, November 17, 2012 9:33 AM
Saturday, November 17, 2012 9:32 AM