我在rowbound事件定义如下在GridView中排序箭头不工作
<asp:GridView ID="MyDataGridView" runat="server" CellPadding="4" GridLines="None"
AutoGenerateColumns="False" EmptyDataText="No records found" AllowSorting="True"
AllowPaging="True" PageSize="2"
OnPageIndexChanging="MyDataGridView_OnPageIndexChanging" OnSorting="MyDataGridView_OnSorting" OnRowDataBound="MyDataGridView_OnRowDataBound">
<Columns>
<asp:BoundField DataField="CreatedDate" HeaderText="CreatedDate" SortExpression="CreatedDate" />
<asp:BoundField DataField="NameOfCity" HeaderText="Cityname" />
</Columns>
</asp:GridView>
然后我写了下面的代码的GridView
protected void MyDataGridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
string imgAsc = @" <img src='../Images/asc.gif' border='0' title='Ascending' />";
string imgDes = @" <img src='../Images/desc.gif' border='0' title='Descendng' />";
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell cell in e.Row.Cells)
{
LinkButton lnkbtn = (LinkButton)cell.Controls[0];
if (lnkbtn.Text == MyDataGridView.SortExpression)
{
if (DataGridView.SortDirection == SortDirection.Ascending)
{
lnkbtn.Text += imgAsc;
}
else
lnkbtn.Text += imgDes;
}
}
}
}
但是,当运行代码,然后我得到错误“指定的参数超出有效值的范围参数名称:索引“
当我调试我发现我得到这个错误在foreach循环里面,当它进入第二次。 它就行LinkButton lnkbtn = (LinkButton)cell.Controls[0];
即使我向前移动光标并跳过错误,我仍然无法看到向下的箭头。我究竟做错了什么?
你在'NameOfCity'数据字段 – mhmtztmr
@mhmtztmr我不需要分拣Nameofcity – Happy