colspan gridview rows[英] colspan gridview rows

本文是小编为大家收集整理的关于colspan gridview rows的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我将行添加到GridView中. GridView中有20列.我如何在GridView中执行类似Colspan的功能,该功能可以在2-3列以下显示2-3行,并保留为Colspan.

基本上,我希望在GridView的行中实现GridView中的Colspan.

因此,我现在的GV就像;

Col 1 Col 2 Col 3 Col 4 ...... Col 20

Cell1 Cell2 Cell3 Cell 3 Cell 4 ...... Cell 20(对于行#1)

我希望有

之类的东西

Col 1 Col 2 Col 3 Col 4 ...... Col 20

    Cell1      Cell2    ...... Cell 20   (For Rows # 1)

让我知道任何查询.

谢谢

推荐答案

您需要处理GridView的OnrowCreated事件,如下所示:

 protected void grid_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[2].ColumnSpan = 2;
        //now make up for the colspan from cell2
        e.Row.Cells.RemoveAt(4);
    }
}

您的标记应该是这样的:

<asp:GridView runat="server" ID="grid" OnRowCreated="grid_RowCreated" >

在上面的示例中,我用以下方式填充了网格:

DataTable dt = new DataTable();
        for (int i = 0; i < 5; i++)
        {
            dt.Columns.Add("Col " + i);
        }
        for (int i = 0; i < 10; i++)
        {
            DataRow r = dt.NewRow();
            r.ItemArray=new object[]{"row "+i,"row "+i,"row "+i,"row "+i,"row "+i};
            dt.Rows.Add(r);
        }

        grid.DataSource = dt;
        grid.DataBind();

它产生了这个: 示例图像

我只是意识到您想拥有一行(不一定是标题),以便您可以做:

:

 protected void grid_RowCreated(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[2].ColumnSpan = 2;
        //now make up for the colspan from cell2
        e.Row.Cells.RemoveAt(4);
    }
}

它将产生:

在此处输入图像说明

其他推荐答案

BoundField和TemplateField标签具有属性ItemStyle-Width =" 22%",因为您可以看到,可以为每列设置它,并以百分比响应

本文地址:https://www.itbaoku.cn/post/256715.html

问题描述

I have added rows into gridview. There are 20 columns in gridview. How can i do a colspan-like feature in gridview which could show me 2-3 rows under 2-3 columns and remaining as a colspan.

Basically i wish to implement colspan in gridview on the rows of the gridview.

hence my present gv is like ;

Col 1 Col 2 Col 3 Col 4 ...... Col 20

Cell1 Cell2 Cell3 Cell 4 ...... Cell 20 (For Rows # 1)

I wish to have something like

Col 1 Col 2 Col 3 Col 4 ...... Col 20

    Cell1      Cell2    ...... Cell 20   (For Rows # 1)

Let me know for any query.

Thanks

推荐答案

You need to handle the OnRowCreated event of the GridView as follows:

 protected void grid_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[2].ColumnSpan = 2;
        //now make up for the colspan from cell2
        e.Row.Cells.RemoveAt(4);
    }
}

Your markup should be something like this:

<asp:GridView runat="server" ID="grid" OnRowCreated="grid_RowCreated" >

On the above example, I populated the grid with this:

DataTable dt = new DataTable();
        for (int i = 0; i < 5; i++)
        {
            dt.Columns.Add("Col " + i);
        }
        for (int i = 0; i < 10; i++)
        {
            DataRow r = dt.NewRow();
            r.ItemArray=new object[]{"row "+i,"row "+i,"row "+i,"row "+i,"row "+i};
            dt.Rows.Add(r);
        }

        grid.DataSource = dt;
        grid.DataBind();

And it produces this: sample image

I just realized that you wanted to have the ROWS (not necessarily the header) to have certain colspan, in which case you can do:

 protected void grid_RowCreated(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[2].ColumnSpan = 2;
        //now make up for the colspan from cell2
        e.Row.Cells.RemoveAt(4);
    }
}

And it will produce:

enter image description here

其他推荐答案

BoundField and TemplateField tags has the property ItemStyle-Width="22%" as you can see you can set it for each column with a percentage to be responsive