2

Okay since this question did not get an answer, I want to re-ask again the same question. When I am using paging on my merged cell, it gets separated when the user goes to the next page.

Here is my aspx code :

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

my merging code :

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


    }

And here it is my paging code :

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

my Data Source :

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

        GridViewEmployee.DataBind();
    }

My question is, how to make the merged cell in GridView not get separated when moving to the next page?

THIS piece of code that still has a bug but almost got it :

After struggling with this for a week, I finally made it work. Thanks to Venki for giving me the code logic. Here is an updated code that i make it into an answer.

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

See the second answer from Venki, Try and understand that logic and code from it. I'll update this when i already try it.

Community
  • 1
  • 1
Nicolas
  • 246
  • 1
  • 6
  • 23
  • 1
    hey Nicolas, very nice to see the great work here. there are two cases yet which you need to focus in your code. 1. there is also chance to show only two items in single page. 2. Check what if the user clicks from page 1 to page 3 or so. you will lost some data or you will be loose the structure of grid with 2 or 3 rows in single page – Venki Jan 14 '15 at 11:29
  • Darn it, so we have to make a new logic or condition here, i have check when the user click like from page 1 to page 3 or so, the data got separated or screw again – Nicolas Jan 14 '15 at 12:09
  • 2
    yes am currently working on that. Almost completed. at last I come to know that inbuilt gridview paging wont works for our functionality so want go manually. ping you in 2 days with that code don't worry – Venki Jan 14 '15 at 12:20

2 Answers2

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

Reference demo Example

Rock star
  • 201
  • 2
  • 7
  • Please check the formatting. There aren't enough parentheses and add identation. – t3chb0t Jan 19 '15 at 10:28
  • Its my First answer boss. let me learn, formating may be worng but answer is usefull – Rock star Jan 19 '15 at 10:36
  • I didn't downvote you. I let you learn by telling you what's wrong and how you can improve it ;-] it's much better now... but still, a few words about the shippet you've pasted would be nice. – t3chb0t Jan 19 '15 at 10:38
  • Is this a coincidence that your name is the same as the other person who posted the accepted answer or are you the same person? – t3chb0t Jan 19 '15 at 10:40
  • Nice bro venki, i'll try it. And by the way, is it you? Make new account? LoL – Nicolas Jan 19 '15 at 12:30
1

Hi Nicolas am too working on the same issue. i have written piece of code to manage paging but wont works fine in all the aspects.

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

in above code MaxBindval is a global variable which holds the last shown records index of Dataset. initially it will be as Zero. update me if u got a better.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Venki
  • 535
  • 2
  • 8
  • 21
  • I see in there you put your data first into data table, and then merging it in there? After that put that data table into gridview data source. And then you search for the "not same value" as for the pageSize. Is that right? – Nicolas Jan 09 '15 at 07:42
  • Merge will take place in OnrowBound before calling the onrowbound event itself we should make this code to work for setting pagesize – Venki Jan 09 '15 at 11:16
  • 1
    yeah right, so first we have to bind the data into something that can contain it, like data table. After that put in your logic like above to set it for the pagesize. And that datatable we bind it into gridview datasource. – Nicolas Jan 12 '15 at 03:37
  • See my updated question. It has now been SOLVED. Thanks for your logic code – Nicolas Jan 14 '15 at 11:07