源码:
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 ;
}
}
}
}
{
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;
}
}
}
}
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;
}
{
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 ;
}
}
}
}
}
}
{
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;
}
{
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;
}