ASP.NET:在DataGrid中显示下拉列表框
脚本:
1<asp:DataGrid id="dgHHRole" runat="server" CssClass="DefaultText" AutoGenerateColumns="False" AllowPaging="True" PageSize="20" AllowSorting="True">
2<FooterStyle Wrap="False" HorizontalAlign="Center" VerticalAlign="Middle"></FooterStyle>
3<SelectedItemStyle HorizontalAlign="Center" ForeColor="Black" CssClass="defaultText" BackColor="#99FFFF"></SelectedItemStyle>
4<AlternatingItemStyle HorizontalAlign="Center" CssClass="GridAlternateRows"></AlternatingItemStyle>
5<ItemStyle HorizontalAlign="Center"></ItemStyle>
6<HeaderStyle HorizontalAlign="Left" CssClass="GridHeaderAbove" VerticalAlign="Middle"></HeaderStyle>
7<Columns>
8 <asp:BoundColumn Visible="False" DataField="RoleId" HeaderText="RoleId"></asp:BoundColumn>
9 <asp:BoundColumn DataField="FullName" SortExpression="FullName" HeaderText="Full Name"></asp:BoundColumn>
10 <asp:BoundColumn DataField="Location" HeaderText="Location"></asp:BoundColumn>
11 <asp:BoundColumn DataField="CallType" HeaderText="Call Type"></asp:BoundColumn>
12 <asp:BoundColumn DataField="RouteNm" HeaderText="Route Number"></asp:BoundColumn>
13 <asp:TemplateColumn HeaderText="Role">
14 <ItemTemplate>
15 <asp:DropDownList id="ddlHHRole" runat="server"></asp:DropDownList>
16 </ItemTemplate>
17 </asp:TemplateColumn>
18</Columns>
19<PagerStyle Mode="NumericPages"></PagerStyle>
20</asp:DataGrid>
在ItemCreated事件中绑定数据2<FooterStyle Wrap="False" HorizontalAlign="Center" VerticalAlign="Middle"></FooterStyle>
3<SelectedItemStyle HorizontalAlign="Center" ForeColor="Black" CssClass="defaultText" BackColor="#99FFFF"></SelectedItemStyle>
4<AlternatingItemStyle HorizontalAlign="Center" CssClass="GridAlternateRows"></AlternatingItemStyle>
5<ItemStyle HorizontalAlign="Center"></ItemStyle>
6<HeaderStyle HorizontalAlign="Left" CssClass="GridHeaderAbove" VerticalAlign="Middle"></HeaderStyle>
7<Columns>
8 <asp:BoundColumn Visible="False" DataField="RoleId" HeaderText="RoleId"></asp:BoundColumn>
9 <asp:BoundColumn DataField="FullName" SortExpression="FullName" HeaderText="Full Name"></asp:BoundColumn>
10 <asp:BoundColumn DataField="Location" HeaderText="Location"></asp:BoundColumn>
11 <asp:BoundColumn DataField="CallType" HeaderText="Call Type"></asp:BoundColumn>
12 <asp:BoundColumn DataField="RouteNm" HeaderText="Route Number"></asp:BoundColumn>
13 <asp:TemplateColumn HeaderText="Role">
14 <ItemTemplate>
15 <asp:DropDownList id="ddlHHRole" runat="server"></asp:DropDownList>
16 </ItemTemplate>
17 </asp:TemplateColumn>
18</Columns>
19<PagerStyle Mode="NumericPages"></PagerStyle>
20</asp:DataGrid>
1 private void dgHHRole_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
2 {
3 if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
4 {
5 DropDownList ddl = (DropDownList)e.Item.FindControl("ddlHHRole");
6 if(ddl != null)
7 {
8 ddl.DataSource = HHRoleDt;
9 ddl.DataTextField = "RoleNm";
10 ddl.DataValueField = "RoleId";
11 ddl.DataBind();
12 }
13 }
14 }
在ItemDataBound事件中设置值2 {
3 if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
4 {
5 DropDownList ddl = (DropDownList)e.Item.FindControl("ddlHHRole");
6 if(ddl != null)
7 {
8 ddl.DataSource = HHRoleDt;
9 ddl.DataTextField = "RoleNm";
10 ddl.DataValueField = "RoleId";
11 ddl.DataBind();
12 }
13 }
14 }
1 private void dgHHRole_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
2 {
3 if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
4 {
5 DropDownList ddl = (DropDownList)e.Item.FindControl("ddlHHRole");
6 ListItem lst = new ListItem();
7 lst.Text = "";
8 lst.Value = "";
9
10 ddl.Items.Insert(0,lst);
11 SetValue(ddl,e.Item.Cells[0].Text);
12 }
13 }
14
15 private void SetValue(DropDownList ddl,string strvalue)
16 {
17 if(strvalue == null || strvalue.Trim().Length ==0)
18 {
19 return;
20 }
21 if(ddl != null && ddl.Items.Count >0)
22 {
23 int iIndex;
24 for(iIndex=0;iIndex<ddl.Items.Count;iIndex++)
25 {
26 if(ddl.Items[iIndex].Value.Trim() == strvalue.Trim())
27 {
28 ddl.Items[iIndex].Selected =true;
29 return;
30 }
31 else
32 {
33 ddl.Items[iIndex].Selected =false;
34 }
35 }
36 }
37 }
2 {
3 if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
4 {
5 DropDownList ddl = (DropDownList)e.Item.FindControl("ddlHHRole");
6 ListItem lst = new ListItem();
7 lst.Text = "";
8 lst.Value = "";
9
10 ddl.Items.Insert(0,lst);
11 SetValue(ddl,e.Item.Cells[0].Text);
12 }
13 }
14
15 private void SetValue(DropDownList ddl,string strvalue)
16 {
17 if(strvalue == null || strvalue.Trim().Length ==0)
18 {
19 return;
20 }
21 if(ddl != null && ddl.Items.Count >0)
22 {
23 int iIndex;
24 for(iIndex=0;iIndex<ddl.Items.Count;iIndex++)
25 {
26 if(ddl.Items[iIndex].Value.Trim() == strvalue.Trim())
27 {
28 ddl.Items[iIndex].Selected =true;
29 return;
30 }
31 else
32 {
33 ddl.Items[iIndex].Selected =false;
34 }
35 }
36 }
37 }