Show Hide ASP.Net GridView AutoGenerate Select Button based on DropDownList using C# and VB.Net

Last Reply 27 days ago By dharmendr

Posted 27 days ago

i am trying to hide select button of gridview when dropdown selection is equal to Receive, but condition is not working properly

    protected void Unnamed1_SelectedIndexChanged(object sender, EventArgs e)
    {
        statsGRN();
    }

    private void statsGRN()
    {
        if (DDLstat.SelectedItem.Text == "Pending")
        { 
            con.Open();
            SqlCommand cmd = new SqlCommand("[Sp_Rags_Arrivel]", con);

            //cmd.Parameters.AddWithValue("@Action", "SELECT");
            cmd.Parameters.AddWithValue("@Action", "Pending");
            SqlDataAdapter sda = new SqlDataAdapter(cmd);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            DataTable dt = new DataTable();

            sda.Fill(dt);
            GVCon.DataSource = dt;
            GVCon.DataBind();
            con.Close();
            con.Dispose();
        }
        else if (DDLstat.SelectedItem.Text == "Receive")
           GVCon.AutoGenerateSelectButton = false;
        {
            con.Open();          
            SqlCommand cmd = new SqlCommand("[Sp_Rags_Arrivel]", con);

            //cmd.Parameters.AddWithValue("@Action", "SELECT");
            cmd.Parameters.AddWithValue("@Action", "Receive");
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                DataTable dt = new DataTable();

                sda.Fill(dt);
                GVCon.DataSource = dt;
                GVCon.DataBind();              
                con.Close();
                con.Dispose();               
            }      
        }
    } 

 

You are viewing reply posted by: dharmendr 27 days ago.
Posted 27 days ago

Hi akhter,

Check this example. Now please take its reference and correct your code.

HTML

<asp:DropDownList ID="ddlStatus" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Changed">
    <asp:ListItem Selected="True">Select</asp:ListItem>
    <asp:ListItem>Receive</asp:ListItem>
    <asp:ListItem>Pending</asp:ListItem>
</asp:DropDownList>
<hr />
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="False" AutoGenerateSelectButton="true">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="ID" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>

Code

C#

private void BindgridView()
{
    System.Data.DataTable dt = new System.Data.DataTable();
    dt.Columns.AddRange(new System.Data.DataColumn[] { 
                new System.Data.DataColumn("Id", typeof(int)),
                new System.Data.DataColumn("Name", typeof(string)),
                new System.Data.DataColumn("Country",typeof(string)) });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

protected void Changed(object sender, EventArgs e)
{
    statsGRN();
}

private void statsGRN()
{
    if (ddlStatus.SelectedItem.Text.Trim().ToLower() == "pending")
    {
        this.GridView1.AutoGenerateSelectButton = true;
        this.BindgridView();
    }
    else if (ddlStatus.SelectedItem.Text.Trim().ToLower() == "receive")
    {
        this.GridView1.AutoGenerateSelectButton = false;
        this.BindgridView();
    }
}

VB.Net

Private Sub BindgridView()
    Dim dt As System.Data.DataTable = New System.Data.DataTable()
    dt.Columns.AddRange(New System.Data.DataColumn() {
                        New System.Data.DataColumn("Id", GetType(Integer)),
                        New System.Data.DataColumn("Name", GetType(String)),
                        New System.Data.DataColumn("Country", GetType(String))})
    dt.Rows.Add(1, "John Hammond", "United States")
    dt.Rows.Add(2, "Mudassar Khan", "India")
    dt.Rows.Add(3, "Suzanne Mathews", "France")
    dt.Rows.Add(4, "Robert Schidner", "Russia")
    GridView1.DataSource = dt
    GridView1.DataBind()
End Sub

Protected Sub Changed(ByVal sender As Object, ByVal e As EventArgs)
    statsGRN()
End Sub

Private Sub statsGRN()
    If ddlStatus.SelectedItem.Text.Trim().ToLower() = "pending" Then
        Me.GridView1.AutoGenerateSelectButton = True
        Me.BindgridView()
    ElseIf ddlStatus.SelectedItem.Text.Trim().ToLower() = "receive" Then
        Me.GridView1.AutoGenerateSelectButton = False
        Me.BindgridView()
    End If
End Sub

Screenshot