65.9K
CodeProject 正在变化。 阅读更多。
Home

如何在 DataGrid 中放置 DropDownList 并为每一行触发 SelectedIndexChanged

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.54/5 (11投票s)

2007年4月4日

4分钟阅读

viewsIcon

59923

downloadIcon

753

本示例说明如何在 DataGrid 中放置 DropDownList,并为每一行根据 DropDownList 中选定的项目触发 DropDownList 的 SelectedIndexChanged 事件。

Screenshot - ScreenShot.jpg

引言

在本例中,我在 DataGrid 的两个列中放置了两个 DropDownList,并根据第一列 DDL 的选择,为每一行更改第二列 DDL 的内容。

背景

(可选) 是否有任何关于本文的背景信息可能有用,例如对所介绍的基本思想的介绍?

使用代码

如何在 DataGrid 中放置控件


1. 放置 DataGrid
2. 右键单击选择“属性生成器”
3. 从左侧面板选择“列”
4. 在“可用列”中选择“模板列”
5. 单击“>”(使“模板列”出现在“选定列”)
6. 按需更改“标题文本”、“页脚文本”等。
7. 点击“OK”
8. 右键单击网格并选择“编辑模板” --> “列[0]”(您需要选择要放置控件的列)
9. 将控件放置在某个区域
a. 项模板或
b. 编辑项模板等,根据您的需求

注意:在我们的示例中,我们将一个下拉列表放置在项模板中。

在 DataGrid 中放置控件非常简单。如果您放置一个复选框或单选按钮并处理它非常容易,即使是 DropDownList。但是当我们遇到困难时?如果您放置一个控件(如复选框、单选按钮或 DropDownList),然后想处理该控件的事件,这会带来一些困难。如何克服这个问题?


如何处理该控件的事件


要处理该控件的事件,您需要编写一个将在事件触发时触发的过程。在我们的示例中,该过程是“ddlGroup_SelectedIndexChanged”。

在我们的示例代码中,我有一个名为“DDLSample”的表,其中包含一些数据。生成表和插入数据的脚本是:

create table DDLSample (GroupName varchar(15), Items varchar(15))

insert into DDLSample values ('Fruit', 'Mango')
insert into DDLSample values ('Fruit', 'Orange')
insert into DDLSample values ('Fruit', 'Grape')

insert into DDLSample values ('Color', 'Red')
insert into DDLSample values ('Color', 'Green')
insert into DDLSample values ('Color', 'Blue')
insert into DDLSample values ('Color', 'Yellow')

insert into DDLSample values ('Animal', 'Dog')
insert into DDLSample values ('Animal', 'Cat')
insert into DDLSample values ('Animal', 'Lion')

打开一个 vb.net ASP.Net Web 应用程序

放置一个 Grid 并将 AutoGenerateColumns 设置为 false

在两个列中放置两个 DropDownList,并将列标题分别设置为“Group”和“Item”。

分别将下拉列表的 ID 改为“ddlGroup”和“ddlItem”。

在 webform1 的 HTML 编辑器中,在 ddlGroup 的属性中添加以下行:
OnSelectedIndexChanged="ddlGroup_SelectedIndexChanged"

这样,ddlGroup 的代码应该如下所示:

<code>
<ItemTemplate>
<asp:DropDownList id="ddlGroup" runat="server" OnSelectedIndexChanged="ddlGroup_SelectedIndexChanged"
AutoPostBack="True"> </asp:DropDownList>
</ItemTemplate>
</code>

声明变量为:
<code>
Dim sqlConnection As sqlConnection
Dim sqlCommand As sqlCommand
Dim dsGroup As DataSet = New DataSet
Dim dsItem As DataSet = New DataSet
Dim daGroup As SqlDataAdapter
Dim daItem As SqlDataAdapter = New SqlDataAdapter
</code>

在 Page_Load 中
<code>
If Not IsPostBack Then
sqlConnection = New SqlConnection("initial catalog=[DBName];user id=sa;password=;server=(local)")
sqlConnection.Open()
sqlCommand = New SqlCommand("select distinct GroupName from ddlsample", sqlConnection)
sqlCommand.CommandType = CommandType.Text
daGroup = New SqlDataAdapter(sqlCommand)
daGroup.Fill(dsGroup, "dtGroup")
DataGrid1.DataSource = dsGroup
DataGrid1.DataBind()
End If
</code>


在 DataGrid1_ItemDataBound 中
<code>
Dim dr As DataRowView = CType(e.Item.DataItem, DataRowView)
If ((e.Item.ItemType = ListItemType.Item) _ OrElse (e.Item.ItemType = ListItemType.AlternatingItem)) Then
Dim listGroup As DropDownList = CType(e.Item.FindControl("ddlGroup"), DropDownList)
listGroup.DataSource = dsGroup
listGroup.DataValueField = "GroupName"
listGroup.DataTextField = "GroupName"
listGroup.DataBind()
listGroup.Items.Insert(0, New ListItem("Select One", "Select One")) End If
</code>

触发事件的自定义过程
<code>
Protected Sub ddlGroup_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddllist As DropDownList = CType(sender, DropDownList)
Dim cell As TableCell = CType(ddllist.Parent, TableCell)
Dim item As DataGridItem = CType(cell.Parent, DataGridItem)
Dim content As String = item.Cells(0).Text
Dim ddlType As DropDownList = CType(item.Cells(0).FindControl("ddlGroup"), DropDownList)
sqlConnection = New SqlConnection("initial catalog=SathyaSample;user id=sa;password=sa;server=(local)")
sqlConnection.Open()
sqlCommand = New SqlCommand(("select Items from ddlsample where GroupName = '"+ (ddlType.SelectedItem.Text + "'")), sqlConnection) sqlCommand.CommandType = CommandType.Text
daItem = New SqlDataAdapter(sqlCommand)
daItem.Fill(dsItem, "dtItem")
Dim ddlItem As DropDownList = CType(item.Cells(1).FindControl("ddlItem"), DropDownList)
If (ddlType.SelectedItem.Text = "Select One") Then ddlItem.Items.Clear()
Else
ddlItem.Items.Clear()
ddlItem.DataSource = dsItem
ddlItem.DataValueField = "Items"
ddlItem.DataTextField = "Items"
ddlItem.DataBind()
ddlItem.Items.Insert(0, New ListItem("Select One", "Select One"))
End If
End Sub
</Code>

摘要

现在处理 DataGrid 中的控件变得非常容易。

© . All rights reserved.