使用OnDataBound asp:net gridview合并单元格后对网格视图进行排序


private void GridViewBind()
{
SqlConnection Dbcon = new SqlConnection();
Dbcon.ConnectionString = ConfigurationManager.ConnectionStrings[
"ConnectionString"].ConnectionString;
Dbcon.Open();
string expSql = "SELECT [dName],[item],[cnt] FROM [Test1].[dbo].[Test3]
ORDER BY [dName], [cnt] desc";
SqlDataAdapter adAdapter = new SqlDataAdapter(expSql, Dbcon);
DataSet ds = new DataSet();
adAdapter.Fill(ds);
GridView.DataSource = ds.Tables[0];
GridView.DataBind();
ViewState["dt"] = ds.Tables[0];
ViewState["sort"] = "Desc";
}

我使用gridview(asp:gridview(绘制了一个表,并制作了一个数据库进行测试并绑定然后,为了合并具有相同主题的单元格,使用OnDataBound合并单元格。

单击cnt时,我想按合并主题排序然而,我的排序代码对整个cnt列进行排序,合并后的单元格就会被分解。

我需要一种方法来保留合并的单元格,并在其中进行ASC/DESC排序!

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

我没有声誉可以评论,所以我在这里评论作为回答。你能在合并主题的地方添加你的OnDataBound代码吗?也可以将合并的主题作为列进行选择查询,然后只需要将数据直接绑定到gridview。

对于排序,您可以遵循以下链接:

https://www.c-sharpcorner.com/UploadFile/b8e86c/paging-and-sorting-in-Asp-Net-gridview/

https://codepedia.info/gridview-sorting-header-click

最新更新