Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid RowDataBound when GridView is edited?

Currently, I have the following code in the RowDataBound:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label groupID = (Label)e.Row.FindControl("idgroup");
            LinkButton myLink = (LinkButton)e.Row.FindControl("groupLink");
            myLink.Attributes.Add("rel", groupID.Text);
        }
}

However, when I click on the Edit link, it tries to run that code and throws an error. Therefore, how can I run that code ONLY when the GridView is in read mode? But not when editing...

like image 758
aleafonso Avatar asked Dec 04 '22 07:12

aleafonso


2 Answers

Here is how to do it! It will only execute the code over the rows (when reading or editing mode) except for the row that is being edited!!!

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState == DataControlRowState.Normal) || (e.Row.RowState == DataControlRowState.Alternate))
            {
                Label groupID = (Label)e.Row.FindControl("idgroup");
                LinkButton myLink = (LinkButton)e.Row.FindControl("groupLink");
                myLink.Attributes.Add("rel", groupID.Text);
            }
        }
    }
like image 122
aleafonso Avatar answered Dec 11 '22 10:12

aleafonso


you can add a check like this:

if (e.Row.RowState != DataControlRowState.Edit)
{
  // Here logic to apply only on initial DataBinding...
}
like image 21
Davide Piras Avatar answered Dec 11 '22 09:12

Davide Piras