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");
}
}
... {
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 >
< 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;
}
... {
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 >
< 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 >