Search code examples
c#asp.netgridviewerror-handlingrowdatabound

Error: Specified argument was out of the range of valid values. in RowDataBound Gridview


I want Bind data gridview and change data '0' to '-' . but error: Specified argument was out of the range of valid values. Parameter name: index

Code asp

 <asp:GridView ID="GridView1" runat="server" 
  AutoGenerateColumns="False" AllowPaging="true" 
  OnRowCreated="GridView1_RowCreated"  
  OnRowDataBound="GridView1_RowDataBound" >
   <Columns>
     <asp:BoundField DataField="amt" HeaderText="Total" 
          DataFormatString="{0:#,##0.00000}" ItemStyle-HorizontalAlign="Right" />
   </Columns>               
 </asp:GridView>

Code C#

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {                
             string sAmt = DataBinder.Eval(e.Row.DataItem, "amt").ToString();    

          if (sAmt == "0.00000")
          {
             e.Row.Cells[10].Text = "-"; <<< Line Error
          }
     }
 }

If fields AMT Show 0.

I want instead "0" to "-" .

Help me please. Thanks advance for time. ;)


Solution

  • Is the index of 10 in:

    e.Row.Cells[10].Text = "-"; <<< Line Error
    

    valid? I mean are there 11 items in the array, you might be trying to access something outside of the range of the array.