gridview 合并列值相同的单元格
publicstaticvoidMergeCells(GridViewgv,int[]columnIndices){//指定的列中需要设置RowSpan的单元格的行索引i...
public static void MergeCells(GridView gv, int[] columnIndices)
{
// 指定的列中需要设置RowSpan的单元格的行索引
int[] aryInt = new int[columnIndices.Length];
// 是否重新指定aryInt的相关元素的值 // aryInt中的元素与aryBln中的元素一一对应
bool[] aryBln = new bool[columnIndices.Length]; // aryInt初值均为0
for (int i = 0; i < aryInt.Length; i++)
{
aryInt[i] = 0;
}
// aryBln初值均为true
for (int i = 0; i < aryBln.Length; i++)
{
aryBln[i] = true;
}
for (int i = 1; i < gv.Rows.Count; i++)
{
// 本行和上一行均为DataControlRowType.DataRow
if (gv.Rows[i].RowType == DataControlRowType.DataRow && gv.Rows[i - 1].RowType == DataControlRowType.DataRow)
{ // 遍历指定的列索引
for (int j = 0; j < columnIndices.Length; j++)
{
// 列索引超出范围则不处理
if (columnIndices[j] < 0 || columnIndices[j] > gv.Columns.Count - 1)
{
continue;
}
// 相邻单元格的内容相同
if (gv.Rows[i].Cells[columnIndices[j]].Text == gv.Rows[i - 1].Cells[columnIndices[j]].Text)
{
{
if (aryBln[j])
{
aryInt[j] = i - 1;
}
if (gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan == 0)
{
gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan = 1;
gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan++;
gv.Rows[i].Cells[columnIndices[j]].Visible = false;
aryBln[j] = false;
}
else
{
aryBln[j] = true;
}
}
}
}
}
}
}
这个代码合并的是一列的全部值,我需要,上下值相同的合并,没有相同的不合并 展开
{
// 指定的列中需要设置RowSpan的单元格的行索引
int[] aryInt = new int[columnIndices.Length];
// 是否重新指定aryInt的相关元素的值 // aryInt中的元素与aryBln中的元素一一对应
bool[] aryBln = new bool[columnIndices.Length]; // aryInt初值均为0
for (int i = 0; i < aryInt.Length; i++)
{
aryInt[i] = 0;
}
// aryBln初值均为true
for (int i = 0; i < aryBln.Length; i++)
{
aryBln[i] = true;
}
for (int i = 1; i < gv.Rows.Count; i++)
{
// 本行和上一行均为DataControlRowType.DataRow
if (gv.Rows[i].RowType == DataControlRowType.DataRow && gv.Rows[i - 1].RowType == DataControlRowType.DataRow)
{ // 遍历指定的列索引
for (int j = 0; j < columnIndices.Length; j++)
{
// 列索引超出范围则不处理
if (columnIndices[j] < 0 || columnIndices[j] > gv.Columns.Count - 1)
{
continue;
}
// 相邻单元格的内容相同
if (gv.Rows[i].Cells[columnIndices[j]].Text == gv.Rows[i - 1].Cells[columnIndices[j]].Text)
{
{
if (aryBln[j])
{
aryInt[j] = i - 1;
}
if (gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan == 0)
{
gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan = 1;
gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan++;
gv.Rows[i].Cells[columnIndices[j]].Visible = false;
aryBln[j] = false;
}
else
{
aryBln[j] = true;
}
}
}
}
}
}
}
这个代码合并的是一列的全部值,我需要,上下值相同的合并,没有相同的不合并 展开
4个回答
展开全部
这个是用vs2003的时候写的,现在第三方控件里就有属性合并同样的行了,gridview就没写对应的,你可以把下面的DataGrid1.Items都换成Gridview.Rows用,
private void SpanGrid()
{
int j=DataGrid1.Items.Count;//行数
int u;//自循环所用参数
for (int i = 0;i<j;i++)
{
int inRowSpan = 1;//跨行数
string ItmStr = DataGrid1.Items[i].Cells[0].Text.ToString();
for(u = i+1;u<j;u++)
{
if (ItmStr.Equals(DataGrid1.Items[u].Cells[0].Text.ToString()))
{
inRowSpan += 1;
DataGrid1.Items[i].Cells[0].RowSpan = inRowSpan;//对最歪层所循环行赋要跨行数
DataGrid1.Items[u].Cells[0].Visible = false;//当前被跨行不可见
}
else
{
break;
}
}
i = u - 1;
}
}
好用的话,有没有分加啊??
private void SpanGrid()
{
int j=DataGrid1.Items.Count;//行数
int u;//自循环所用参数
for (int i = 0;i<j;i++)
{
int inRowSpan = 1;//跨行数
string ItmStr = DataGrid1.Items[i].Cells[0].Text.ToString();
for(u = i+1;u<j;u++)
{
if (ItmStr.Equals(DataGrid1.Items[u].Cells[0].Text.ToString()))
{
inRowSpan += 1;
DataGrid1.Items[i].Cells[0].RowSpan = inRowSpan;//对最歪层所循环行赋要跨行数
DataGrid1.Items[u].Cells[0].Visible = false;//当前被跨行不可见
}
else
{
break;
}
}
i = u - 1;
}
}
好用的话,有没有分加啊??
展开全部
//合并GridView中一列的相同行
protected void GroupRows(GridView gv, int cellNum)
{
int i = 0, rowSpanNum = 1;
while (i < gv.Rows.Count - 1)
{
GridViewRow gvr = gv.Rows[i];
for (++i; i < gv.Rows.Count; i++)
{
GridViewRow gvrNext = gv.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 == gv.Rows.Count - 1)
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
}
}
}
}我就用的这个。
protected void GroupRows(GridView gv, int cellNum)
{
int i = 0, rowSpanNum = 1;
while (i < gv.Rows.Count - 1)
{
GridViewRow gvr = gv.Rows[i];
for (++i; i < gv.Rows.Count; i++)
{
GridViewRow gvrNext = gv.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 == gv.Rows.Count - 1)
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
}
}
}
}我就用的这个。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class GridDecorator
{
public static void MergeRows(GridView GridView1)
{
for (int rowIndex = GridView1.Rows.Count-2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = GridView1.Rows[rowIndex];
GridViewRow previousRow = GridView1.Rows[rowIndex +1];
for (int i = 0; i < row.Cells.Count; i++)
{
if (row.Cells[5].Text == previousRow.Cells[5].Text)
{
previousRow.Cells[i].Text = "";
previousRow.Cells[6].Text = "";
previousRow.Cells[7].Text = "";
}
}
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
GridDecorator.MergeRows(GridView1);
}
}
{
public static void MergeRows(GridView GridView1)
{
for (int rowIndex = GridView1.Rows.Count-2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = GridView1.Rows[rowIndex];
GridViewRow previousRow = GridView1.Rows[rowIndex +1];
for (int i = 0; i < row.Cells.Count; i++)
{
if (row.Cells[5].Text == previousRow.Cells[5].Text)
{
previousRow.Cells[i].Text = "";
previousRow.Cells[6].Text = "";
previousRow.Cells[7].Text = "";
}
}
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
GridDecorator.MergeRows(GridView1);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这个有点复杂哦 第一 分少 第二说得不清楚 很难办
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询