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
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
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.