Click here to Skip to main content
12,944,769 members (50,204 online)
Rate this:
Please Sign up or sign in to vote.
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 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 14-Dec-12 1:07am
yeah databound to my database

i used <%# DataBinder.Eval (Container.DataItem, "Name") %>
Krunal Rohit 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 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 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 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 14-Dec-12 1:48am
Solution been updated...
sreeCoderMan 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
Top Experts
Last 24hrsThis month
OriginalGriff 4,651
CHill60 3,055
Maciej Los 2,423
Jochen Arndt 1,910
ppolymorphe 1,765

Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2017
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