nettoon493
nettoon493

Reputation: 18433

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

Upvotes: 0

Views: 8040

Answers (3)

Vijay Kumbhoje
Vijay Kumbhoje

Reputation: 1441

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

Upvotes: 0

dadymax
dadymax

Reputation: 1

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

Upvotes: 0

Vistari
Vistari

Reputation: 707

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.

Upvotes: 1

Related Questions