上一篇文章的GridView表头和表尾都是固定死的,这样灵活性很差,现在我们来根据数据库动态创建GridView表头和表尾。
前台代码:这里的AutoGenerateColumns属性设置为true,是为了响应后台的sgvCapitalDetailList_RowCreated事件的,因为前台没有设置绑定字段的话将无法触发此事件。
<DtscCommonControl:SmartGridView ID="sgvCapitalDetailList" runat="server" CssClass="gridview"
Width="100%" AutoGenerateColumns="true" OnSorting="sgvCapitalDetailList_Sorting"
MapTree="" AllowSorting="True" onrowcreated="sgvCapitalDetailList_RowCreated">
<OptionTree IsTree="False" OpenImg="" ShutImg="" LeafImg="" LastOpenImg="" LastShutImg="" LastLeafImg=""
VertLineImg="" BlankImg="" HeadRowCount="1" Collapse="False" DeepNode="2" ColumnIndex="0"
Striped="False" HighLight="False" State="False"></OptionTree>
<EmptyDataTemplate>
<dtsc:EmptyData ID="ep1" runat="server" Text="没有此类数据" />
</EmptyDataTemplate>
<HeaderStyle HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
</DtscCommonControl:SmartGridView>
后台代码如下:
#region 方法
/// <summary>
/// 绑定数据列表
/// </summary>
private void BindList()
{
string[] parms = new string[3];
parms[0] = this.ddlYear.SelectedValue;
parms[1] = StrSortExpression;
parms[2] = StrSortDirection;
jgItemCount = BizPojectStatPartial.GetJGItemCount();
xlItemCount = BizPojectStatPartial.GetXLItemCount();
dt=BizPojectStatPartial.GetCaptitalPlanDetail(parms);
#region 生成一张新的数据表
newTable=new DataTable();
DataRow dr=null;
DataColumn dc=null;
for (int i = 0; i < dt.Columns.Count; i++)
{
if (i == 1)
{
dc = new DataColumn("合计");
newTable.Columns.Add(dc);
dc = new DataColumn("小计 ");
newTable.Columns.Add(dc);
}
if (i == jgItemCount)
{
dc = new DataColumn("小计");
newTable.Columns.Add(dc);
}
dc=new DataColumn(dt.Columns[i].Caption);
newTable.Columns.Add(dc);
}
for (int k = 0; k < dt.Rows.Count; k++)
{
dr = newTable.NewRow();
allOrganJGCaptital = 0;
allOrganXLCaptital = 0;
for (int f = 0; f < dt.Columns.Count; f++)
{
if (f>0&f <= jgItemCount)
{
allOrganJGCaptital += TransStringToDecimal(dt.Rows[k][f].ToString());
}
else if(f>jgItemCount&&f<=jgItemCount+xlItemCount)
{
allOrganXLCaptital += TransStringToDecimal(dt.Rows[k][f].ToString());
}
allOrganCaptital = allOrganJGCaptital + allOrganXLCaptital;
if (f == 0)
{
dr[0] = dt.Rows[k][0];
}
else if (f > 0 && f <=jgItemCount)
{
dr[f + 2] = dt.Rows[k][f];
}
else if (f >jgItemCount && f <= jgItemCount + xlItemCount)
{
dr[f + 3] = dt.Rows[k][f];
}
}
dr[1] = allOrganCaptital;
dr[2] = allOrganJGCaptital;
dr[jgItemCount + 3] = allOrganXLCaptital;
newTable.Rows.Add(dr);
}
#endregion
sgvCapitalDetailList.DataSource = newTable;
sgvCapitalDetailList.DataBind();
}
/// <summary>
/// 将字符串类型转换为decimal类型
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private decimal TransStringToDecimal(string str)
{
return string.IsNullOrEmpty(str) ? 0 : decimal.Parse(str);
}
/// <summary>
/// 重建表尾
/// </summary>
/// <param name="tcFooter">表尾单元格集合</param>
void ReBuildFooter(TableCellCollection tcFooter,DataTable dt)
{
if (dt != null)
{
int cells = dt.Columns.Count;
tcFooter.Clear();
GridViewRow rowFooter = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
rowFooter.BorderWidth = 1;
rowFooter.Attributes.Add("class", "yy_sgv_fixRow1");
rowFooter.Attributes.Add("style", "z-index:1000;background-color:#eafbc9;;font-family:宋体;font-weight:bold;border:1px #719626 solid;");
TableCell footerCell = new TableCell();
footerCell.Text = "总计";
footerCell.HorizontalAlign = HorizontalAlign.Center;
footerCell.Wrap = false;
rowFooter.Cells.Add(footerCell);
decimal[] totals = new decimal[dt.Columns.Count - 1]; //用一个数组来存取每一列的总计值
for (int j = 0; j < dt.Rows.Count; j++)
{
for (int i = 1; i < cells; i++) //从第二列开始,因为第一列是 “总计”标题
{
totals[i - 1] += decimal.Parse(dt.Rows[j][i].ToString());
}
}
//生成末尾统计行中的表格
for (int n = 1; n < dt.Columns.Count; n++)
{
footerCell = new TableCell();
footerCell.Text = totals[n - 1].ToString();
footerCell.HorizontalAlign = HorizontalAlign.Center;
footerCell.Wrap = false;
rowFooter.Cells.Add(footerCell);
}
sgvCapitalDetailList.Controls[0].Controls.AddAt(dt.Rows.Count + 3, rowFooter);
}
}
/// <summary>
/// 重建表头
/// </summary>
/// <param name="tcHeader">表头单元格</param>
void RebuildHeader(TableCellCollection tcHeader, DataTable dt)
{
if (dt != null)
{
tcHeader.Clear();
int cells = dt.Columns.Count;
#region 第一行
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
rowHeader.Attributes.Add("class", "f_sgv_fixRow");
rowHeader.Attributes.Add("style", "z-index:1001;background-color:#d2e8ff;color:#2965a2;font-family:宋体;font-weight:bold;border:1px solid;");
rowHeader.BorderWidth = 1;
TableCell headerCell = new TableCell();
headerCell.Text = "企业名称";
headerCell.RowSpan = 2;
headerCell.HorizontalAlign = HorizontalAlign.Center;
headerCell.Attributes.Add("style", "vertical-align:middle;");
headerCell.Wrap = false;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "合计";
headerCell.RowSpan = 2;
headerCell.HorizontalAlign = HorizontalAlign.Center;
headerCell.Attributes.Add("style", "vertical-align:middle;");
headerCell.Wrap = false;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "技改";
headerCell.ColumnSpan = jgItemCount + 1;
headerCell.HorizontalAlign = HorizontalAlign.Center;
headerCell.Wrap = false;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "修理";
headerCell.ColumnSpan = xlItemCount + 1;
headerCell.HorizontalAlign = HorizontalAlign.Center;
headerCell.Wrap = false;
rowHeader.Cells.Add(headerCell);
#endregion
#region 第二行
GridViewRow secondRowHeader = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Normal);
secondRowHeader.Attributes.Add("class", "s_sgv_fixRow");
secondRowHeader.Attributes.Add("style", "z-index:1000;background-color:#d2e8ff;color:#2965a2;font-family:宋体;font-weight:bold;");
secondRowHeader.BorderWidth = 1;
headerCell = new TableCell();
headerCell.Text = "小计";
headerCell.HorizontalAlign = HorizontalAlign.Center;
headerCell.Wrap = false;
secondRowHeader.Cells.Add(headerCell);
for (int i = 1; i < cells; i++)
{
headerCell = new TableCell();
headerCell.Text = dt.Columns[i].Caption;
headerCell.HorizontalAlign = HorizontalAlign.Center;
headerCell.Wrap = false;
secondRowHeader.Cells.Add(headerCell);
if (i == jgItemCount)
{
headerCell = new TableCell();
headerCell.Text = "小计";
headerCell.HorizontalAlign = HorizontalAlign.Center;
headerCell.Wrap = false;
secondRowHeader.Cells.Add(headerCell);
}
}
#endregion
sgvCapitalDetailList.Controls[0].Controls.AddAt(0, rowHeader);
sgvCapitalDetailList.Controls[0].Controls.AddAt(1, secondRowHeader);
}
}
#endregion
接下来在Rowcreated事件中执行:
protected void sgvCapitalDetailList_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
RebuildHeader(e.Row.Cells,dt);
}
if (e.Row.RowType == DataControlRowType.Footer)
{
ReBuildFooter(e.Row.Cells,newTable);
}
}