Click here to Skip to main content
14,545,108 members
Rate this:
Please Sign up or sign in to vote.
See more:
ok I have this item in my gridView:
asp:TemplateField HeaderText="BulletGroup ID" SortExpression="BulletGroupID">
       <ItemTemplate>
                     <asp:Label ID="lblBulletGroupID" Text='<%# Eval("BulletGroupID") %>' runat="server"></asp:Label>
                  </ItemTemplate>
                  <EditItemTemplate>
                     <asp:Label ID="BulletGroupID" Text='<%# Eval("BulletGroupID") %>' runat="server"></asp:Label>
                  </EditItemTemplate>
                  <FooterTemplate>
                     <asp:TextBox ID="txtBulletGroupID" Text='' runat="server"></asp:TextBox>
                  </FooterTemplate>
       </asp:TemplateField>

the grid view being successfully populated with a datasource that is of a type List<>
and in the OnRowDataBound event I am attempting to get the value from this Item template and am creating A DataRowView object thusly:
DataRowView tmp = (DataRowView)row.DataItem;

however I am getting a null back for tmp
which means this line will throw a null exception:
int BulletGroupID = int.Parse( (tmp)["BulletGroupID"].ToString());


I have also tried getting the value back via this call:
int BulletGroupID = int.Parse( ((Label)row.FindControl("lblBulletGroupID")).Text);


Which is also throwing a null exception

Help me ObiWan you are my only hope

What I have tried:

Examining multiple examples of using a Gridview
Posted
Updated 10-Dec-18 12:54pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You're getting null because you are referencing a control from within EditItemTemplate which can only be accessible when the GridView is on Edit mode. You need to check for the RowType and RowState before accessing any controls from GridView at OnRowDataBound event. Here's an example on how you can access a control from ItemTemplate:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         Label lblGroupBulletID = (Label)e.Row.FindControl("lblBulletGroupID");
         //do something here

    }
}


As you can see accessing a control from ItemTemplate is pretty much straight-forward as we will only be checking for the RowType of type DataRow. However, accessing a control from EditItemTemplate requires additional checking. Here's an example:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            Label lblEditGroupBulletID = (Label)e.Row.FindControl("BulletGroupID");
            //do something here

        }
    }
}


First we check the RowType to ensure that we are only manipulating the rows of type DataRow. Please note that a GridView is composed of several row types such as Header, DataRow, EmptyDataRow, Footer, Pager and Separator. The next line in code block above is the critical part of the code and that is to determine the Edit state.

Accessing the controls from within <edititemtemplate> is a bit tricky, especially if you are not really familiar with how the stuff works within a GridView. Equating the RowState to DataControlState.Edit isn't really accurate and you might get an exception when doing so. The RowState property is a bitwise combination. Therefore, the RowState could indicate that you are in the Edit state and the Alternate state. Hence, you cannot do a simple equality check when you are in Edit mode. Instead you must do something like this:

if ((e.Row.RowState & DataControlRowState.Edit) > 0)   
{  
     //access controls here  
} 


We use the bitwise “&” operator to determine if the GridView is in Edit mode and check the result if its greater than zero. For details about the Bitwise operator see: Bitwise Operators in C#[^] .

Once we've manage to determine the Edit state, you can now begin accessing the controls using the FindControl() method as you would typically do when referencing a control from a Data Representation Control such as GridView.

Hope that helps!
   
v2
Comments
RmcbainTheThird 11-Dec-18 8:07am
   
thank you Vincent. This works My big mistake was assuming that the OnRowDataBound event was only called once( the Row part of the event name should have been a hint :) )

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100