Click here to Skip to main content
Rate this: bad
good
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 19:56pm
Comments
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
good
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.
 
Courtesy http://stackoverflow.com/questions/121722/getting-value-from-a-cell-from-a-gridview-on-rowdatabound-event[^]
  Permalink  
Comments
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
good
Please Sign up or sign in to vote.

Solution 2

http://www.vkinfotek.com/gridview/gridview-selectedindexchanged-event.html[^]
 
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;
            }
        }
    }
  Permalink  
v2
Comments
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 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2014
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