Getting error while finding control in Footer Row Gridview

Gani_tpt 2,026 Reputation points
2024-05-29T13:47:28.71+00:00

I am getting error while binding data in dropdown in gridview footer template.

Pls. help us, how to bind dropdown values inside the gridview footer template.

getting below error.

User's image

User's image

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,358 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,461 questions
{count} votes

Accepted answer
  1. AgaveJoe 26,671 Reputation points
    2024-05-29T18:31:05.81+00:00

    A GridView has one footer but the OnRowDataBound fires for every row in the GridView. Your logic is looking for the ddlCustomer in every row which is not very logical.

    Use the GridView DataBound handler to get to the footer during a post back. Keep in mind, GridView handler information is openly published in the official documentation and often comes with sample code.

    protected void gvcustomer_DataBound(Object sender, EventArgs e)
    { 
        // Get the footer row.
        GridViewRow footerRow = gvcustomer.FooterRow;
    }
    

    https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview.footerrow?view=netframework-4.8.1

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Lan Huang-MSFT 27,716 Reputation points Microsoft Vendor
    2024-05-30T02:44:08.77+00:00

    Hi @Gani_tpt,

    Try the following code:

     protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
     {
         if (e.Row.RowType == DataControlRowType.Footer)
         {
             DropDownList DropDownList1 = (e.Row.FindControl("ddlcustomer") as DropDownList);
             List<Item> items = new List<Item>();
             items.Add(new Item() { Value= "1", Text = "USA" });
             items.Add(new Item() { Value = "2", Text = "UK" });
             items.Add(new Item() {Value = "3", Text = " AUS" });
             items.Add(new Item() {Value = "4", Text = "  OTHERS" });
             DropDownList1.DataSource = items;
             DropDownList1.DataTextField = "Text";
             DropDownList1.DataValueField = "Value";
             DropDownList1.DataBind();
             DropDownList1.Items.Insert(0, new ListItem("--Select --", "0"));
         }
     }
    
    public class Item
    {
        public Item() { }
        public string Value { set; get; }
        public string Text { set; get; }
    }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments