C# 试图从数据库中将1显示为“是”时出错

C# 试图从数据库中将1显示为“是”时出错,c#,asp.net,visual-studio,C#,Asp.net,Visual Studio,我在数据库中有一个名为ActDefect的列,它显示null或1。 在我的GridView中,我希望它显示YES而不是1,如果为null,则显示NO。 但我得到了这个错误: 指定的参数超出了有效值的范围。 参数名称:索引 以下是ActDefect的代码: <asp:BoundField DataField="ActDefect" HeaderText="Defects" SortExpression="ActDefect"></asp:BoundField> int i

我在数据库中有一个名为ActDefect的列,它显示null或1。 在我的GridView中,我希望它显示YES而不是1,如果为null,则显示NO。 但我得到了这个错误:

指定的参数超出了有效值的范围。 参数名称:索引

以下是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";
}

int index=gv.Columns.HeaderIndex(“ActDefect”);
if(main.ActDefect!=null)
{
e、 行。单元格[索引]。Text=“是”;
}
其他的
{
e、 行。单元格[索引]。Text=“否”;
}

它表示错误出现在行
e.Row.Cells[index].Text=“YES”中

在“index”到达IF语句之前,您是否已清除并检查其值?确定gridview中标题为ActDefect的列可能有问题

您可以尝试更改此选项

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

int index = gv.Columns.HeaderIndex("Defects");
此外,如果这不起作用,请不要:

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

写这个

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


您可以删除您的C代码

索引的值是多少?
索引的值是0