dataGrid 多维表头、表头跨行跨列设计及绑定数据
程序员文章站
2024-03-05 08:47:36
datagrid 其实就是一个html table 想清楚这个以后,要设置多维表头就好办了 html代码 复制代码 代码如下:
datagrid 其实就是一个html table
想清楚这个以后,要设置多维表头就好办了
html代码
<asp:datagrid id="datagrid1" runat="server"
onitemdatabound="datagrid1_itemdatabound">
</asp:datagrid>
然后绑定数据
protected void page_load(object sender, eventargs e)
{
string strsql = "select empid, name, branchid, loginid, pwd, sex, empcode, email, officetel from mrbaseinf";
sqlconnection con = new sqlconnection("server=.;database=ioffice2009;uid=sa;pwd=sa");
dataset ds = new dataset();
sqldataadapter ter = new sqldataadapter(strsql, con);
con.open();
ter.fill(ds);
con.close();
this.datagrid1.datasource = ds;
datagrid1.databind();
}
接下来添加datagrid1_itemdataboun事件
protected void datagrid1_itemdatabound(object sender, datagriditemeventargs e)
{
if (e.item.itemtype==listitemtype.header)
{
e.item.cells[0].rowspan = 2;
e.item.cells[1].rowspan = 2;
e.item.cells[2].rowspan = 2;
e.item.cells[3].rowspan = 2;
e.item.cells[4].rowspan = 2;
e.item.cells[5].columnspan = 4;
e.item.cells[5].horizontalalign = horizontalalign.center;
e.item.cells[5].text = "测试</td></tr><tr><td>列1</td><td>列2</td><td>列3</td><td>列4</td></tr>";
e.item.cells[6].visible = false;
e.item.cells[7].visible = false;
e.item.cells[8].visible = false;
}
}
效果图
想清楚这个以后,要设置多维表头就好办了
html代码
复制代码 代码如下:
<asp:datagrid id="datagrid1" runat="server"
onitemdatabound="datagrid1_itemdatabound">
</asp:datagrid>
然后绑定数据
复制代码 代码如下:
protected void page_load(object sender, eventargs e)
{
string strsql = "select empid, name, branchid, loginid, pwd, sex, empcode, email, officetel from mrbaseinf";
sqlconnection con = new sqlconnection("server=.;database=ioffice2009;uid=sa;pwd=sa");
dataset ds = new dataset();
sqldataadapter ter = new sqldataadapter(strsql, con);
con.open();
ter.fill(ds);
con.close();
this.datagrid1.datasource = ds;
datagrid1.databind();
}
接下来添加datagrid1_itemdataboun事件
复制代码 代码如下:
protected void datagrid1_itemdatabound(object sender, datagriditemeventargs e)
{
if (e.item.itemtype==listitemtype.header)
{
e.item.cells[0].rowspan = 2;
e.item.cells[1].rowspan = 2;
e.item.cells[2].rowspan = 2;
e.item.cells[3].rowspan = 2;
e.item.cells[4].rowspan = 2;
e.item.cells[5].columnspan = 4;
e.item.cells[5].horizontalalign = horizontalalign.center;
e.item.cells[5].text = "测试</td></tr><tr><td>列1</td><td>列2</td><td>列3</td><td>列4</td></tr>";
e.item.cells[6].visible = false;
e.item.cells[7].visible = false;
e.item.cells[8].visible = false;
}
}
效果图
上一篇: 再谈java回调函数
下一篇: C#基础之数据类型转换