开发者

GridView Sort Expression must return not empty data

开发者 https://www.devze.com 2022-12-21 09:34 出处:网络
My program running good. But i try to add up-down icon on my gridView header. but if i make it, My break point should drop below \"if comparison\" For example; field.SortExpression=Category but every

My program running good. But i try to add up-down icon on my gridView header. but if i make it, My break point should drop below "if comparison" For example; field.SortExpression=Category but every time CustomersGridView.SortExpression is empty when gridview.SortExp is not empty.

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

i need :


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

CustomersGridView.SortExpression must not be empty!!!!!

MY source:

<head runat="server">
    <title></title>
     <link type="text/css" href="StyleSheet.css" rel="styleshee开发者_开发知识库t" />
</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");
                    }
                }
            } 
        }


I've seen this problem when the gridview's datasourceid isn't set. Perhaps this might help.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消