简体   繁体   中英

error when trying to display 1 as YES from database

I have a column in a database called ActDefect that displays null or 1. In my GridView I want it to display YES instead of 1, and NO if its null. But I am getting this error:

Specified argument was out of the range of valid values. Parameter name: index

Here is the code for ActDefect:

<asp:BoundField DataField="ActDefect" HeaderText="Defects" SortExpression="ActDefect"></asp:BoundField>

int index = gv.Columns.HeaderIndex("ActDefect");
if (main.ActDefect != null)
{
      e.Row.Cells[index].Text = "YES";
}
else
{
      e.Row.Cells[index].Text = "NO";
}

it says the error is with the line e.Row.Cells[index].Text = "YES";

You can try changing this

int index = gv.Columns.HeaderIndex("ActDefect");

to this

int index = gv.Columns.HeaderIndex("Defects");

also, if that doesn't work, instead of:

<asp:BoundField DataField="ActDefect" HeaderText="Defects" SortExpression="ActDefect"></asp:BoundField>

write this

<asp:TemplateField HeaderText="Defects">
    <ItemTemplate>
        <asp:Label Text='<%# String.IsNullOrEmpty(Eval("ActDefect").ToString())  ? "No" : "Yes" %>' runat="server"  id="myID"/>
    </ItemTemplate>
</asp:TemplateField>

and you can delete your C# code

Have you debbuged and checked the value of "index" before it reaches the IF statement? it might be a problem of determining the column with the header ActDefect in your gridview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM