我正在使用ASP.NET动态数据为几个数据表生成一个网站,到目前为止一直很好。
但是客户要求他们想要在GridView中合并具有相等值的单元格。
我有一个解决方案CodeProject,它在普通的ASP.NET页面中运行良好。
但是,它只是将所有行合并到动态数据生成的GridView中的一行中。
我跟踪了源代码,发现在GridView_PreRender
方法中,row.Cells[cellIndex].Text
始终为空!
所以,我不能判断两个细胞是否相同。 有没有人遇到过这样的问题?
答案 0 :(得分:0)
根据我的研究,为了在ASP.NET动态数据中的GridView's
事件中获取PreRender
个单元格的值,您需要执行以下操作:
FieldTemplate
FieldTemplate
投射到FieldTemplateUserControl
以获取对其属性的访问权限我刚刚根据我的项目模拟了你的问题。
<强> List.aspx.cs:强>
protected void gvOffices_PreRender(object sender, EventArgs e)
{
for (int rowIndex = gvOffices.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = gvOffices.Rows[rowIndex];
GridViewRow previousRow = gvOffices.Rows[rowIndex + 1];
if (row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < row.Cells.Count; i++)
{
string dataField = ((DynamicField)((DataControlFieldCell)row.Cells[i]).ContainingField).DataField;
Control dataControl = ((FieldTemplateUserControl)((DynamicControl)row.Cells[i].FindDynamicControlRecursive(dataField)).FieldTemplate).DataControl;
string cellText = ((Literal)dataControl).Text; // for text fields
string dataFieldPrev = ((DynamicField)((DataControlFieldCell)previousRow.Cells[i]).ContainingField).DataField;
Control dataControlPrev = ((FieldTemplateUserControl)((DynamicControl)previousRow.Cells[i].FindDynamicControlRecursive(dataField)).FieldTemplate).DataControl;
string cellTextPrev = ((Literal)dataControl).Text; // for text fields
Response.Write(cellText);
Response.Write(cellTextPrev);
//if (cellText == cellTextPrev)
//{
// row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
// previousRow.Cells[i].RowSpan + 1;
// previousRow.Cells[i].Visible = false;
//}
}
}
}
}
您可以在http://csharpbits.notaclue.net/2009/01/dynamic-data-cascading-fieldtemplates.html找到 FindDynamicControlRecursive()
。
在您的项目中考虑这种技术。我希望这会有所帮助。
编辑1:
我们还需要检查GridView单元格中的控件是HyperLink
还是Literal
:
if (dataControl is Literal)
{
cellText = ((Literal)dataControl).Text;
}
else
{
if (dataControl is HyperLink)
{
cellText = ((HyperLink)dataControl).Text;
}
}
编辑2:(它适用于我)
检查GridView单元格内的控件是否为DynamicField
:
protected void gvOffices_PreRender(object sender, EventArgs e)
{
for (int rowIndex = gvOffices.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = gvOffices.Rows[rowIndex];
GridViewRow previousRow = gvOffices.Rows[rowIndex + 1];
if ((row.RowType == DataControlRowType.DataRow) && (previousRow.RowType == DataControlRowType.DataRow))
{
for (int i = 0; i < row.Cells.Count; i++)
{
// check for the current row
if (((DataControlFieldCell)row.Cells[i]).ContainingField is DynamicField)
{
string dataField = ((DynamicField)((DataControlFieldCell)row.Cells[i]).ContainingField).DataField;
Control dataControl = ((FieldTemplateUserControl)((DynamicControl)row.Cells[i].FindDynamicControlRecursive(dataField)).FieldTemplate).DataControl;
string cellText = string.Empty;
if (dataControl is Literal)
{
cellText = ((Literal)dataControl).Text;
}
else
{
if (dataControl is HyperLink)
{
cellText = ((HyperLink)dataControl).Text;
}
}
// get cells text of the current row
Response.Write(cellText);
}
// check for the previous row
if (((DataControlFieldCell)previousRow.Cells[i]).ContainingField is DynamicField)
{
string dataFieldPrev = ((DynamicField)((DataControlFieldCell)previousRow.Cells[i]).ContainingField).DataField;
Control dataControlPrev = ((FieldTemplateUserControl)((DynamicControl)previousRow.Cells[i].FindDynamicControlRecursive(dataFieldPrev)).FieldTemplate).DataControl;
string cellTextPrev = string.Empty;
if (dataControlPrev is Literal)
{
cellTextPrev = ((Literal)dataControlPrev).Text;
}
else
{
if (dataControlPrev is HyperLink)
{
cellTextPrev = ((HyperLink)dataControlPrev).Text;
}
}
// get cells text of the previous row
Response.Write(cellTextPrev);
}
// Try to merge cells
//if (cellText == cellTextPrev)
//{
// row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
// previousRow.Cells[i].RowSpan + 1;
// previousRow.Cells[i].Visible = false;
//}
}
}
}
}