当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net中GridView和DataGrid相同列合并实现代码

asp.net中GridView和DataGrid相同列合并实现代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

好男当家演员表,西安特产,古浪县城搬迁

(一)普通列
复制代码 代码如下:

/// <summary>
/// gridview列的合并(普通列,不包含模板列)
/// 注意:1.gridview在绑定的时候进行分组和排序,才能让相同的行放在一起
/// 2.方法应用的时机,应该在gridview的databound事件中使用
/// </summary>
/// <param name="gv">需要合并的gridview对象</param>
/// <param name="columnindex">所要合并列的索引</param>
public static void unitcell(gridview gv, int columnindex)
{
int i = 0; //当前行数
string lasttype = string.empty; //当前判断是否合并行对应列的值
int lastcell = 0; //判断最后一个相同值的行的索引
if (gv.rows.count > 0)
{
lasttype = gv.rows[0].cells[columnindex].text.tostring();
gv.rows[0].cells[columnindex].rowspan = 1;
lastcell = 0;
}
for (i = 1; i < gv.rows.count; i++)
{
if (gv.rows[i].cells[columnindex].text == lasttype)
{
gv.rows[i].cells[columnindex].visible = false;
gv.rows[lastcell].cells[columnindex].rowspan++;
}
else
{
lasttype = gv.rows[i].cells[columnindex].text.tostring();
lastcell = i;
gv.rows[i].cells[columnindex].rowspan = 1;
}
}
}
/// <summary>
/// datagrid列的合并(普通列,不包含模板列)
/// 注意:1.datagrid在绑定的时候进行分组和排序,才能让相同的行放在一起
/// 2.方法应用的时机,应该在datagrid的databound事件中使用
/// </summary>
/// <param name="dg">需要合并的datagrid对象</param>
/// <param name="columnindex">所要合并列的索引</param>
public static void unitcell_t(datagrid dg, int columnindex)
{
int i = 0; //当前行数
string lasttype = string.empty; //当前判断是否合并行对应列的值
int lastcell = 0; //判断最后一个相同值的行的索引
if (dg.items.count> 0)
{
lasttype = dg.items[0].cells[columnindex].text.tostring();
dg.items[0].cells[columnindex].rowspan = 1;
lastcell = 0;
}
for (i = 1; i < dg.items.count; i++)
{
if (dg.items[i].cells[columnindex].text == lasttype)
{
dg.items[i].cells[columnindex].visible = false;
dg.items[lastcell].cells[columnindex].rowspan++;
}
else
{
lasttype = dg.items[i].cells[columnindex].text.tostring();
lastcell = i;
dg.items[i].cells[columnindex].rowspan = 1;
}
}
}

(二)模板列
复制代码 代码如下:

/// <summary>
/// gridview列的合并(模板列)
/// </summary>
/// <param name="gv">需要合并的gridview对象</param>
/// <param name="columnindex">所要合并列的索引</param>
/// <param name="lblname">模板列对象的id</param>
public static void unitcell(gridview gv, int columnindex, string lblname)
{
int i = 0; //当前行数
string lasttype = string.empty; //当前判断是否合并行对应列的值
int lastcell = 0; //判断最后一个相同值的行的索引
if (gv.rows.count > 0)
{
lasttype = (gv.rows[0].cells[columnindex].findcontrol(lblname) as label).text;
gv.rows[0].cells[columnindex].rowspan = 1;
lastcell = 0;
}
for (i = 1; i < gv.rows.count; i++)
{
if ((gv.rows[i].cells[columnindex].findcontrol(lblname) as label).text == lasttype)
{
gv.rows[i].cells[columnindex].visible = false;
gv.rows[lastcell].cells[columnindex].rowspan++;
}
else
{
lasttype = (gv.rows[i].cells[columnindex].findcontrol(lblname) as label).text.tostring();
lastcell = i;
gv.rows[i].cells[columnindex].rowspan = 1;
}
}
}

(三)在databound事件中调用即可。

在gridview或datagrid中显示固定宽度的列

默认情况下,gridview和datagrid控件依据它们的内容自动调整列大小。要为列指定固定宽度,设置每个tablecell对象的width属性并设置wrap属性为false。下面的例子显示了如何通过使用datagrid控件去做。
复制代码 代码如下:

rotected void datagrid1_itemcreated(object sender, datagriditemeventargs e)
{
listitemtype lit = e.item.itemtype;
if (lit == listitemtype.header)
{
for (int i = 0; i < e.item.cells.count; i++)
{
e.item.cells[i].width = unit.pixel(50);
e.item.cells[i].wrap = false;
}
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网