【问题标题】:Handle Sorting event of programmatically created gridviews处理以编程方式创建的网格视图的排序事件
【发布时间】:2014-02-12 09:02:48
【问题描述】:

我有一个项目,我需要在其中显示许多网格视图。网格视图的数量取决于目录拥有的表的数量。

例如:

目录 A = 8 个表

目录 B = 7 个表

如果用户单击目录 A,则应创建 8 个网格视图。 我已经完成了以编程方式创建此网格视图,我现在的问题是我如何处理每个以编程方式创建的网格视图的排序事件。

这就是我创建网格视图的方式:

foreach (XMLClasses.table dirTab in dir.table.ToList())
{
    if (dirTab.id == child.tabid)
    {
        List<XMLClasses.column> columns = new List<XMLClasses.column>();
        columns = dirTab.column;
        string[] rows = new string[columns.Count];
        int x = 0;
        foreach (XMLClasses.column col in columns.ToList())
        {
            dtContent.Columns.Add(col.title);
            rows[x] = "b";
            x = x + 1;
        }
        dtContent.Rows.Add(rows);
        GridView grdTables = new GridView();
        grdTables.AllowSorting = true;
        grdTables.DataSource = dtContent;
        grdTables.DataBind();
        grdTables.Width = Unit.Percentage(100);
        grdTables.Sorting+=new GridViewSortEventHandler(grdTables_Sorting);

        pnlDirectory.Controls.Add(grdTables);

        Literal lt = new Literal();
        lt.Text = "<br/>";
        pnlDirectory.Controls.Add(lt);

    }
}

我有这个。我不知道下一步该怎么做..

有人知道吗? 任何帮助将不胜感激!

谢谢!

【问题讨论】:

  • 你能发布一些代码吗

标签: c# asp.net sorting gridview


【解决方案1】:

如果您能够以编程方式创建网格视图并为其设置名称,那么您就可以处理排序问题。

void SortButton_Click(Object sender, EventArgs e) {

String expression = "";
SortDirection direction;

// Create the sort expression from the values selected 
// by the user from the DropDownList controls. Multiple
// columns can be sorted by creating a sort expression
// that contains a comma-separated list of field names.
expression = SortList1.SelectedValue + "," + SortList2.SelectedValue;

//  Determine the sort direction. The sort direction
// applies only to the second column sorted.
switch (DirectionList.SelectedValue)
{
  case "Ascending":
    direction = SortDirection.Ascending;
    break;
  case "Descending":
    direction = SortDirection.Descending;
    break;
  default:
    direction = SortDirection.Ascending;
    break;
}

// Use the Sort method to programmatically sort the GridView
// control using the sort expression and direction.
CustomersGridView.Sort(expression, direction);

}

这里发生的是创建了两个属性;表达和方向。

通过使用 sortDirection 对象设置排序(降序或升序)和可用于将某些列作为目标进行排序的字符串“表达式”,并将其用作 .Sort 方法的参数,您可以得到想要的效果使用。

编辑

忘记添加代码的来源。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sort(v=vs.110).aspx

【讨论】:

  • 我需要在用户单击其列时触发排序事件
  • 您可以在事件处理程序中使用此代码并获取列属性以用正确的值填充“表达式”,因此 Sort 方法可以为您完成。 msdn.microsoft.com/en-us/library/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-09
  • 1970-01-01
相关资源
最近更新 更多