动态修改DataGrid中列的呈现
修改DataGrid显示列的呈现大体有四种方法,下面以"经典"的性别显示为例介绍这几种方法.
1>绑定的时候修改列的呈现之前台实现.
CodeBehind:
==================================================================
/// <summary>
/// Fetch data from db.
/// </summary>
private void GetData()
{
SqlConnection cnn=new SqlConnection("data source=localhost;initial catalog=pfizer;integrated
security=sspi");
SqlCommand cmd=new SqlCommand("select seqid,employeename, malefemale from pub_employee where
seqid>10",cnn);
SqlDataAdapter adapter=new SqlDataAdapter(cmd);
DataSet dst=new DataSet();
try
{
adapter.Fill(dst);
DataGrid1.DataSource=dst.Tables[0].DefaultView;
DataGrid1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
aspx:
==========================================================================
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="sharptest.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 24px"
runat="server"
Width="272px" AutoGenerateColumns=False ShowHeader=True>
<Columns>
<asp:BoundColumn DataField="seqid" HeaderText="编号"></asp:BoundColumn>
<asp:BoundColumn DataField="employeename" HeaderText="姓名"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="性别">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"malefemale").ToString()=="F"?"男":"女"%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>
这里用到是c系列的三目运算符 == ? :,如果你用vb.net可以用 IIf(testexpression,truepart,falsepart) 实现 .
2>绑定的时候修改列的呈现之自定义函数实现
CodeBehind:
==================================================================
后台代码和1>相同
aspx:
==================================================================
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 24px" runat="server"
Width="272px" AutoGenerateColumns=False ShowHeader=True>
<Columns>
<asp:BoundColumn DataField="seqid" HeaderText="编号"></asp:BoundColumn>
<asp:BoundColumn DataField="employeename" HeaderText="姓名"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="性别">
<ItemTemplate>
<%#ShowSex(DataBinder.Eval(Container.DataItem,"malefemale"))%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
3>绑定的时候修改列的呈现ItemDataBound事件实现
CodeBehind:
==================================================================
绑定函数同1>相同,加上下面的处理过程.
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.EditItem)
{
if(DataBinder.Eval(e.Item.DataItem,"malefemale").ToString()=="F")
e.Item.Cells[2].Text="男";
else
e.Item.Cells[2].Text="女";
}
}
aspx:
==================================================================
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 24px" runat="server"
Width="272px" AutoGenerateColumns="False" ShowHeader="True">
<Columns>
<asp:BoundColumn DataField="seqid" HeaderText="编号"></asp:BoundColumn>
<asp:BoundColumn DataField="employeename" HeaderText="姓名"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="性别">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"malefemale")%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
4>在sql语句中修改列的呈现
CodeBehind:
==================================================================
private void GetData()
{
SqlConnection cnn=new SqlConnection("data source=localhost;initial catalog=pfizer;integrated
security=sspi");
SqlCommand cmd=new SqlCommand("select seqid,employeename, case malefemale when 'F' then '男' else '女
' end as malefemale from pub_employee where seqid>10",cnn);
SqlDataAdapter adapter=new SqlDataAdapter(cmd);
DataSet dst=new DataSet();
try
{
adapter.Fill(dst);
DataGrid1.DataSource=dst.Tables[0].DefaultView;
DataGrid1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
主要是用了sql里的case语句.
aspx:
==================================================================
同 3>.
结束语:
一样每种方法都有各自的优势.方法二看起来比较垃圾,但是如果你要里面实现比较复杂的逻辑它会排上用场,绝对比其他的用起来要爽的多.
方法三你可以随意扩展不只是显示,可以更改北京,可以实现内嵌下来列表默认选择的实现等.
对于这几种方法完全适用于"字符串太长用省略号显示"之类的问题,大家可以自由发挥!