如何通过特定类别VB.NET在gridview中设置下拉列表的选定值 [英] How to set selectedvalue of dropdownlist in a gridview by specific category VB.NET
问题描述
亲爱的,
我尝试制作一个带有选定值的下拉列表,并将显示在gridview表中。所以我有这5个下拉列表值:OPTO,IMP,PHL,Tool Room&其他过程。对于gridview,应分别按下拉列表显示每个模块文件。仅供参考,下拉列表是位于SQL中ModulContents下modul_category中的模块。请帮我这样做。
这里有清晰的视图:
请选择: - 选择选项 -
OPTO
IMP
PHL
工具室
其他流程
ID模块名称模块类别查看文件
1简介OPTO®
我尝试了什么:
Asp.net代码:
style =font-size:12px; font-family:Calibri; text -align:left;>
请选择:< asp:DropDownList ID =DropDownListrunat =serverHeight =26pxWidth =254pxDataSourceID =SqlDataSource1
DataTextField =modul_categoryDataValueField =modul_categoryAppendDataBoundItems =TrueAutoPostBack =TrueEnableViewState =False>
< asp:ListItem Value => - 选择选项 -
代码背后:
受保护的子Page_Load(ByVal发送者为对象,ByVal e As System.EventArgs)处理Me.Load
如果不是IsPostBack那么
BindLocation()
BindGridView()
结束如果
End 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)
使用myConnection作为新的SqlConnection(Strcon)
'你需要为sql server提供密码
str =SELECT * FROM Employee WHERE emp_username ='&阅读与阅读'
'Response.Write(str)
myCommand = New SqlCommand(str,myConnection)
myConnection.Open()
Dim cmd As New SqlCommand(从ModulContents中选择modul_category,myConnection)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
reader = myCommand.ExecuteReader()
If(reader.Read())Then < br $>
userNameLabel.Text = reader(emp_username)。ToString()
deptLabel.Text = reader(deptName)。ToString( )
结束如果
myConnection.Close()
DropDownList。 DataSource = dt
DropDownList.DataTextField =modul_category
DropDownList.DataValueField =modul_category
DropDownList.DataBind()
DropDownList.Items.Insert(0,New ListItem(Select Option,))
结束使用
结束子
受保护的子BindGridView()
Dim dt As New DataTable()
使用con As新的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()
结束使用
结束子
受保护的子DropDownList_SelectedIndexChanged(ByVal Sender As Object,ByVal e As EventArgs)
Dim dt As New DataTable()
使用con作为新的SqlConnection(Strcon)
con.Open()
如果DropDownList.SelectedValue< > 然后
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)
结束如果
con.Close()
GridView1.DataSource = dt
GridView1.DataBind()
结束使用
结束Sub
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
推荐答案
是的,就是这样。是否可以这样做?
yes, that's it. Is it possible to do that?
这是一个关于如何填充 GridView
的简单示例基于 DropDownList
所选值。
VB.NET
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#等价物
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);
}
}
想法是连接 DropDownList
SelectedIndexChanged
事件并通过传递选定的值来调用绑定 GridView
的方法。 注意:确保在下拉列表中将 AutoPostback
设置为 TRUE
以触发改变的事件。
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.
这篇关于如何通过特定类别VB.NET在gridview中设置下拉列表的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!