Click here to Skip to main content
11,796,275 members (76,539 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++ C C# ASP ASP.NET HTML Ajax
in gridview i have listed many names from database
while selecting a row i need the names to be displayed in label how to do?...

please help me

i tried
GridViewRow row = CustomersGridView.SelectedRow;

MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";

its not working while using break point
in .Text is blank
Posted 13-Dec-12 18:56pm
Kiran Susarla at 14-Dec-12 1:02am
Are the columns databound to some database fields? how did you populate the grid? Can you please show your code here?
sreeCoderMan at 14-Dec-12 1:07am
yeah databound to my database

i used <%# DataBinder.Eval (Container.DataItem, "Name") %>
Krunal Rohit at 14-Dec-12 1:38am
If I'm not mistaken, then

GridViewRow row = CustomersGridView.SelectedRow;
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
is from MSDN...
sreeCoderMan at 14-Dec-12 1:48am
i tried this but its not working in my databound gridview
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

First you need to wrap your code in a Label or Literal control so that you can reference it properly. What's happening is that there's no way for the system to keep track of it, because there's no control associated with the text. It's the control's responsibility to add its contents to viewstate.

You need to use gridView.FindControl("controlName"); to get the control in the row. From there you can get at its properties including Text.

You can also get at the DataItem property of the Row in question and cast it to the appropriate type and extract the information directly.

sreeCoderMan at 14-Dec-12 1:35am
i couldnt able to figured it out can u please show me an example or demo
Rate this: bad
Please Sign up or sign in to vote.

Solution 2[^]

protected void GridView1_DataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            if (e.Row.Cells[0].Text.Contains("bla bla bla"))
                e.Row.Cells[0].Forecolor = System.Drawing.Color.Aqua;
sreeCoderMan at 14-Dec-12 1:44am
it works for simple gridview... but here its databound ... can u please show me a demo code or demo example
Krunal Rohit at 14-Dec-12 1:48am
Solution been updated...
sreeCoderMan at 14-Dec-12 1:54am
let me try this

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

  Print Answers RSS
0 Maciej Los 460
1 Abhinav S 368
2 OriginalGriff 340
3 CPallini 269
4 Sergey Alexandrovich Kryukov 252
0 OriginalGriff 2,012
1 Maciej Los 1,615
2 KrunalRohit 1,310
3 CPallini 1,015
4 Richard MacCutchan 828

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 14 Dec 2012
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