locked
Refresh dropdownlist in gridview,on Button Click RRS feed

  • Question

  • User-367318540 posted

    How to refresh dropdownlist in gridview on button click again ,below code,which is populating gridview ,i do not want to refresh whole page.

    protected void gvtrans_RowDataBound(object sender, GridViewRowEventArgs e)
          {
     
     
              if (e.Row.RowType == DataControlRowType.DataRow)
              {
     
     
                  DropDownList ddlsup = (DropDownList)e.Row.FindControl("ddlsup");
                  SqlCommand cmd = new SqlCommand("SP_Ret_COA_DDl_GV", con);
                  SqlDataAdapter sda = new SqlDataAdapter(cmd);
     
                  DataTable dt = new DataTable();
     
                  sda.Fill(dt);
                  ddlsup.DataSource = dt;
                  ddlsup.DataTextField = "Level_Five_Name";
                  ddlsup.DataValueField = "Level_Five_ID";
     
                  ddlsup.DataBind();
     
                  ddlsup.Items.Insert(0, new ListItem("--Select Bank--", "0"));
     
              }
     
    

    Wednesday, May 19, 2021 3:47 PM

Answers

  • User-939850651 posted

    Hi akhterr,

    Yes this is my requirement,but need more help,that how i will retrieve data from Database 

    as you below retrieving 

      protected void add_Click(object sender, EventArgs e)
            {
                dropDT.Rows.Add("Item4", 4);
                GridViewBind();
            }

    I just used a static DataTable to simulate the data.

    In your case, you need to use ADO.NET operations to insert it into the table. When you rebind the GridView data source, the OnRowDataBound event will be triggered. In the stored procedure you use will get new data to fill the dropdownlist box.

    Best regards,

    Xudong Peng

    • Marked as answer by An0nym0u5User Tuesday, September 21, 2021 12:00 AM
    Friday, May 21, 2021 1:49 AM

All replies

  • User-939850651 posted

    Hi akhterr,

    How to refresh dropdownlist in gridview on button click again ,below code,which is populating gridview ,i do not want to refresh whole page.

    According to your requirement, I recommand that you could use UpdatePanel.

    Something like this:

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager runat="server"></asp:ScriptManager>
                <asp:UpdatePanel runat="server" ID="updatePanel1">
                    <ContentTemplate>
                        <asp:GridView runat="server" ID="GV1" AutoGenerateColumns="false" OnRowDataBound="GV1_RowDataBound">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:DropDownList runat="server" ID="ddlsup" AutoPostBack="true" OnSelectedIndexChanged="ddlsup_SelectedIndexChanged"></asp:DropDownList>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="ID" HeaderText="Column1" />
                                <asp:BoundField DataField="Name" HeaderText="Column2" />
                                <asp:BoundField DataField="Description" HeaderText="Column3" />
                            </Columns>
                        </asp:GridView>
                        <br />
                        <asp:Label Text="" ID="result" runat="server" />
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="GV1" />
                    </Triggers>
                </asp:UpdatePanel>
            </div>
        </form>
    </body>
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[] {
                        new DataColumn("ID"),
                        new DataColumn("Name"),
                        new DataColumn("Description")
                    });
                    dt.Rows.Add(1, "Name1", "Description1");
                    dt.Rows.Add(2, "Name2", "Description2");
                    dt.Rows.Add(3, "Name3", "Description3");
                    GV1.DataSource = dt;
                    GV1.DataBind();
                }
            }
    
            protected void GV1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DropDownList ddlsup = (DropDownList)e.Row.FindControl("ddlsup");
    
                    DataTable dropDT = new DataTable();
                    dropDT.Columns.AddRange(new DataColumn[] {
                        new DataColumn("Level_Five_Name"),
                        new DataColumn("Level_Five_ID")
                    });
                    dropDT.Rows.Add("Item1", 1);
                    dropDT.Rows.Add("Item2", 2);
                    dropDT.Rows.Add("Item3", 3);
    
                    ddlsup.DataSource = dropDT;
                    ddlsup.DataTextField = "Level_Five_Name";
                    ddlsup.DataValueField = "Level_Five_ID";
    
                    ddlsup.DataBind();
                    ddlsup.Items.Insert(0, new ListItem("--Select--", "0"));
                }
            }
    
            protected void ddlsup_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ddl = sender as DropDownList;
                GridViewRow row = (GridViewRow)ddl.Parent.Parent;
                result.Text = "selected value in row " + (row.RowIndex + 1) + " is " + ddl.SelectedValue;
            }

    Hope this can help.

    Best regards,

    Xudong Peng

    Thursday, May 20, 2021 2:22 AM
  • User-367318540 posted

    i do not want above ,my requirement is that,i want to reload dropdownlist from database on button Click,i do not want to refresh whole page.

    means that ,on first time load page ,then dropdownlist which is in gridview ,have 3 item ,later i inserted one more item in database ,then i will not reload whole page ,i just want to reload dropdownlist.

    Thursday, May 20, 2021 5:54 AM
  • User-939850651 posted

    Hi akhterr,

    i do not want above ,my requirement is that,i want to reload dropdownlist from database on button Click,i do not want to refresh whole page.

    means that ,on first time load page ,then dropdownlist which is in gridview ,have 3 item ,later i inserted one more item in database ,then i will not reload whole page ,i just want to reload dropdownlist.

    You only need to add it to the data table in the button event, and re-bind the GridView. Something like this:

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager runat="server"></asp:ScriptManager>
                <asp:UpdatePanel runat="server" ID="updatePanel1">
                    <ContentTemplate>
                        <asp:GridView runat="server" ID="GV1" AutoGenerateColumns="false" OnRowDataBound="GV1_RowDataBound">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:DropDownList runat="server" ID="ddlsup" AutoPostBack="true" OnSelectedIndexChanged="ddlsup_SelectedIndexChanged"></asp:DropDownList>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="ID" HeaderText="Column1" />
                                <asp:BoundField DataField="Name" HeaderText="Column2" />
                                <asp:BoundField DataField="Description" HeaderText="Column3" />
                            </Columns>
                        </asp:GridView>
                        <br />
                        <asp:Label Text="" ID="result" runat="server" />
                        <br />
                        <asp:Button Text="add item" ID="add" OnClick="add_Click" runat="server" />
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="GV1" />
                    </Triggers>
                </asp:UpdatePanel>
            </div>
        </form>
    </body>
    public static DataTable dropDT = new DataTable();
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    dropDT.Columns.AddRange(new DataColumn[] {
                        new DataColumn("Level_Five_Name"),
                        new DataColumn("Level_Five_ID")
                    });
                    dropDT.Rows.Add("Item1", 1);
                    dropDT.Rows.Add("Item2", 2);
                    dropDT.Rows.Add("Item3", 3);
                    GridViewBind();
                }
            }
    
            private void GridViewBind()
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[] {
                        new DataColumn("ID"),
                        new DataColumn("Name"),
                        new DataColumn("Description")
                    });
                dt.Rows.Add(1, "Name1", "Description1");
                dt.Rows.Add(2, "Name2", "Description2");
                dt.Rows.Add(3, "Name3", "Description3");
                GV1.DataSource = dt;
                GV1.DataBind();
            }
    
            protected void GV1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DropDownList ddlsup = (DropDownList)e.Row.FindControl("ddlsup");
                    ddlsup.DataSource = dropDT;
                    ddlsup.DataTextField = "Level_Five_Name";
                    ddlsup.DataValueField = "Level_Five_ID";
    
                    ddlsup.DataBind();
                    ddlsup.Items.Insert(0, new ListItem("--Select--", "0"));
                }
            }
    
            protected void ddlsup_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ddl = sender as DropDownList;
                GridViewRow row = (GridViewRow)ddl.Parent.Parent;
                result.Text = "selected value in row " + (row.RowIndex + 1) + " is " + ddl.SelectedValue;
            }
    
            protected void add_Click(object sender, EventArgs e)
            {
                dropDT.Rows.Add("Item4", 4);
                GridViewBind();
            }

    Best regards,

    Xudong Peng

    Thursday, May 20, 2021 8:28 AM
  • User-367318540 posted

    Yes this is my requirement,but need more help,that how i will retrieve data from Database 

    as you below retrieving 

      protected void add_Click(object sender, EventArgs e)
            {
                dropDT.Rows.Add("Item4", 4);
                GridViewBind();
            }

    Thursday, May 20, 2021 8:35 AM
  • User-939850651 posted

    Hi akhterr,

    Yes this is my requirement,but need more help,that how i will retrieve data from Database 

    as you below retrieving 

      protected void add_Click(object sender, EventArgs e)
            {
                dropDT.Rows.Add("Item4", 4);
                GridViewBind();
            }

    I just used a static DataTable to simulate the data.

    In your case, you need to use ADO.NET operations to insert it into the table. When you rebind the GridView data source, the OnRowDataBound event will be triggered. In the stored procedure you use will get new data to fill the dropdownlist box.

    Best regards,

    Xudong Peng

    • Marked as answer by An0nym0u5User Tuesday, September 21, 2021 12:00 AM
    Friday, May 21, 2021 1:49 AM