C# 我哪里做错了?控制gridview列值

C# 我哪里做错了?控制gridview列值,c#,asp.net,C#,Asp.net,上面是我的网格视图中的一列。我们将此列称为“x” 我试图控制.cs文件中x的值,如下所示: protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.Cells[8].Text.Equals("0")) { e.Row.Cells[13].Text = "0%"; return;

上面是我的网格视图中的一列。我们将此列称为“x”

我试图控制.cs文件中x的值,如下所示:

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.Cells[8].Text.Equals("0"))
        {
            e.Row.Cells[13].Text = "0%";
            return;
        }


        int p,q;
    GridViewRow item = e.Row;
    SqlConnection con = new SqlConnection(connectionstring.ToString());
    string selectSQL = "  SELECT COUNT(*) AS 'Count' FROM Analysed WHERE runId =@myvar group by runId";
    SqlCommand cmd = new SqlCommand(selectSQL, con);
    cmd.Parameters.AddWithValue("@myvar", item.Cells[0].Text);
    SqlDataReader reader;
    try
    {
        con.Open();
        Int32.TryParse(item.Cells[8].Text, out p);
        reader = cmd.ExecuteReader();
        if (reader.HasRows)
        {
            reader.Read();
            Int32.TryParse( reader["Count"].ToString(),out q);
            item.Cells[13].Text =(q/p).ToString() + "%";

        }
        reader.Close();

    }


     }
我收到一个异常,表示System.Web.dll中发生了“System.ArgumentOutOfRangeException”类型的异常,但未在用户代码中处理其他信息:指定的参数超出了有效值的范围。这在以下行中发生:

if (e.Row.Cells[8].Text.Equals("0"))
        {
            e.Row.Cells[13].Text = "0%";
            return;
        }
有人能帮忙吗

编辑:

GridView代码:

 <asp:GridView ID = "GridView2" runat = "server" HorizontalAlign="Center" 
            DataSourceID = "source" AutoGenerateColumns = "False"  AllowPaging="True"  OnRowDataBound="GridView2_RowDataBound">

                <Columns> 
                <asp:HyperLinkField DataNavigateUrlFields="runId" DataTextField="runId" HeaderText = "RUN ID"  ControlStyle-CssClass="hlink" DataNavigateUrlFormatString="runanalysis.aspx?runId={0}" ItemStyle-Width="5%" ItemStyle-Font-Underline="true" />
                <asp:HyperLinkField DataField="link" HeaderStyle-ForeColor="White" HeaderStyle-Font-Underline="true"  ItemStyle-Width="10%" ItemStyle-Font-Underline="true"/>
                <asp:BoundField DataField="Family" HeaderText="Product Family" ItemStyle-Width="7%" />
                 <asp:BoundField DataField = "Date"   DataFormatString="{0:MM/dd/yy}" ItemStyle-Width="7%"/>
                <asp:BoundField DataField = "Number"  ItemStyle-Width="5%"/>
                <asp:BoundField DataField = "Total"  ItemStyle-Width="7%" />
                <asp:BoundField DataField="Pass" ItemStyle-Width="7%" HeaderText="Pass Percent" DataFormatString="{0}%" />
                <asp:BoundField DataField="pass"  ItemStyle-Width="7%"/>
                <asp:BoundField DataField="fail"  ItemStyle-Width="7%"/>
                <asp:BoundField DataField="Owner" ItemStyle-Width="7%"/>
                 <asp:BoundField DataField="Lang"  ItemStyle-Width="5%"/>
                <asp:BoundField DataField="Plat"  ItemStyle-Width="7%"/>
                <asp:BoundField DataField="Flavor"  ItemStyle-Width="7%"/>
              <asp:HyperLinkField DataNavigateUrlFields="runId" DataTextField="Percent" ControlStyle-CssClass="hlink" HeaderText="% SEEN" ItemStyle-Width="6%" DataNavigateUrlFormatString="run.aspx?runId={0}" ItemStyle-Font-Underline="true"/>
              <asp:BoundField DataField="AutomationType" HeaderText ="Automation Type" ItemStyle-Width="7%"/>


                 </Columns>
            </asp:GridView>

GridView2
中的单元格数量可能少于您尝试访问的单元格数量。我认为
单元格
(列)的数量是
13
,您必须使用索引12而不是
13
。最好在访问其中一个单元之前检查单元的数量

if (e.Row.Cells.Count > 12 && e.Row.Cells[8].Text.Equals("0"))
{
      e.Row.Cells[12].Text = "0%";
      return;
}
根据OP评论编辑

您正在尝试访问尚未在RowDataBound事件中分配的值。在这种情况下,您可以访问数据项并对其进行解析,而不是解析控件/单元格

改变


注意:其他列的值也是如此。

嗨,Adil…我也将用网格视图的代码编辑这个问题。我已经从0开始计数,你从哪一行得到异常?我注释掉了:if(e.Row.Cells[8].Text.Equals(“0”){e.Row.Cells[13].Text=“0%”return;}异常现在位于:Int32.TryParse(item.Cells[8].Text,out p);你知道阿迪尔吗?我完全不知道如何在Convert.ToInt32(DataBinder.Eval(e.Row.DataItem,“ColumnFromDatabase”);我在哪里写item.cells[8]?
Int32.TryParse(item.Cells[8].Text, out p); 
p = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ColumnFromDatabase"));