Click here to Skip to main content
15,898,920 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear,

I try to make a dropdownlist with selected value and will display in gridview table. So I have this 5 dropdown list value which are : OPTO,IMP,PHL,Tool Room & Other Process. For the gridview should be display each module file by dropdownlist respectively. FYI, the dropdownlist are the modules located in modul_category under ModulContents in SQL. Please help me to do so.

Here for clear view :
Please Choose : --Select Option--
OPTO
IMP
PHL
Tool Room
Other Process

ID Modul Name Modul Category View File
1 Intro OPTO ®

What I have tried:

Asp.net Code :
style="font-size: 12px; font-family:Calibri; text-align: left;">
Please Choose : <asp:DropDownList ID="DropDownList" runat="server" Height="26px" Width="254px" DataSourceID="SqlDataSource1"
DataTextField="modul_category" DataValueField="modul_category" AppendDataBoundItems="True" AutoPostBack="True" EnableViewState="False">
<asp:ListItem Value="">-- Select Option --



Behind Code :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindLocation()
BindGridView()

End If
End Sub
Protected Sub BindLocation()
Dim dt As New DataTable()
Dim myCommand As SqlCommand
Dim str As String
Dim reader As SqlDataReader
Dim readsessionid As String = Session("emp_username")

Using myConnection As New SqlConnection(Strcon)

'you need to provide password for sql server
str = "SELECT * FROM Employee WHERE emp_username='" & readsessionid & "'"
'Response.Write(str)
myCommand = New SqlCommand(str, myConnection)
myConnection.Open()

Dim cmd As New SqlCommand("Select modul_category from ModulContents", myConnection)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
reader = myCommand.ExecuteReader()

If (reader.Read()) Then

userNameLabel.Text = reader("emp_username").ToString()
deptLabel.Text = reader("deptName").ToString()

End If

myConnection.Close()
DropDownList.DataSource = dt
DropDownList.DataTextField = "modul_category"
DropDownList.DataValueField = "modul_category"
DropDownList.DataBind()
DropDownList.Items.Insert(0, New ListItem("Select Option", ""))

End Using

End Sub
Protected Sub BindGridView()
Dim dt As New DataTable()
Using con As New SqlConnection(Strcon)
con.Open()
Dim cmd As New SqlCommand("SELECT * FROM ModulContents", con)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
con.Close()
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Sub
Protected Sub DropDownList_SelectedIndexChanged(ByVal Sender As Object, ByVal e As EventArgs)
Dim dt As New DataTable()
Using con As New SqlConnection(Strcon)
con.Open()
If DropDownList.SelectedValue <> "" Then
Dim cmd As New SqlCommand("SELECT * FROM ModulContents WHERE modul_category = @modul_category", con)
cmd.Parameters.AddWithValue("@modul_category", DropDownList.SelectedValue)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
Else
Dim cmd As New SqlCommand("SELECT * FROM ModulContents", con)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
End If
con.Close()
GridView1.DataSource = dt
GridView1.DataBind()

End Using
End Sub
Posted
Updated 22-Aug-18 14:50pm
Comments
Vincent Maverick Durano 21-Aug-18 9:19am    
I'm not sure if I follow you correctly, are you trying to populate your GridView based DropDownList value?
Member 13949827 22-Aug-18 20:17pm    
yes, that's it. Is it possible to do that?

1 solution

Quote:
yes, that's it. Is it possible to do that?


Here's a quick example on how to populate a GridView based on DropDownList selected value.


VB.NET
VB
Class SurroundingClass
    Private Function GetConnectionString() As String
        Return System.Configuration.ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString
    End Function

    Private Sub BindGridView(ByVal dropDownValue As String)
        Dim sqlStatement As String = "SELECT * FROM YourTableName WHERE ColumnName = @Value1"
        Dim dt As DataTable = New DataTable()

        Using connection As SqlConnection = New SqlConnection(GetConnectionString())

            Using cmd As SqlCommand = New SqlCommand(sqlStatement, connection)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@Value", dropDownValue)
                connection.Open()

                Using adapter = New SqlDataAdapter(cmd)
                    adapter.Fill(dt)

                    If dt.Rows.Count > 0 Then
                        GridView1.DataSource = dt
                        GridView1.DataBind()
                    Else
                    End If
                End Using
            End Using
        End Using
    End Sub

    Protected Sub DropDownLis1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        BindGridView(DropDownList1.SelectecItem.Value)
    End Sub
End Class


C# Equivalent
C#
class SurroundingClass
{
    private string GetConnectionString()
    {
        return System.Configuration.ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString;
    }

    private void BindGridView(string dropDownValue)
    {
        string sqlStatement = "SELECT * FROM YourTableName WHERE ColumnName = @Value1";
        DataTable dt = new DataTable();

        using (SqlConnection connection = new SqlConnection(GetConnectionString()))
        {
            using (SqlCommand cmd = new SqlCommand(sqlStatement, connection))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Value", dropDownValue);
                connection.Open();

                using (var adapter = new SqlDataAdapter(cmd))
                {
                    adapter.Fill(dt);

                    if (dt.Rows.Count > 0)
                    {
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                    else
                    {
                    }
                }
            }
        }
    }

    protected void DropDownLis1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridView(DropDownList1.SelectecItem.Value);
    }
}


The idea is to wire up the DropDownList SelectedIndexChanged event and call the method for binding the GridView by passing along the selected value. Note: Make sure to set AutoPostback to TRUE in your dropdownlist to trigger the changed event.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900