在gridview中显示分组列

时间:2014-02-19 11:53:25

标签: c# asp.net .net gridview

我在从数据库查询后在gridview中显示以下结果。

Canada           Ontario      Cambridge
Canada           Ontario      Halton
Canada           Ontario      Hamilton
Canada           Ontario      Milton
United Kingdom    Wales       Bangor
United Kingdom    Wales       Cardiff
United Kingdom    Wales       New Port
United Kingdom    Wales       Swansea
United States     CA          Los Angeles
United States     CA          Merced
United States     CA          Morgan Hill
United States     CA          San Francisco

我曾参与过gridview,但我怎么可能在gridview中显示结果

Canada           Ontario     Cambridge
                             Halton
                             Hamilton
                             Milton
United Kingdom    Wales      Bangor
                             Cardiff
                             New Port
                             Swansea
United States     CA         Los Angeles
                             Merced
                             Morgan Hill
                             San Francisco

对此有何建议?

2 个答案:

答案 0 :(得分:3)

This is the best link which can help you

public class GridDecorator
{
public static void MergeRows(GridView gridView)
{
    for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
    {
        GridViewRow row = gridView.Rows[rowIndex];
        GridViewRow previousRow = gridView.Rows[rowIndex + 1];

        for (int i = 0; i < row.Cells.Count; i++)
        {
            if (row.Cells[i].Text == previousRow.Cells[i].Text)
            {
                row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 : 
                                       previousRow.Cells[i].RowSpan + 1;
                previousRow.Cells[i].Visible = false;
            }
        }
    }
}

}

protected void gridView_PreRender(object sender, EventArgs e)
{
    GridDecorator.MergeRows(gridView);
}

答案 1 :(得分:0)

您可以通过将结果分组到数据库中来完成此操作(使用laglead函数与上一行进行比较。您也可以使用Infragistics UltraGrid等自定义控件,对我们来说很好,但这是一个付费的解决方案。