Gridview Paging 分离了 Merge Cell Data
Gridview Paging got Merge Cell Data Separated
好的,因为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 的第二个答案,尝试理解其中的逻辑和代码。当我已经尝试过时,我会更新它。
嗨,尼古拉斯,我也在处理同样的问题。我已经编写了一段代码来管理分页,但在所有方面都无法正常工作。
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 是一个全局变量,它保存了数据集最后显示的记录索引。最初它将为零。如果你有更好的,请更新我。
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;
}
}
}
好的,因为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 的第二个答案,尝试理解其中的逻辑和代码。当我已经尝试过时,我会更新它。
嗨,尼古拉斯,我也在处理同样的问题。我已经编写了一段代码来管理分页,但在所有方面都无法正常工作。
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 是一个全局变量,它保存了数据集最后显示的记录索引。最初它将为零。如果你有更好的,请更新我。
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;
}
}
}