Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Unable to get gridview footer values in RowCommand

Error: 

Unable to get gridview footer values in RowCommand

Solution 1:

Code in the page load should look like this, check whether you are binding the grid view correctly in the Page Load.

if (!IsPostBack)

{

//Bind Grid data here

  LoadGrid();

}

 Solution 2:

You are not using sender in your find control code line.

Example your code like:

    TextBox txtname = (TextBox)GrdProducts.FooterRow.FindControl("txtAddProductName");

   TextBox txtprice = (TextBox)GrdProducts.FooterRow.FindControl("txtAddprice");

This should be:

TextBox txtprice = (TextBox)((GridView)sender).FooterRow.FindControl("txtAddprice");

How to bind data in GridView Footer dropdown?

How to bind data in GridView Footer dropdown?

I want to bind list of product Categories in gridview footer Dropdown List.

I want to use this dropdown list as search filter to search products by its category.

ASPX Code:

<asp:GridView ID="GrdProducts" runat="server AutoGenerateColumns="False"
OnRowDataBound="GrdProducts_RowDataBound">          
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate> 
<asp:label ID="lblProductId" runat="Server" Text='<%# Eval("ProductID") %>' ></asp:label>  
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>      
<asp:label ID="txtProductName" runat="Server" Text='<%# Eval("ProductName") %>' ></asp:label>
</ItemTemplate>  
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Price">
<ItemTemplate>      
<asp:label ID="txtProductPrice" runat="Server" Text='<%# Eval("Price") %>' ></asp:label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

C# Code:

  protected void GrdProducts_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                // Retrieve the LinkButton control from the first column.
                DropDownList drpCategories = (DropDownList)e.Row.FindControl("drpCat");
                var cat = CRUDHelperClass.GetCatDetails();
 
                drpCategories.DataSource = cat.Tables[0];
                drpCategories.DataTextField = "CategoryName";
                drpCategories.DataValueField = "CategoryID";
                drpCategories.DataBind();
 
            }
        }