1

テーブルを結合したいのですが、方法がわかりません。私はすでに何度も試しましたが、まだ正しい解決策を得ることができません。現在、私のグリッドビューは次のようになっています:

Data 1  |  Data 1  |  Data 1  |  Data 1
Data 1  |  Data 1  |  Data 1  |  Data 1
Data 2  |  Data 2  |  Data 2  |  Data 2
Data 2  |  Data 2  |  Data 2  |  Data 2

そして、グリッドビューを次のようにしたい:

Data 1   |   Data 1   |   Data 1  |   Data 1
         |   Data 1   |   Data 1  |
Data 2   |   Data 2   |   Data 2  |   Data 2
         |   Data 2   |   Data 2  |
4

1 に答える 1

3

セルをマージするコードは非常に短いです。

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;
                }
            }
        }
    }
}

最後のアクションは、GridView の OnPreRender イベント ハンドラーを追加することです。

protected void gridView_PreRender(object sender, EventArgs e)
{
    GridDecorator.MergeRows(gridView);
}
于 2012-05-17T03:34:54.033 に答える