【问题标题】:gridview rows and columns mergegridview 行列合并
【发布时间】:2018-05-29 07:49:50
【问题描述】:

我正在尝试绑定到网格视图的以下格式的数据表中的数据(格式 -1)

 Id     col1    col2
 20      a     ac-1
 20      a     ac-2
 20      a     ac-3
 20      a     ac-4
 21      a     ac-1
 21      a     ac-2
 21      a     ac-3
 21      a     ac-4

数据绑定后,我得到了以下格式的gridview (format- 2)

 Id     col1    col2
 20      a      ac-1
                ac-2
                ac-3
                ac-4
 21             ac-1
                ac-2
                ac-3
                ac-4

但我正在寻找要在 gridview 中表示的以下数据格式 (Format-3)

   Id   col1    col2
   20     a     ac-1
                ac-2
                ac-3
                ac-4
   21     a     ac-1
                ac-2
                ac-3
                ac-4

下面的代码是用于 grdiview 中的 Ondatabound 事件

   for (int i = gvConversionGrid.Rows.Count - 1; i > 0; i--)
   {
        GridViewRow row = gvConversionGrid.Rows[i];
        GridViewRow previousRow = gvConversionGrid.Rows[i - 1];
        for (int j = 0; j < row.Cells.Count; j++)
        {
            if (row.Cells[j].Text == previousRow.Cells[j].Text)
            {
                if (previousRow.Cells[j].RowSpan == 0)
                {
                    if (row.Cells[j].RowSpan == 0)
                    {
                        previousRow.Cells[j].RowSpan += 2;
                    }
                    else
                    {
                        previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
                    }
                    row.Cells[j].Visible = false;
                }
            }
        }
   }

有什么方法可以操作 gridview 并为 gridview 数据带来格式,如 format-3..

请任何人帮助解决这个问题,非常感谢我.. 非常感谢提前

更新:我正在寻找以下格式图片

【问题讨论】:

  • 根本不可能吗,有人对此合并行有什么建议吗..
  • 格式 1 来自一个表吗?它应该只绑定到一个 GridView 吗??
  • @Asif.Ali 它只是单个数据表的形式......是的,它只将它绑定到一个网格视图......
  • col1 值对所有行都相同,只有 Id 和 col2 不同?
  • @Asif.Ali 抱歉,我无法告诉你 col1 值对于所有行都相同意味着?

标签: c# asp.net c#-4.0 datatable c#-3.0


【解决方案1】:

您可以将之前的Id 值设置为在方法外部声明的变量,并将当前行与GridView 的OnRowDataBound 事件中的行进行比较。

string previousCellValue = "";

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the dataitem back to a row
        DataRowView row = e.Row.DataItem as DataRowView;

        //check if the current id matches the previous row
        if (previousCellValue == row["Id"].ToString())
        {
            //clear the first cell
            e.Row.Cells[0].Text = "";

            //apply column span
            e.Row.Cells[0].ColumnSpan = 2;
            e.Row.Cells[1].Visible = false;
        }
        else
        {
            previousCellValue = row["Id"].ToString();
        }
    }
}

结果:

更新

string previousCellValue = "";
int previousCellCount = 1;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow and not the first row
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the dataitem back to a row
        DataRowView row = e.Row.DataItem as DataRowView;

        //check if the current id matches the previous row
        if (previousCellValue == row["Id"].ToString())
        {
            //count the number of same cells
            previousCellCount++;
        }
        else
        {
            //span the rows for the first two cells
            if (previousCellCount > 1)
            {
                GridView1.Rows[e.Row.RowIndex - previousCellCount].Cells[0].RowSpan = previousCellCount;
                GridView1.Rows[e.Row.RowIndex - previousCellCount].Cells[1].RowSpan = previousCellCount;

                //hide the other cells in the column
                for (int i = 1; i < previousCellCount; i++)
                {
                    GridView1.Rows[(e.Row.RowIndex - previousCellCount) + i].Cells[0].Visible = false;
                    GridView1.Rows[(e.Row.RowIndex - previousCellCount) + i].Cells[1].Visible = false;
                }
            }

            previousCellValue = row["Id"].ToString();
            previousCellCount = 1;
        }
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        //use the footer row to create spanning for the last rows if needed
        if (previousCellCount > 1)
        {
            GridView1.Rows[GridView1.Rows.Count - previousCellCount].Cells[0].RowSpan = previousCellCount;
            GridView1.Rows[GridView1.Rows.Count - previousCellCount].Cells[1].RowSpan = previousCellCount;

            //hide the other cells in the column
            for (int i = 1; i < previousCellCount; i++)
            {
                GridView1.Rows[(GridView1.Rows.Count - previousCellCount) + i].Cells[0].Visible = false;
                GridView1.Rows[(GridView1.Rows.Count - previousCellCount) + i].Cells[1].Visible = false;
            }
        }
    }
}

结果:

【讨论】:

  • 我在 onDataBound 中进行了该操作..您已经在 rowDataBound 事件中给出了正确的方法..
  • 没错,就是 OnRowDataBound 事件。
  • 仍然我在第二列中只得到一个“a”..您能否建议必须进行任何更改
  • 我不明白你最后的评论。我的 sn-p 将产生您所指示的确切结果。查看添加的屏幕截图。
  • 我在我的问题中附上了图片,您能否建议任何更改以获得该图片类型的网格..
【解决方案2】:

您可以插入 2 个隐藏列

 Id     col1    col2  groupindex  groupcount
 20      a     1      1         4
 20      a     2      2         4
 20      a     3      3         4
 20      a     4      4         4
 21      a     1      1         5
 21      a     2      2         5
 21      a     3      3         5
 21      a     4      4         5
 21      a     5      5         5

并设置行跨度:

 void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow )
    {

        // set rowspan for the first row of group
        if (Convert.ToInt32(e.Row.Cells[3]) == 1)
        {
            var rowSpan = Convert.ToString(e.Row.Cells[4]);
            e.Row.Cells[0].Attributes.Add("rowspan", rowSpan);
            e.Row.Cells[1].Attributes.Add("rowspan", rowSpan);
        }
        else
        {
            e.Row.Cells[0].Visible = false;
            e.Row.Cells[1].Visible = false;
        }
    }
}

【讨论】:

  • 感谢您的建议 .. 我需要在哪里插入该组索引和组计数以及如何将其填充到 gridview ...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
  • 2012-05-24
  • 2014-11-22
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多