views:

386

answers:

1

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="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  A: 

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

Steve
it's another question. i need real help
Phsika
It's hard to tell without the aspx source for the gridview and how you're binding. Can you post it?
Steve
i refreshed my post. Please look above!
Phsika
Ah, you didn't mention Ajax, that updatepanel makes a big difference. Try adding EnablePartialRendering="true" in scriptmanager.
Steve
Dear Steve; it is not working. İt is related to above C# codes. i added some codes above...
Phsika
I don't think the rowdatabound event knows what the sort field is as that's triggered not by a databind, but by a sort event. If you're trying to change the look of header and cells, I would remove all the code from the event and just hardcode the column numbers: if (...) { e.Row.Cells[2].CssClass += ...; e.Row.Cells[3].CssClass += ...; } else{ e.Row.Cells[2].CssClass += ...; e.Row.Cells[3].CssClass += ...; } or instead of checking for a sortexpression, I'd check if the column was a boundfield.
Steve