我哪里错了?控制gridview列值

时间:2015-03-12 04:32:29

标签: c# asp.net

<asp:HyperLinkField DataNavigateUrlFields="runId" DataTextField="Percent" ControlStyle-CssClass="hlink" HeaderText="% SEEN" ItemStyle-Width="6%" DataNavigateUrlFormatString="run.aspx?runId={0}" ItemStyle-Font-Underline="true"/&GT;

以上是我的网格视图中的一列。我们可以将此列称为“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>

1 个答案:

答案 0 :(得分:0)

GridView2中的单元格数可能少于您尝试访问的单元格数。我认为cells(列)的数量为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事件中尚未分配的值。在这种情况下,您可以访问数据项并解析它而不是解析控件/单元格。

更改

Int32.TryParse(item.Cells[8].Text, out p); 

p = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ColumnFromDatabase"));

注意: 其他列值相同。