2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > ASP.NET 2.0中合并 GridView 的表头单元格

ASP.NET 2.0中合并 GridView 的表头单元格

时间:2023-09-14 21:30:19

相关推荐

ASP.NET 2.0中合并 GridView 的表头单元格

在实际工作中,往往需要合并表格头部的单元格,下面就是一个实现的例子。运行结果如图:

C#

<%...@PageLanguage="C#"AutoEventWireup="true"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"

"/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<scriptrunat="server">...

ICollectionCreateDataSource()

...{

System.Data.DataTabledt=newSystem.Data.DataTable();

System.Data.DataRowdr;

dt.Columns.Add(newSystem.Data.DataColumn("学生姓名",typeof(System.String)));

dt.Columns.Add(newSystem.Data.DataColumn("语文",typeof(System.Decimal)));

dt.Columns.Add(newSystem.Data.DataColumn("数学",typeof(System.Decimal)));

dt.Columns.Add(newSystem.Data.DataColumn("英语",typeof(System.Decimal)));

for(inti=0;i<8;i++)

...{

System.Randomrd=newSystem.Random(Environment.TickCount*i);;

dr=dt.NewRow();

dr[0]="学生"+i.ToString();

dr[1]=System.Math.Round(rd.NextDouble()*100,2);

dr[2]=System.Math.Round(rd.NextDouble()*100,2);

dr[3]=System.Math.Round(rd.NextDouble()*100,2);

dt.Rows.Add(dr);

}

System.Data.DataViewdv=newSystem.Data.DataView(dt);

returndv;

}

protectedvoidPage_Load(objectsender,EventArgse)

...{

if(!IsPostBack)

...{

GridView1.BorderColor=System.Drawing.Color.DarkOrange;

GridView1.DataSource=CreateDataSource();

GridView1.DataBind();

}

}

protectedvoidGridView1_RowCreated(objectsender,GridViewRowEventArgse)

...{

if(e.Row.RowType==DataControlRowType.Header)

...{

GridViewRowrowHeader=newGridViewRow(0,0,DataControlRowType.Header,DataControlRowState.Normal);

rowHeader.BackColor=System.Drawing.Color.White;

rowHeader.Font.Bold=true;

TableCellCollectioncells=e.Row.Cells;

TableCellheaderCell=newTableCell();

headerCell.Text="";

rowHeader.Cells.Add(headerCell);

headerCell=newTableCell();

headerCell.Text="学生成绩";

headerCell.ColumnSpan=cells.Count-1;

headerCell.HorizontalAlign=HorizontalAlign.Center;

rowHeader.Cells.Add(headerCell);

rowHeader.Visible=true;

GridView1.Controls[0].Controls.AddAt(0,rowHeader);

}

}

protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse)

...{

e.Row.Attributes.Add("style","background:#FFF");

}

</script>

<htmlxmlns="/1999/xhtml">

<head>

<title>合并GridView的表头单元格</title>

</head>

<body>

<formid="Form1"runat="server">

<asp:GridViewID="GridView1"runat="server"CellSpacing="1"CellPadding="3"

Font-Size="12px"Width="300px"BackColor="orange"BorderWidth="0"

OnRowDataBound="GridView1_RowDataBound"OnRowCreated="GridView1_RowCreated">

</asp:GridView>

</form>

</body>

</html>

<%...@PageLanguage="VB"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<scriptrunat="server">...

FunctionCreateDataSource()AsICollection

DimdtAsSystem.Data.DataTable=NewSystem.Data.DataTable

DimdrAsSystem.Data.DataRow

dt.Columns.Add(NewSystem.Data.DataColumn("学生姓名",GetType(System.String)))

dt.Columns.Add(NewSystem.Data.DataColumn("语文",GetType(System.Decimal)))

dt.Columns.Add(NewSystem.Data.DataColumn("数学",GetType(System.Decimal)))

dt.Columns.Add(NewSystem.Data.DataColumn("英语",GetType(System.Decimal)))

DimiAsInteger=0

Fori=0To7

DimrdAsSystem.Random=NewSystem.Random(Environment.TickCount*i)

dr=dt.NewRow

dr(0)="学生"+i.ToString

dr(1)=System.Math.Round(rd.NextDouble*100,2)

dr(2)=System.Math.Round(rd.NextDouble*100,2)

dr(3)=System.Math.Round(rd.NextDouble*100,2)

dt.Rows.Add(dr)

Next

DimdvAsSystem.Data.DataView=NewSystem.Data.DataView(dt)

Returndv

EndFunction

ProtectedSubPage_Load(ByValsenderAsObject,ByValeAsEventArgs)

IfNotIsPostBackThen

GridView1.BorderColor=System.Drawing.Color.DarkOrange

GridView1.DataSource=CreateDataSource()

GridView1.DataBind()

EndIf

EndSub

ProtectedSubGridView1_RowCreated(ByValsenderAsObject,ByValeAsGridViewRowEventArgs)

Ife.Row.RowType=DataControlRowType.HeaderThen

DimrowHeaderAsGridViewRow=NewGridViewRow(0,0,DataControlRowType.Header,DataControlRowState.Normal)

rowHeader.BackColor=System.Drawing.Color.White

rowHeader.Font.Bold=True

DimcellsAsTableCellCollection=e.Row.Cells

DimheaderCellAsTableCell=NewTableCell

headerCell.Text=""

rowHeader.Cells.Add(headerCell)

headerCell=NewTableCell

headerCell.Text="学生成绩"

headerCell.ColumnSpan=cells.Count-1

headerCell.HorizontalAlign=HorizontalAlign.Center

rowHeader.Cells.Add(headerCell)

rowHeader.Visible=True

GridView1.Controls(0).Controls.AddAt(0,rowHeader)

EndIf

EndSub

ProtectedSubGridView1_RowDataBound(ByValsenderAsObject,ByValeAsGridViewRowEventArgs)

e.Row.Attributes.Add("style","background:#FFF")

EndSub

</script>

<htmlxmlns="/1999/xhtml">

<head>

<title>合并GridView的表头单元格</title>

</head>

<body>

<formid="Form1"runat="server">

<asp:GridViewID="GridView1"runat="server"CellSpacing="1"CellPadding="3"

Font-Size="12px"Width="300px"BackColor="orange"BorderWidth="0"

OnRowDataBound="GridView1_RowDataBound"OnRowCreated="GridView1_RowCreated">

</asp:GridView>

</form>

</body>

</html>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。