zoukankan
html css js c++ java
GridView合并行代码
图:
Code
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(
!
IsPostBack)
{
DataTable oDataSource
=
CreateTable();
DataView oDV
=
oDataSource.DefaultView;
oDV.Sort
=
"
StudentNo
"
;
GvStudent.DataSource
=
oDV;
GvStudent.DataBind();
}
}
private
DataTable CreateTable()
{
DataTable oDT
=
new
DataTable();
oDT.Columns.Add(
"
StudentNo
"
);
oDT.Columns[
0
].DataType
=
typeof
(System.Int32);
DataColumn oDC
=
new
DataColumn(
"
StudentName
"
,
typeof
(System.String));
oDT.Columns.Add(oDC);
oDT.Columns.Add(
"
Do1
"
);
oDT.Columns.Add(
"
Do2
"
);
DataRow oDR
=
oDT.NewRow();
oDR[
0
]
=
1001
;
oDR[
"
StudentName
"
]
=
"
小张
"
;
oDR[
"
Do1
"
]
=
"
学国学
"
;
oDR[
"
Do2
"
]
=
"
学英语
"
;
oDT.Rows.Add(oDR);
DataRow oDR1
=
oDT.NewRow();
oDR1[
"
StudentNo
"
]
=
1004
;
oDR1[
1
]
=
"
小李
"
;
oDR1[
2
]
=
"
闲着
"
;
oDR1[
3
]
=
"
继续闲着
"
;
oDT.Rows.Add(oDR1);
DataRow oDR2
=
oDT.NewRow();
oDR2[
0
]
=
1001
;
oDR2[
"
StudentName
"
]
=
"
小张
"
;
oDR2[
"
Do1
"
]
=
"
学英语
"
;
oDR2[
"
Do2
"
]
=
"
学法语
"
;
oDT.Rows.Add(oDR2);
return
oDT;
}
int
iPreStudentNo, iCurrentStudentNo;
int
iPreRowNum;
int
iMergeBegin
=
2
, iMergeEnd
=
3
;
protected
void
GvStudent_RowDataBound(
object
sender, GridViewRowEventArgs e)
{
if
(e.Row.RowType
==
DataControlRowType.DataRow)
{
DataRowView drv
=
e.Row.DataItem
as
DataRowView;
GridViewRow oRow
=
e.Row;
int
.TryParse(drv[
"
StudentNo
"
].ToString(),
out
iCurrentStudentNo);
if
(oRow.RowIndex
>
0
)
{
//
第二条记录才进行是否合并
if
(iCurrentStudentNo
!=
iPreStudentNo)
{
if
(oRow.RowIndex
-
iPreRowNum
>
1
)
{
AddRowSpanForColumns(GvStudent.Rows[iPreRowNum], iMergeBegin, iMergeEnd, oRow.RowIndex
-
iPreRowNum);
}
iPreStudentNo
=
iCurrentStudentNo;
iPreRowNum
=
oRow.RowIndex;
}
else
{
RemoveColumnsFromRow(oRow, iMergeBegin, iMergeEnd);
}
}
else
{
//
第一条记录
iPreStudentNo
=
iCurrentStudentNo;
iPreRowNum
=
oRow.RowIndex;
}
}
}
Combination Columns
#region
Combination Columns
private
void
AddRowSpanForColumns(GridViewRow oRow,
int
iIndexOfColumnBegin,
int
iIndexOfColumnEnd,
int
iRowspan)
{
//
给上一个相同的加上Span
for
(
int
i
=
0
; i
<
oRow.Cells.Count; i
++
)
{
if
(i
<
iIndexOfColumnBegin
||
i
>
iIndexOfColumnEnd)
{
oRow.Cells[i].RowSpan
=
iRowspan;
}
}
}
/**/
///
<summary>
///
移除列,进行合并
///
</summary>
///
<param name="oRow"></param>
///
<param name="iIndexOfColumnBegin"></param>
///
<param name="iIndexOfColumnEnd"></param>
private
void
RemoveColumnsFromRow(TableRow oRow,
int
iIndexOfColumnBegin,
int
iIndexOfColumnEnd)
{
for
(
int
i
=
0
; i
<
oRow.Cells.Count; i
++
)
{
if
(i
<
iIndexOfColumnBegin
||
i
>
iIndexOfColumnEnd)
{
oRow.Cells.RemoveAt(i);
i
--
;
iIndexOfColumnBegin
--
;
iIndexOfColumnEnd
--
;
}
}
}
#endregion
查看全文
相关阅读:
浅谈IIS处理Asp.net请求和 Asp.net页面生命周期
Windows Phone 7与Android和iPhone的比较
ExtJs开发需要引用的Js库文件
Windows phone开发(1)一些基本概念
synthesize
Objective C 基础教程
Objective C基础教程笔记
ObjectiveC语法与Cocoa框架
Handing basic Interactio IOS notes
Learning ObjectiveC and Xcode
原文地址:https://www.cnblogs.com/chinabc/p/1592952.html
最新文章
[mave] mave3的安装与部署
Data Access Applicatiion Block snippets
使用Data Access Application Block遇到的问题
The Data Mapping Application Block Preview 的下载地址
随笔记录
解决WEB调试的受限制和未能创建Mutex的问题
区分依赖,关联和聚集的关系(转)
开张拉
背熟以下文章,就能脱离鼠标,光用键盘操作了
红烧肉的做法详细介绍
热门文章
regsvr32命令,希望大家不断补充完善。
IT 2010年终回顾
编写高性能 Web 应用程序的 10 个技巧
html动态添加删除保存
舞蹈视频
敏捷开发
IT 培训参考
Windows Phone 手机号码归属地Demo
ASP.NET网站性能优化需要考虑的方面
反射学习笔记
Copyright © 2011-2022 走看看