GridView模板列示范代码
//页面源代码
<form id="form1" runat="server">
<div style="text-align: center">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" DataSourceID="EmployeeList">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="EmployeeID" HeaderText="雇员ID号" />
<asp:BoundField DataField="Name" HeaderText="雇员姓名" />
<asp:TemplateField HeaderText="BirthDate出生日期" SortExpression="BirthDate">
<EditItemTemplate>
<asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"
BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
ForeColor="#663399" Height="200px" SelectedDate='<%# Bind("BirthDate") %>' ShowGridLines="True"
VisibleDate='<%# Eval("BirthDate") %>' Width="220px">
<SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
<SelectorStyle BackColor="#FFCC66" />
<OtherMonthDayStyle ForeColor="#CC9966" />
<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
</asp:Calendar>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("BirthDate", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="上司">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ReportsToList"
DataTextField="Name" DataValueField="EmployeeID" SelectedValue='<%# Bind("Commander") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# GetReportsToName(Eval("Commander")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</div>
<asp:SqlDataSource ID="EmployeeList" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT EmployeeID, LastName + ',' + FirstName AS Name, BirthDate, ReportsTo AS Commander FROM Employees">
</asp:SqlDataSource>
<asp:SqlDataSource ID="ReportsToList" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="Select EmployeeID,LastName+','+FirstName as Name From Employees Union Select Null,'无' Order by Name ASC">
</asp:SqlDataSource>
</form>
<form id="form1" runat="server">
<div style="text-align: center">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" DataSourceID="EmployeeList">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="EmployeeID" HeaderText="雇员ID号" />
<asp:BoundField DataField="Name" HeaderText="雇员姓名" />
<asp:TemplateField HeaderText="BirthDate出生日期" SortExpression="BirthDate">
<EditItemTemplate>
<asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"
BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
ForeColor="#663399" Height="200px" SelectedDate='<%# Bind("BirthDate") %>' ShowGridLines="True"
VisibleDate='<%# Eval("BirthDate") %>' Width="220px">
<SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
<SelectorStyle BackColor="#FFCC66" />
<OtherMonthDayStyle ForeColor="#CC9966" />
<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
</asp:Calendar>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("BirthDate", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="上司">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ReportsToList"
DataTextField="Name" DataValueField="EmployeeID" SelectedValue='<%# Bind("Commander") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# GetReportsToName(Eval("Commander")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</div>
<asp:SqlDataSource ID="EmployeeList" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT EmployeeID, LastName + ',' + FirstName AS Name, BirthDate, ReportsTo AS Commander FROM Employees">
</asp:SqlDataSource>
<asp:SqlDataSource ID="ReportsToList" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="Select EmployeeID,LastName+','+FirstName as Name From Employees Union Select Null,'无' Order by Name ASC">
</asp:SqlDataSource>
</form>
//绑定第一个模板列的Label1控件,将EmployeeID转换为姓名
public string GetReportsToName(object reportsToId)
{
foreach (DataRowView row in ReportsToList.Select(DataSourceSelectArguments.Empty))
{
if (reportsToId.Equals(row["EmployeeID"]))
{
return row["Name"].ToString();
}
}
throw new ArgumentException();
}
public string GetReportsToName(object reportsToId)
{
foreach (DataRowView row in ReportsToList.Select(DataSourceSelectArguments.Empty))
{
if (reportsToId.Equals(row["EmployeeID"]))
{
return row["Name"].ToString();
}
}
throw new ArgumentException();
}