回答你的问题有几个部分, 我正在做一些假设(你用视觉工作室作为IDE, 你可以用VB作为代码背后的语言):
我要绑定一个 Sql 语句以填充下调列表...
您可以在代码内或通过视觉工作室 GUI 中做到这一点。 您可以使用一个下调列表的边框, 模板字段最终会给予您更大的灵活性 。 我将在模板字段上读取( http:// msdn. microsoft. com/ en- us/library/ bb288032. aspx” rel = “ nofollow” > here a > ), 因为除了基本数据显示之外, 它将是您使用 Gridview 的朋友 。 使用 GUI, 选择下调列表应该给您上右侧的小箭头, 这样您就可以创建数据连接和数据源, 以便与您的下调列表绑在一起 。
Also based on the selection the user makes with this dropdownlist, I
want to populate the next column (column2) with the corresponding data
这要复杂一些, 因为您需要触发下调列表中的 PostBack (使用 AutoPostBack), 处理下调列表 s 选中的 Index changed 事件, 发现第二个栏中的控件需要更新, 并根据您下调列表中选中的索引/ 项目/ 值更新此控件。 有几种方法可以做到这一点, 但这里是我找到的最快的( 使用 asp. net winforms ) 。 我正在使用 SQL 冒险工作数据库, 在第一栏中的模板字段中与 雇员ID 嵌入一个下调, 并使用所选的雇员ID 在第二栏中填充一个标签, 与该雇员ID 管理器ID 。
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
EnableModelValidation="True" AutoGenerateColumns="False"
DataKeyNames="EmployeeID">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID"
InsertVisible="False" ReadOnly="True" SortExpression="EmployeeID" />
<asp:TemplateField>
<ItemTemplate>
Select EmployeeID
<asp:DropDownList ID="ddEmpID" runat="server" OnSelectedIndexChanged="ddEmpID_SelectedIndexChanged"
DataSourceID="SqlDataSource1" DataTextField="EmployeeID"
DataValueField="EmployeeID" AutoPostBack="True">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="labManagerID" runat="server" Text="ManagerID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=MyServerInstance;Initial Catalog=AdventureWorks;Integrated Security=True"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT TOP(10) EmployeeID, ManagerID FROM HumanResources.Employee">
</asp:SqlDataSource>
和从代码后方选择的 Index 更改的事件 :
Protected Sub ddEmpID_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim myDDList As DropDownList = CType(sender, DropDownList)
Dim gvRow As GridViewRow = CType(myDDList.NamingContainer, GridViewRow)
Dim myLabel As Label = CType(gvRow.FindControl("labManagerID"), Label)
Create your sql query here to populate a data object and assign values throughout your row
Dim myConnection As SqlConnection = New SqlConnection("Data Source=MyServerInstance;Initial Catalog=AdventureWorks;Integrated Security=True")
Dim myCmd As SqlCommand = New SqlCommand("Select ManagerID FROM HumanResources.Employee WHERE EmployeeID= " + myDDList.SelectedValue + " ", myConnection)
If myConnection.State = ConnectionState.Closed Then myConnection.Open()
myLabel.Text = myCmd.ExecuteScalar
If myConnection.State = ConnectionState.Open Then myConnection.Close()
End Sub
因为与Gridview合作有时是一种自焚练习, 我建议拥有一些好的 漫步辅导 手边:
- J,J,J,J,J,J,J,J,J,J,J,J,J,J,J,J