0

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. ;)

nettoon493
  • 17,733
  • 7
  • 30
  • 45

3 Answers3

1

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.

Vistari
  • 697
  • 9
  • 21
0

Error message tells you that in GridView there is no cell with index of 10. Ensure that Cells array is large enought. May be should try

e.Row.Cells[e.Row.Cells.Length-1].Text = "-";
dadymax
  • 1
  • 2
  • This will not take intended row it always get value in last cell of the row – Vijay Kumbhoje Nov 26 '15 at 08:43
  • Yeap. But this will always work (unless Cells are empty array) and not throw specified exception. Anyway the problem in topic code - out of array bounds. How to solve - depends of hidden code. – dadymax Nov 27 '15 at 15:20
0

First of all check if there is 9th column (which is e.Row.Cells[10] starting from 0 ) exist in selected Row.

In case you want to display specific value in GridView cell use below methis with TemplateField of Gridview

 <asp:TemplateField HeaderText="Gender">
         <ItemTemplate>
               <asp:Label runat="server" Text='<% #GetLangID(Eval("langid2")) %>' />
         </ItemTemplate>
 </asp:TemplateField>

This will be your Code Behind

public string GETLangID(object dataItem)
{
    string text = string.Empty;
    int? val = dataItem as int?;
    switch (val)
    {
        case 0:
            text = "-";
            break;
        case 1:
            text = "One";
            break;
        case 2:
            text = "two";
            break;

    }
    return text;
}
Vijay Kumbhoje
  • 1,401
  • 2
  • 25
  • 44