Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Sir,

I have a database with a table it has two columns (Name,Id). I wrote a code to customize the sorting for a grid view. Please see the below code :
C#
<asp:GridView ID="gv1" DataSourceID="sql1" AutoGenerateSelectButton="true" DataKeyNames="Name,Id" AllowSorting="true" CssClass="gridview" SelectedRowStyle-CssClass="selectedRow" OnRowDataBound="rowdatabound" runat="server" />
  <asp:SqlDataSource id="sql1" runat="server"  SelectCommand="select * from contact" ConnectionString="Data Source=.\SQLEXPRESS;AttachDBFileName=|DataDirectory|Database1.mdf; User Instance=True; Integrated Security=True"/>

The Code Behind is :
C#
public void rowdatabound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.Header)
          {
              foreach (TableCell cell in e.Row.Cells)
              {
                  LinkButton sortlink = (LinkButton)cell.Controls[0]; -------1
                  if (sortlink.Text == gv1.SortExpression)
                  {
                      if (gv1.SortDirection == SortDirection.Ascending)
                      {
                          sortlink.Text = "Ascending";
                      }
                      else
                      {
                          sortlink.Text = "Descending";
                      }

                  }

              }

          }

      }

But the error i got was index out of bounds at line number 1.

Please help,
Thanks
Posted
Updated 8-Sep-13 18:23pm
v2

 
Share this answer
 
Comments
Rahul VB 9-Sep-13 6:30am    
Thanks sir,

very helpful
jaideepsinh 9-Sep-13 6:53am    
Thank's,
Please accept as answer.
Rahul VB 9-Sep-13 23:56pm    
done
Hi Rahul,

Try this code.
C#
DataControlFieldHeaderCell sortlink = (DataControlFieldHeaderCell)cell;


The reason your code in not working properly b'coz you have turned on
C#
AutoGenerateSelectButton="true"
.

If you remove
C#
AutoGenerateSelectButton="true"
your existing code will work perfectly, I mean without any errors or exceptions.

Hope this helps!
 
Share this answer
 
Comments
Rahul VB 9-Sep-13 6:19am    
Hello Sir,
Thanks a lot for your solution it worked. Can you please tell me why removing:
AutoGenerateSelectButton="true" option makes the code work?

Thanks a lot
bitofweb 9-Sep-13 9:21am    
Hi Rahul,
I think this is because,when you set the AutoGenerateSelectButton = true then one extra column is added dynamically (as you already know) and the value of that column is __doPostBack(eventTarget, eventArgument), so user should not be able to modify the column cell 's values.

Sample of generated code
<td>Select</td><td>Suits</td><td>0</td>
Rahul VB 9-Sep-13 23:57pm    
thanks sir,
very nice
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900