MSDN ASP.NET GridView 範例集 Group Row 部份程式碼修正

本文修正了发布于MSDN上关于ASP.NET GridView控件中GroupRow部分的示例代码错误,并提供了正确的实现方式,特别是针对泛型List的使用进行了详细说明。

可能因為排版緣故,導致刊登於MSDN上的ASP.NET GridView 範例集中Group Row的部份程式碼有錯誤,

特於此列出正確的程式碼(主要是泛型List的泛型參數問題,由於使用了<,> 符號,可能於排版時漏掉了).

Group Row 程式碼

private void PrepareGroup()
{
int lastSupID = -1;
GridViewRow currentRow = null;
List<GridViewRow> <gridviewrow></gridviewrow>tempModifyRows = new List<GridViewRow><gridviewrow></gridviewrow>();
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
if (currentRow == null)
{
currentRow = row;
int.TryParse(row.Cells[2].Text, out lastSupID);
continue;
}

int currSupID = -1;
if (int.TryParse(row.Cells[2].Text, out currSupID))
{
if (lastSupID != currSupID)
{
currentRow.Cells[2].Attributes["rowspan"] = (tempModifyRows.Count+1).ToString();
currentRow.Cells[2].Attributes["valign"] = "center";
foreach (GridViewRow row2 in tempModifyRows)
row2.Cells.RemoveAt(2);
lastSupID = currSupID;
tempModifyRows.Clear();
currentRow = row;
lastSupID = currSupID;
}
else
tempModifyRows.Add(row);
}
}
}

if (tempModifyRows.Count > 0)
{
currentRow.Cells[2].Attributes["rowspan"] = (tempModifyRows.Count + 1).ToString();
currentRow.Cells[2].Attributes["valign"] = "center";
foreach (GridViewRow row2 in tempModifyRows)
row2.Cells.RemoveAt(2);
}
}

protected void GridView1_PreRender(object sender, EventArgs e)
{
PrepareGroup();
}

抱歉造成各位讀者的困擾了 ^_^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值