Allow GridView sorting in Composite Control
-
23-07-2021 - |
Pregunta
I can't figure out, how to enable sorting in dynamically created GridView placed in my Composite Control. Of course, I try to set property AllowSorting of GridView to "true", but it does not allow sorting functionality (there is no linkbutton in header on which I can click). AllowPaging property works just fine.
Any ideas how to resolve this?
protected override void OnInit(EventArgs e)
{
innerGridView.AutoGenerateColumns = false;
innerGridView.AllowSorting = true;
innerGridView.AllowPaging = true;
innerGridView.PageSize = 2;
base.OnInit(e);
}
Solución
I think the only visible problem is, that there is no LinkButton generated in GridView HeaderRow. So I bypass it by setting onclick attribute to every cell in HeaderRow.
protected override void Render(HtmlTextWriter writer)
{
if (innerGridView.HeaderRow != null)
{
for (int i = 0; i < innerGridView.HeaderRow.Cells.Count; i++)
{
innerGridView.HeaderRow.Cells[i].Attributes["onclick"] =
Page.ClientScript.GetPostBackClientHyperlink(innerGridView, "Sort$" + InnerGridViewDataTable.Columns[i].Caption, true);
}
}
base.Render(writer);
}
Now I can click to header and sorting works. Of course, I will be glad if someone will come with better solution.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow