DataGrid应用技巧两则(downmoon)
一:增加求和列:
private void dgMeets_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType== ListItemType.Item || e.Item.ItemType== ListItemType.Item)
{
//某列总和
intTotalNew=0;
int i;
DataTable myTable;
myTable = (DataTable) ((DataSet)this.dgMeets.DataSource).Tables[0];
for(i = 0;i < myTable.Rows.Count;i++)
{
intTotalNew+=int.Parse(myTable.Rows[i]["attendnum"].ToString());
}
}
else if (e.Item.ItemType== ListItemType.Footer)
{
e.Item.Cells[6].Text = "总计:" + intTotalNew.ToString()+"人";
e.Item.Cells[6].Attributes.Add("Align","Right");
}
}
二:列字段转换:
方法1:
<asp:TemplateColumn HeaderText="是否可见">
<HeaderStyle HorizontalAlign="Center" Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:Label runat="server" ID="lb" Text='<%# ((DataBinder.Eval(Container, "DataItem.Conf_show", "{0}"))=="0")?"是":"<font color=red>否</font>" %>'> ' ></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
方法2:
后台:
public string ConvertToBool(string k)
{
string s = (k=="0")?"是":"<font color=red>否</font>";
return s;
}
前台:
<asp:TemplateColumn HeaderText="是否可见">
<HeaderStyle HorizontalAlign="Center" Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:Label runat="server" ID="lb" Text='<%# ConvertToBool(DataBinder.Eval(Container, "DataItem.Conf_show", "{0}")) %>'> ' ></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
来源:博远电子(软件定制),如涉及版权问题请与我们联系。
TAG
软件定制,软件开发,瀚森HANSEN,辽宁,沈阳,抚顺