GridView合并单元格(所有内容相同的单元格)

 源码:

1、 代码
 
   
public static void GroupRow(GridView gridView)
{
for ( int rowIndex = gridView.Rows.Count - 2 ; rowIndex >= 0 ; rowIndex -- )
{
GridViewRow row
= gridView.Rows[rowIndex];
GridViewRow previousRow
= gridView.Rows[rowIndex + 1 ];

for ( int i = 0 ; i < row.Cells.Count; i ++ )
{
if (row.Cells[i].Text == previousRow.Cells[i].Text)
{
row.Cells[i].RowSpan
= previousRow.Cells[i].RowSpan < 2 ? 2 :
previousRow.Cells[i].RowSpan
+ 1 ;
previousRow.Cells[i].Visible
= false ;
}
}
}
}

 

2、 代码
 
   
// cellNum 表示需要合并的列,索引从0开始
public static void GroupRows(GridView GridView1, int cellNum)
{
int i = 0 , rowSpanNum = 1 ;
while (i < GridView1.Rows.Count - 1 )
{
GridViewRow gvr
= GridView1.Rows[i];
for ( ++ i; i < GridView1.Rows.Count; i ++ )
{
GridViewRow gvrNext
= GridView1.Rows[i];
if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
{
gvrNext.Cells[cellNum].Visible
= false ;
rowSpanNum
++ ;
}
else
{
gvr.Cells[cellNum].RowSpan
= rowSpanNum;
rowSpanNum
= 1 ;
break ;
}

if (i == GridView1.Rows.Count - 1 )
{
gvr.Cells[cellNum].RowSpan
= rowSpanNum;
}
}
}
}

 

3、 两张表合并成一张
 
   
private DataTable HBTable(DataTable dt1, DataTable dt2)
{
DataTable newDataTable
= dt1.Clone();

object [] obj = new object [newDataTable.Columns.Count];
for ( int i = 0 ; i < dt1.Rows.Count; i ++ )
{
dt1.Rows[i].ItemArray.CopyTo(obj,
0 );
newDataTable.Rows.Add(obj);
}

for ( int i = 0 ; i < dt2.Rows.Count; i ++ )
{
dt2.Rows[i].ItemArray.CopyTo(obj,
0 );
newDataTable.Rows.Add(obj);
}
return newDataTable;


}

 

4、 代码
 
   
protected void Unite(GridView gv)
{
int i;
string LastType1;
int LastCell;
if (gv.Rows.Count > 0 )
{
for ( int j = 0 ; j < 10 ; j ++ )
{
LastType1
= gv.Rows[ 0 ].Cells[j].Text;
gv.Rows[
0 ].Cells[j].RowSpan = 1 ;
LastCell
= 0 ;

if (j != 3 && j != 4 )
{
for (i = 1 ; i < gv.Rows.Count; i ++ )
{
if (gv.Rows[i].Cells[j].Text == LastType1)
{
gv.Rows[i].Cells[j].Visible
= false ;
gv.Rows[LastCell].Cells[j].RowSpan
++ ;
}
else
{
LastType1
= gv.Rows[i].Cells[j].Text;
LastCell
= i;
gv.Rows[i].Cells[j].RowSpan
= 1 ;
}
}
}
}
}
}

5、 将两张表拼接成一张表
 
   
public DataSet MergeTable(DataTable tab1, DataTable tab2)
{
DataTable table
= new DataTable();
if (tab1.Rows.Count != 0 && tab2.Rows.Count != 0 )
{
DataColumn[] columns
= new DataColumn[tab1.Columns.Count + tab2.Columns.Count];
tab1.Columns.CopyTo(columns,
0 );
tab2.Columns.CopyTo(columns, tab1.Columns.Count);
foreach (DataColumn column in columns)
{
table.Columns.Add(column.ColumnName);
}
int count = tab1.Rows.Count > tab2.Rows.Count ? tab1.Rows.Count : tab2.Rows.Count;
for ( int i = 0 ; i < count; i ++ )
{
if (tab1.Rows.Count <= i)
{
break ;
}
DataRow row
= table.NewRow();
for ( int m = 0 ; m < tab1.Columns.Count; m ++ )
{
DataRow item
= tab1.Rows[i];
row[tab1.Columns[m].ToString()]
= item[tab1.Columns[m].ToString()];
}
for ( int n = 0 ; n < tab2.Columns.Count; n ++ )
{
if (tab2.Rows.Count <= i)
{
break ;
}
DataRow item
= tab2.Rows[i];
row[tab2.Columns[n].ToString()]
= item[tab2.Columns[n].ToString()];
}
table.Rows.Add(row);
}
}
else
{
table
= tab1.Rows.Count == 0 ? tab2.Copy() : tab1.Copy();
}
DataSet ds
= new DataSet();
ds.Tables.Add(table);
return ds;
}

 

 

 

 

转载于:https://www.cnblogs.com/axing/archive/2010/11/09/GridView.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值