Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GridView排序表达式必须返回非空数据_C#_.net_Asp.net_Gridview - Fatal编程技术网

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

C# GridView排序表达式必须返回非空数据,c#,.net,asp.net,gridview,C#,.net,Asp.net,Gridview,我的程序运行良好。但我尝试在gridView标题上添加上下图标。但如果我成功了,我的断点应该下降到“如果比较”以下;field.SortExpression=类别,但每次当gridview.SortExpression不为空时,CustomerGridView.SortExpression都为空 foreach (DataControlField field in CustomersGridView.Columns) { if (field.SortExpression ==

我的程序运行良好。但我尝试在gridView标题上添加上下图标。但如果我成功了,我的断点应该下降到“如果比较”以下;field.SortExpression=类别,但每次当gridview.SortExpression不为空时,CustomerGridView.SortExpression都为空

foreach (DataControlField field in CustomersGridView.Columns)
    {
      if (field.SortExpression == CustomersGridView.SortExpression)
      {
        return CustomersGridView.Columns.IndexOf(field);
      }
    }
我需要: CustomerGridView.SortExpression不能为空

我的消息来源:
当gridview的datasourceid没有设置时,我看到了这个问题。也许这会有帮助


当gridview的数据源ID未设置时,我看到了这个问题。也许这会有帮助


这是另一个问题。我需要真正的帮助如果没有gridview的aspx源代码,很难判断您是如何绑定的。你能发布它吗?我刷新了我的帖子。请看上面!啊,你没有提到Ajax,updatepanel有很大的不同。尝试在scriptmanager中添加EnablePartialRendering=“true”;它不起作用。İt与上述C#代码有关。我在上面添加了一些代码…这是另一个问题。我需要真正的帮助如果没有gridview的aspx源代码,很难判断您是如何绑定的。你能发布它吗?我刷新了我的帖子。请看上面!啊,你没有提到Ajax,updatepanel有很大的不同。尝试在scriptmanager中添加EnablePartialRendering=“true”;它不起作用。İt与上述C#代码有关。我在上面添加了一些代码。。。

foreach (DataControlField field in CustomersGridView.Columns)
    {
      if (field.SortExpression == "Category")
      {
        return 2;
      }
    }
<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");
                    }
                }
            } 
        }