GridView跨行合并单元格怎么用?
请问下面的代码在CS文件里面怎么用啊?/// <summary>
///GridViewCellCombine 的摘要说明
/// </summary>
public class GridViewCellCombine
{
/// <summary>
/// 目标GridView
/// </summary>
GridView _gridView;
/// <summary>
/// 上一行单元格集
/// </summary>
TableCell[] _cells;
/// <summary>
/// 要合并单元格的列索引集合
/// </summary>
int[] _colIndexes;
/// <summary>
/// 单元格合并相等判断条件
/// </summary>
ICellEquality[] _colCellEquality;
/// <summary>
/// GridView单元格合并辅助类
/// </summary>
/// <param name="gridView">目标GridView</param>
/// <param name="colIndexes">要合并的列的索引号</param>
public GridViewCellCombine(GridView gridView, int[] colIndexes, ICellEquality[] colCellEquality)
{
if (gridView == null)
throw new Exception("GridView不能为空");
this._gridView = gridView;
_gridView.RowDataBound += new GridViewRowEventHandler(GridViewRowDataBound);
if (colIndexes != null && colCellEquality != null)//在指定了要合并单元格列及单元格比较器时,列数和比较器数应该一样。
{
if (colIndexes.Length != colCellEquality.Length)
{
throw new Exception("请为要合并单元格的每一列都添加单元格比较类");
}
}
if (colIndexes != null)
{
_colIndexes = colIndexes;
_cells = new TableCell[colIndexes.Length];
}
else
{
_cells = new TableCell[this._gridView.Columns.Count];
_colIndexes = Enumerable.Range(0, this._gridView.Columns.Count).ToArray();
}
if (colCellEquality != null)
{
_colCellEquality = colCellEquality;
}
else
_colCellEquality = new ICellEquality[_cells.Length];
}
/// <summary>
/// GridView行绑定事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void GridViewRowDataBound(object sender, GridViewRowEventArgs e)
{
if (_colIndexes != null)
{
for (int i = 0; i < _colIndexes.Length; i++)
{
//如果上一行该列单元格为空,则从当前行的单元格之后进行比较
if (_cells[i] == null)
{
_cells[i] = e.Row.Cells[_colIndexes[i]];//把当前单元格引用暂存用于下一行时比较
continue;
}
//如果当前行第_colIndexes[i]列单元格与上一行单元格相等,合并单元格
if (CellEquals(_cells[i], e.Row.Cells[_colIndexes[i]], _colCellEquality[i]))
{
_cells[i].RowSpan++;
if (_cells[i].RowSpan == 1)//RowSpan属性为0 1时都表示不合并
_cells[i].RowSpan++;
e.Row.Cells[_colIndexes[i]].Visible = false;
}
else
{
_cells[i] = e.Row.Cells[_colIndexes[i]];
}
}
}
}
/// <summary>
/// 获取指定位置的单元格
/// </summary>
/// <param name="rowIndex"></param>
/// <param name="colIndex"></param>
/// <returns></returns>
private TableCell GetCell(int rowIndex, int colIndex)
{
return this._gridView.Rows[rowIndex].Cells[colIndex];
}
/// <summary>
/// 单元格相等比较
/// </summary>
/// <param name="cell1">cell1</param>
/// <param name="cell2">cell2</param>
/// <param name="equality">相等比较器</param>
/// <returns>返回是否相等</returns>
private bool CellEquals(TableCell cell1, TableCell cell2, ICellEquality equality)
{
bool result = false;
if (equality == null)//如果比较器为空采用默认比较器
{
result = DefaultCellEquality.Entity.CellEquals(cell1, cell2);
}
else
{
result = equality.CellEquals(cell1, cell2);
}
return result;
}
/// <summary>
/// 单元格默认比较类
/// </summary>
class DefaultCellEquality : ICellEquality
{
/// <summary>
/// 默认的单元格相等比较器,用单元格内的文字进行比较
/// </summary>
public static DefaultCellEquality Entity = new DefaultCellEquality();
#region ICellEquality 成员
/// <summary>
/// 比较方法
/// </summary>
/// <param name="cell1">cell1</param>
/// <param name="cell2">cell2</param>
/// <returns>是否相等</returns>
public bool CellEquals(TableCell cell1, TableCell cell2)
{
if (cell1 == null || cell2 == null)
return false;
return cell1.Text.Equals(cell2.Text);//根据单元格内容比较
}
#endregion
}
}
/// <summary>
/// 单元格相等比较器
/// </summary>
public interface ICellEquality
{
/// <summary>
/// 比较两个单元格是否相等
/// </summary>
/// <param name="cell1">单元格1</param>
/// <param name="cell2">单元格2</param>
/// <returns>返回是否相等</returns>
bool CellEquals(TableCell cell1, TableCell cell2);
} --------------------编程问答-------------------- Protected Sub GridView2_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowCreated
If e.Row.RowType = DataControlRowType.Header Then
Dim heard As TableCellCollection = e.Row.Cells
heard.Clear()
heard.Add(New TableHeaderCell)
heard(0).Attributes.Add("rowspan", "2")
heard(0).Attributes.Add("bgcolor", "pink")
heard(0).VerticalAlign = VerticalAlign.Middle
heard(0).Text = "项目"
heard.Add(New TableHeaderCell)
heard(1).Attributes.Add("colspan", "2")
heard(1).Attributes.Add("align", "center")
heard(1).Attributes.Add("bgcolor", "silver")
heard(1).Text = "统计数字<th></tr><tr>"
heard.Add(New TableHeaderCell)
heard(2).BackColor = Drawing.Color.Brown
heard(2).Text = "数量"
heard.Add(New TableHeaderCell)
heard(3).BackColor = Drawing.Color.BlueViolet
heard(3).Text = "金额"
End If
End Sub
这是VB的代码,你看一下, --------------------编程问答-------------------- 晕 头昏眼花 repeater 里面放个table 合并table会把! --------------------编程问答-------------------- 如果要特殊效果,还是自己拼接html吧。 --------------------编程问答-------------------- 用repeater啦,里面自己布局 --------------------编程问答-------------------- 传递参数到类中
public static void SpanSingleRow(GridView gView, int GroupColumn, int Compare1)
{
int i = 0;
int j = 0;
int rowSpan;
string strSource = "";
string strTarget = "";
for (i = 0; i < gView.Rows.Count; i++)
{
rowSpan = 1;
strSource = gView.Rows[i].Cells[Compare1].Text;
for (j = i + 1; j < gView.Rows.Count; j++)
{
strTarget = gView.Rows[j].Cells[Compare1].Text;
if (strSource.Length > 0 && strSource != " " && string.Compare(strSource, strTarget) == 0)
{
rowSpan += 1;
gView.Rows[i].Cells[GroupColumn].RowSpan = rowSpan;
gView.Rows[j].Cells[GroupColumn].Visible = false;
}
else
{
break;
}
}
i = j - 1;
}
}
http://topic.csdn.net/u/20091119/21/90e714df-77ca-4f46-8e2e-3c241e16d1a7.html --------------------编程问答-------------------- 请高手指点一下,我用GridView怎么合并单元格,上面说的不是懂,忘具体指点下!加我QQ632359961 --------------------编程问答-------------------- 使用GridView合并单元格,还不如自己来实现HTML放到前端。与其纠结在GridView上,不如放弃GridView --------------------编程问答-------------------- 如果你问“代码怎么用啊”这类问题,我想很难回答。因为你自己都不知道自己需要什么,只是想天上掉馅饼之后自己再去判断自己对它有没有兴趣。如果从自己出发,就应该从手头的设计出发,而不是抄别人的代码。
如果只是问Grid合并代码,那么其实很简单:
//从grd的第rowIndex行colIndex列单元格以下count行合并
public static void 合并行(this GridView grd, int rowIndex, int colIndex, int count)
{
grd.Rows[rowIndex].Cells[colIndex].RowSpan = count;
for (var i = rowIndex + 1; i < rowIndex + count; i++)
grd.Rows[i].Cells[colIndex].Visible = false;
}
//从grd的第rowIndex行colIndex列单元格向右count列合并
public static void 合并列(this GridView grd, int rowIndex, int colIndex, int count)
{
grd.Rows[rowIndex].Cells[colIndex].ColumnSpan = count;
for (var i = 1; i < count; i++)
grd.Rows[rowIndex].Cells[colIndex + i].Visible = false;
}
然后你需要做的就是另外去考虑如何扫面数据的问题。例如一个for循环分别遍历每一行,看看之后有几行的值与当前行相同,如果返回的值>0则合并行并且对for循环变量加上这个数字(即跳过已经被合并的行)。这样一个思路就是你这个功能的架构设计。 --------------------编程问答-------------------- 然后你需要做的就是另外去考虑如何扫面数据的问题 --> 然后你需要做的就是另外去考虑循环Grid.Items的问题
补充:.NET技术 , ASP.NET