4

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...

aleafonso
  • 2,244
  • 8
  • 38
  • 58

5 Answers5

7

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);
            }
        }
    }
aleafonso
  • 2,244
  • 8
  • 38
  • 58
6

you can add a check like this:

if (e.Row.RowState != DataControlRowState.Edit)
{
  // Here logic to apply only on initial DataBinding...
}
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • I have just tried but still when the edit link is clicked it pass this condition. Therefore, it does not avoid the code to run when editing. Any other option? Thanks a lot – aleafonso Sep 14 '11 at 14:09
  • can you debug and check other properties of the e or e.Row objects to see if you can detect the edit status of the row and act accordingly? :) – Davide Piras Sep 14 '11 at 14:17
  • Thank you for the advice man! I have just sorted it out debugging and looking at e.Row object as you mentioned. I will publish it as soon as stackoverflow allows me to answer my own question. Cheers! – aleafonso Sep 14 '11 at 15:07
  • 1
    You can edit your question putting the findings at the bottom. – Davide Piras Sep 14 '11 at 15:09
2

Add a check for e.Row.RowState:

if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
    //In Edit mode
}
Waqas
  • 6,812
  • 2
  • 33
  • 50
  • This condition is not suitable since the code is not run (even when I haven't clicked on the "edit" link). Therefore, the code in the RowDataBound is never run. Any other option? Thanks in advance! – aleafonso Sep 14 '11 at 14:07
  • +1. As stated in the code comment, this block executes when the row is in edit mode. Checking the opposite ("not in edit mode") can be done by either wrapping the statement with "!()", changing ">" with "==", or by adding an else block (in case you have a use for the if block). – Shmuel Valariola Oct 11 '12 at 23:01
2

Davide's answer is almost correct.. However it will fail for alternate rows. Here is the correct solution:

if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState != DataControlRowState.Edit && e.Row.RowState != (DataControlRowState.Edit | DataControlRowState.Alternate))
{ 
    // Here logic to apply only on rows not in edit mode
}
Sameer Alibhai
  • 3,092
  • 4
  • 36
  • 36
0

In your gridview, search for OnrowDataBound event which will like OnrowDataBound="GridView1_RowDataBound" remove that code and disable the above code.

Narender
  • 1
  • 2
  • Sorry for the misunderstanding. I do want the RowDataBound to run but only when the GridView is in reading mode (no in editing mode after clicking on the edit link). Thanks anyway – aleafonso Sep 14 '11 at 14:12