Reputation: 73988
I need change the value for a Label inside a GridView programmatically.
I user this code but does not work. I am not able to Find the Control.
Thanks
protected void uxListAuthorsDisplayer_RowCreated(object sender, GridViewRowEventArgs e)
{
int myRowIndex = e.Row.RowIndex;
//Label myLabel = (Label)e.Row[myRowIndex].FindControl("uxUserIdDisplayer");
if (myRowIndex >= 0)
{
//e.Row.Cells[3].Text = "Ciao";
Label myLabel = (Label)e.Row.FindControl("uxUserIdDisplayer");
myLabel.Text = "TEST";
}
}
Upvotes: 0
Views: 6477
Reputation: 113
The GridView.RowCreated event is appropriate. Maybe you should check if it is not a header row for example, case when indeed there is no control to find:
if(e.Row.RowType == DataControlRowType.DataRow)
{
Label myLabel = (Label)e.Row.FindControl("uxUserIdDisplayer");
myLabel.Text = "TEST";
}
Upvotes: 1
Reputation: 5985
You're searching for a cell with index "RowIndex" in your row.
protected void uxListAuthorsDisplayer_RowCreated(object sender, GridViewRowEventArgs e) { // Skip the header and footer rows if (e.Row.RowType == DataControlRowType.DataRow) { Label myLabel = (Label) e.Row.FindControl("uxUserIdDisplayer"); } }
I prefer to use the RowDataBound event, since that one occurs when the data has been bound to the row, so that you can access the data via DataBinder.Eval(e.Row.DataItem, "a data item")
Upvotes: 3