1

我正在使用 ASP.NET 动态数据为几个数据表生成一个网站,到目前为止一切都很好。

但是客户要求他们希望在 GridView 中合并具有相同值的单元格。

我有一个解决方案CodeProject,它在普通的 ASP.NET 页面中运行良好。

但是,它只是将所有行合并为动态数据生成的 GridView 中的一行。我跟踪了源代码,发现在GridView_PreRender方法中,row.Cells[cellIndex].Text总是为空!

所以,我无法判断两个单元格是否相同。有没有人遇到过这样的问题?

4

1 回答 1

0

根据我的研究,为了在 ASP.NET 动态数据中获取事件GridView's单元格的值,PreRender您需要执行以下操作:

  1. FieldTemplate在单元格的控件集合中找到
  2. 强制FieldTemplate转换FieldTemplateUserControl以访问它的属性

我刚刚根据我的项目模拟了您的问题。

列表.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;
                //}
            }
        }
    }
}

FindDynamicControlRecursive()您可以在http://csharpbits.notaclue.net/2009/01/dynamic-data-cascading-fieldtemplates.html找到。

在您的项目中考虑这种技术。我希望这将有所帮助。

编辑1:

我们还需要检查 GridView 单元格内的控件是否为HyperLinkor 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;
                //}
            }
        }
    }
}
于 2013-05-19T18:51:05.147 回答