asp.net使用控件datagrid实现表头单元格合并

时间:2021-11-05 13:19:38

合并的要点:

1.datagid的单元格合并原理是table中tr,td的布局实现;

2.合并的时机实在其datagridcreate事件中实现;

3.认识一个对象TableCellCollection,它是由TableCell组成的集合,TableCell可以看成一个标题.

实现下面的效果;

asp.net使用控件datagrid实现表头单元格合并

前台只有一个空DataGrid,后台源码如下:

 private DataTable dt = null;
protected void Page_Load(object sender, EventArgs e)
{
//数据源
dt = new DataTable();
dt.Columns.Add("姓名");
dt.Columns.Add("年龄");
dt.Columns.Add("住址");
dt.Columns.Add("公司");
dt.Columns.Add("邮件");
dt.Columns.Add("电话"); //绑定数据源
mydg.DataSource = dt;
mydg.DataBind(); } protected void mydg_ItemCreated(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
List<CellRule> ils = new List<CellRule>(); TableCellCollection tcc = e.Item.Cells;
tcc.Add(new TableCell());
tcc[].Text = dt.Columns[].ColumnName;
tcc[].Attributes.Add("rowspan", "");
tcc.Add(new TableCell());
tcc[].Text = dt.Columns[].ColumnName;
tcc[].Attributes.Add("rowspan", "");
tcc.Add(new TableCell());
tcc[].Attributes.Add("colspan", "");
tcc[].Text = "详细信息</th></tr><tr>";
tcc[].HorizontalAlign = HorizontalAlign.Center; tcc.Add(new TableCell());
tcc[].Text = dt.Columns[].ColumnName;
tcc.Add(new TableCell());
tcc[].Text = dt.Columns[].ColumnName;
tcc.Add(new TableCell());
tcc[].Text = dt.Columns[].ColumnName;
tcc.Add(new TableCell());
tcc[].Text = dt.Columns[].ColumnName; //ils.Add(new CellRule(2, 4, "详细信息"));
//OganizeTcc(e.Item.Cells, ils, dt);
}
}

再将合并表头单元格封装成通用的方法:

优化后的代码如下:

 private DataTable dt = null;
protected void Page_Load(object sender, EventArgs e)
{
//数据源
dt = new DataTable();
dt.Columns.Add("姓名");
dt.Columns.Add("年龄");
dt.Columns.Add("住址");
dt.Columns.Add("公司");
dt.Columns.Add("邮件");
dt.Columns.Add("电话"); //绑定数据源
mydg.DataSource = dt;
mydg.DataBind(); } protected void mydg_ItemCreated(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
List<CellRule> ils = new List<CellRule>(); //TableCellCollection tcc = e.Item.Cells;
//tcc.Add(new TableCell());
//tcc[0].Text = dt.Columns[0].ColumnName;
//tcc[0].Attributes.Add("rowspan", "2");
//tcc.Add(new TableCell());
//tcc[1].Text = dt.Columns[1].ColumnName;
//tcc[1].Attributes.Add("rowspan", "2");
//tcc.Add(new TableCell());
//tcc[2].Attributes.Add("colspan", "4");
//tcc[2].Text = "详细信息</th></tr><tr>";
//tcc[2].HorizontalAlign = HorizontalAlign.Center; //tcc.Add(new TableCell());
//tcc[3].Text = dt.Columns[2].ColumnName;
//tcc.Add(new TableCell());
//tcc[4].Text = dt.Columns[3].ColumnName;
//tcc.Add(new TableCell());
//tcc[5].Text = dt.Columns[4].ColumnName;
//tcc.Add(new TableCell());
//tcc[6].Text = dt.Columns[5].ColumnName; ils.Add(new CellRule(, , "详细信息"));
OganizeTcc(e.Item.Cells, ils, dt);
}
} private void OganizeTcc(TableCellCollection tcc, List<CellRule> ils, DataTable dt)
{
tcc.Clear();
ils = ils.OrderBy(p => p.iStart).ToList();
List<string> secHeaders = new List<string>();
//第一行
for (int i = , j = ; j < dt.Columns.Count;)
{
if (i >= ils.Count || ils[i].iStart != j)
{
tcc.Add(new TableCell());
tcc[tcc.Count - ].Text = dt.Columns[j].ColumnName;
tcc[tcc.Count - ].Attributes.Add("rowspan", "");
j++;
if (j == dt.Columns.Count)
tcc[tcc.Count - ].Text = tcc[tcc.Count - ].Text + "</th></tr><tr>";
}
else
{
for (int iSec = ; iSec < ils[i].iCount; iSec++)
{
secHeaders.Add(dt.Columns[j + iSec].ColumnName);
}
tcc.Add(new TableCell());
tcc[tcc.Count-].Text = ils[i].strHeaderText;
tcc[tcc.Count - ].Attributes.Add("colspan", ils[i].iCount.ToString());
tcc[tcc.Count - ].HorizontalAlign = HorizontalAlign.Center;
j = j + ils[i].iCount;
i++;
if(j == dt.Columns.Count)
tcc[tcc.Count-].Text = tcc[tcc.Count-].Text+"</th></tr><tr>";
} }
//第二行
foreach (string str in secHeaders)
{
tcc.Add(new TableCell());
tcc[tcc.Count - ].Text = str;
}
}

设定合并规则的实体类:

 public class CellRule
{
public int iStart { get; set; }
public int iCount { get; set; }
public string strHeaderText { get; set; }
public CellRule(int iStart, int iCount, string strHeaderText)
{
this.iStart = iStart;
this.iCount = iCount;
this.strHeaderText = strHeaderText;
}
}

现在只有一阶转二阶的方法,如果在搞成N阶通用的方法,我是暂时想不到了.就这样.