Gridview分页获得合并单元格数据分离

时间:2015-01-08 08:29:22

标签: c# asp.net gridview merge pagination

好的,因为this question没有得到答案,我想再问一遍同样的问题。当我在合并的单元格上使用分页时,当用户转到下一页时它会分开。

这是我的aspx代码:

<asp:GridView ID="GridViewEmployee" AutoGenerateColumns="false" runat="server" CssClass="Grid"
    AllowPaging="true" Width="100%" OnDataBound="OnDataBound" ShowHeaderWhenEmpty="true"
    EmptyDataText="No Records Found" PageSize="20" OnPageIndexChanging="OnPaging">
    <Columns>
        <asp:BoundField DataField="EMPLOYEE_NAME" HeaderText="Employee Name" />
        <asp:TemplateField HeaderText="View">
            <ItemTemplate>
                <asp:LinkButton ID="linkView" runat="server" 
                      CommandArgument='<%# Bind("EMPLOYEE_ID")%>'
                      Text='<%# Bind("EMPLOYEE_ID")%>' 
                      OnClick="DetailView">
                </asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

我的合并代码:

protected void OnDataBound(object sender, EventArgs e)
    {
        for (int i = GridViewEmployee.Rows.Count - 1; i > 0; i--)
        {
            GridViewRow row = GridViewEmployee.Rows[i];
            GridViewRow previousRow = GridViewEmployee.Rows[i - 1];
            for (int j = 0; j < row.Cells.Count & j != 5; 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;
                    }
                }
            }
        }

        //Looping for TemplateField
        for (int i = GridViewEmployee.Rows.Count - 1; i > 0; i--)
        {
            GridViewRow row = GridViewEmployee.Rows[i];
            GridViewRow previousRow = GridViewEmployee.Rows[i - 1];
            for (int j = 0; j < row.Cells.Count - 1; j++)
            {

                if (((LinkButton)row.Cells[1].FindControl("linkView")).Text == ((LinkButton)previousRow.Cells[1].FindControl("linkView")).Text)
                {
                    if (previousRow.Cells[1].RowSpan == 0)
                    {
                        if (row.Cells[1].RowSpan == 0)
                        {
                            previousRow.Cells[1].RowSpan += 2;
                        }
                        else
                        {
                            previousRow.Cells[1].RowSpan = row.Cells[1].RowSpan + 1;
                        }
                        row.Cells[1].Visible = false;
                    }
                }
            }
        }


    }

这是我的分页代码:

protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
        GridViewEmployee.PageIndex = e.NewPageIndex;
        BindData();
    }

我的数据来源:

private void BindData()
    {
        GridViewEmployee.DataSource = empQuery.getEmployeeList();

        GridViewEmployee.DataBind();
    }

我的问题是,如何在移动到下一页时使GridView中的合并单元格不分离?

这段代码仍然有一个bug,但几乎得到了它:

经过一周的努力,我终于成功了。感谢Venki为我提供了代码逻辑。 这是一个更新的代码,我把它作为一个答案。

private void Testing()
    {
        List<Model> listTest = data.getData(2002);
        DataTable table = ListToDataTable(listTest);
        string nextSty, currentSty;
        int pageSize = GridTest.PageSize;

        foreach (DataRow row in table.Rows)
        {
            int a = 0;
            for (int j = pageSize; j < table.Rows.Count; j++)
            {
                nextSty = table.Rows[a+1][0].ToString();
                currentSty = table.Rows[a][0].ToString();
                if (currentSty != nextSty)
                {
                    GridTest.PageSize = j;
                    break;
                }
                a++;
            }
        }

        GridTest.DataSource = table;
        GridTest.DataBind();

    }

    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
        GridTest.PageIndex = e.NewPageIndex;
        Testing();
    }

    public DataTable ListToDataTable<T>(List<T> items)
    {

        DataTable dataTable = new DataTable(typeof(T).Name);
        PropertyInfo[] Properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

        foreach (PropertyInfo propInfo in Properties)
        {
            dataTable.Columns.Add(propInfo.Name);
        }

        foreach (T item in items)
        {
            var values = new object[Properties.Length];

            for (int i = 0; i < Properties.Length; i++)
            {
                values[i] = Properties[i].GetValue(item, null);
            }
            dataTable.Rows.Add(values);
        }
        return dataTable;
    }

请参阅Venki的第二个答案,尝试并理解它的逻辑和代码。我已经尝试过了,我会更新这个。

2 个答案:

答案 0 :(得分:2)

for (i = PagSize; i <= dt.Rows.Count; i++)
  {
     Curr = dt.Rows[i - 1]["Brand"].ToString();
        if (i < dt.Rows.Count)
         {
           Nxt = dt.Rows[i]["Brand"].ToString();
           diff = dt.Rows.Count - i;
           if (Curr != Nxt)
           {
            DctPaging.Add(PageNum, i);
            PageNum = PageNum + 1;
            i = i + PagSize;
            if (i >= dt.Rows.Count)
            {
             DctPaging.Add(PageNum, dt.Rows.Count);
             break;
            }
        }
   }

参考demo Example

答案 1 :(得分:1)

嗨,尼古拉斯也在研究同样的问题。我已经写了一段代码来管理分页,但在所有方面都不能正常工作。

try
     {

            con.Open();
            da1.Fill(DS);
            grd_popup_details.DataSource = DS;
            string nextSty, currentSty;
            int psize;
            psize = grd_popup_details.PageSize;
           // MaxBindVal= Convert.ToInt32(hidMaxGridVal.Value);
            for (int i = psize; i < DS.Tables[0].Rows.Count; i++)
            {
                currentSty = DS.Tables[0].Rows[(MaxBindVal) + i - 1]["STY_NBR"].ToString();
                nextSty = DS.Tables[0].Rows[(MaxBindVal) + i]["STY_NBR"].ToString();
                if (currentSty != nextSty)
                {
                    grd_popup_details.PageSize = i;
                    MaxBindVal = MaxBindVal + i;
                    hidMaxGridVal.Value = MaxBindVal.ToString();
                    break;
                }

            }

            grd_popup_details.AllowPaging = true;
            grd_popup_details.DataBind();
            con.Close();
        }
        catch (Exception es)
        {
            lblstatus.Text = es.Message.ToString();
}

在上面的代码中,MaxBindval是一个全局变量,它保存数据集的最后显示的记录索引。最初它将为零。如果你变得更好,请更新我。