GridView排序表达式必须返回非空数据

时间:2010-03-01 17:21:34

标签: c# .net asp.net gridview

我的程序运行良好。但我尝试在gridView标题上添加上下图标。但如果我做到了,我的断点应该低于“如果比较”,例如; field.SortExpression = Category但每次在gridview.SortExp不为空时,CustomersGridView.SortExpression为空。

foreach (DataControlField field in CustomersGridView.Columns)
    {
      if (field.SortExpression == CustomersGridView.SortExpression)
      {
        return CustomersGridView.Columns.IndexOf(field);
      }
    }

我需要:


foreach (DataControlField field in CustomersGridView.Columns)
    {
      if (field.SortExpression == "Category")
      {
        return 2;
      }
    }

CustomersGridView.SortExpression不能为空!!!!!

我的来源:

<head runat="server">
    <title></title>
     <link type="text/css" href="StyleSheet.css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
         <asp:GridView 
                        ID="gvCustomers" runat="server" CssClass="tablestyle" 
                        AllowSorting="true" 
                        OnRowDataBound="GvCustomers_RowDataBound" AutoGenerateColumns="false">
                        <AlternatingRowStyle CssClass="altrowstyle" />
                        <HeaderStyle CssClass="headerstyle" />
                        <RowStyle CssClass="rowstyle" />
                        <Columns>
                            <asp:BoundField HeaderText="Kategori" DataField="Category" SortExpression="Category" />
                            <asp:BoundField HeaderText="Tarih" DataField="Date" SortExpression="Date" />

                        </Columns>
                    </asp:GridView>
        </ContentTemplate>
        </asp:UpdatePanel>

   protected void GvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridView gridView = (GridView)sender;

            if (gridView.SortExpression.Length > 0)
            {
                int cellIndex = -1;
                foreach (DataControlField field in gridView.Columns)
                {
                    if (field.SortExpression == gvCustomers.SortExpression)
                    {
                        cellIndex = gridView.Columns.IndexOf(field);
                        break;
                    }
                }

                if (cellIndex > -1)
                {
                    if (e.Row.RowType == DataControlRowType.Header)
                    {
                        //  this is a header row,
                        //  set the sort style
                        e.Row.Cells[cellIndex].CssClass +=
                            (gridView.SortDirection == SortDirection.Ascending
                            ? " sortascheader" : " sortdescheader");
                    }
                    else if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        //  this is an alternating row
                        e.Row.Cells[cellIndex].CssClass +=
                            (e.Row.RowIndex % 2 == 0
                            ? " sortaltrow" : " sortrow");
                    }
                }
            } 
        }

1 个答案:

答案 0 :(得分:1)

我没有设置gridview的datasourceid时遇到过这个问题。也许这可能有所帮助。

http://forums.asp.net/p/1074688/1575948.aspx#1575948