【发布时间】:2015-03-06 07:30:39
【问题描述】:
好的,因为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 中的合并单元格在移动到下一页时不分离?
这段代码仍然有一个错误,但几乎得到它:
在为此苦苦挣扎了一个星期后,我终于成功了。感谢 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 的第二个答案,尝试并从中理解该逻辑和代码。当我已经尝试过时,我会更新它。
【问题讨论】:
-
嘿 Nicolas,很高兴看到这里的出色工作。您需要在代码中关注两种情况。 1. 也有机会在单页中只显示两个项目。 2.检查如果用户从第1页点击到第3页左右会怎样。您将丢失一些数据,或者您将失去单页 2 或 3 行的网格结构
-
该死,所以我们必须在这里建立一个新的逻辑或条件,我已经检查了当用户从第 1 页到第 3 页左右点击时,数据被分离或再次拧紧
-
是的,目前正在努力。快完成了。最后我知道内置的gridview分页不适用于我们的功能,所以想要手动进行。用该代码在 2 天内 ping 你不用担心
标签: c# asp.net gridview merge pagination