Grid VIew的OnRowDataBound事件 [英] OnRowDataBound event of Grid VIew

查看:109
本文介绍了Grid VIew的OnRowDataBound事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好先生,



我有一个数据库,有一个表有两列(Name,Id)。我编写了一个代码来自定义网格视图的排序。请参阅以下代码:

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 :

<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 :

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";
                      }

                  }

              }

          }

      }



但我得到的错误是第1行的索引超出范围。



请帮助,

谢谢


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

Please help,
Thanks

推荐答案

请参阅此链接,希望能解决您的问题:



http://www.c-sharpcorner.com /UploadFile/deepak.sharma00/gridview-paging-and-sorting-in-Asp-Net-using-sqldatasource/ [ ^ ]



使用对象数据源优化分页和排序 [<小时ef =http://www.codeproject.com/Articles/42043/Optimized-Paging-and-Sorting-using-Object-Data-Soutarget =_ blanktitle =New Window> ^ ]



网格视图使用向上和向下图标排序,分页 [ ^ ]
Refer this link hope that one solve your problem :

http://www.c-sharpcorner.com/UploadFile/deepak.sharma00/gridview-paging-and-sorting-in-Asp-Net-using-sqldatasource/[^]

Optimized Paging and Sorting using Object Data Source[^]

Gridview Sorting with Up and Down Icons, Paging[^]


嗨Rahul,



试试这个代码。

Hi Rahul,

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





您的代码无法正常工作的原因b'coz您已打开



The reason your code in not working properly b'coz you have turned on

AutoGenerateSelectButton="true"





如果你删除

.

If you remove

AutoGenerateSelectButton="true"

您现有的代码将完美运行,我的意思是没有任何错误或例外。



希望这会有所帮助!

your existing code will work perfectly, I mean without any errors or exceptions.

Hope this helps!


这篇关于Grid VIew的OnRowDataBound事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆