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.