Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
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") %>
[no name] 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

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[^]
 
Share this answer
 
Comments
sreeCoderMan 14-Dec-12 1:35am    
i couldnt able to figured it out can u please show me an example or demo
http://www.vkinfotek.com/gridview/gridview-selectedindexchanged-event.html[^]

C#
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;
            }
        }
    }
 
Share this answer
 
v2
Comments
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
[no name] 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900