C# 使用C根据条件更改Gridview上的单元格颜色#

C# 使用C根据条件更改Gridview上的单元格颜色#,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在尝试改变电池的颜色,如果平衡小于零,则将其变为红色。我得到这个错误: Input string was not in a correct format. 这是我的gridview <asp:GridView ID="gvTest" runat="server" Width="700px" CssClass="table table-hover table-bordered table-responsive" OnRowDataBound = "OnRowDataBound"

我正在尝试改变电池的颜色,如果平衡小于零,则将其变为红色。我得到这个错误:

Input string was not in a correct format.
这是我的gridview

<asp:GridView ID="gvTest" runat="server" Width="700px" CssClass="table table-hover table-bordered table-responsive" OnRowDataBound = "OnRowDataBound"
                        AutoGenerateColumns="false" DataKeyNames="ID"
                        AllowPaging="true"
                        OnPageIndexChanging="OnPaging_gvBookKeeping"
                        PageSize="25">
                        <Columns>
                          <asp:BoundField DataField="ID" HeaderText="ID" HtmlEncode="true" />
                          <asp:BoundField DataField="FullName" HeaderText="Name" HtmlEncode="true" />

                          <asp:BoundField DataField="Remaining_Ballance" DataFormatString="{0:C0}" HeaderText="Remaining Ballance" HtmlEncode="true" />
                          <asp:BoundField DataField="Note" HeaderText="Note" HtmlEncode="true" />
                          <asp:BoundField DataField="fully_paid" HeaderText="Fully Paid" HtmlEncode="true" />

                          <asp:TemplateField ItemStyle-Width="30px" HeaderText="Edit Link">
                            <ItemTemplate>
                              <asp:LinkButton ID="lnkEdit" CausesValidation="false" runat="server" Text="Edit" OnClick="Edit"></asp:LinkButton>
                            </ItemTemplate>
                          </asp:TemplateField>
                        </Columns>
                        <HeaderStyle BackColor="#E5E5E5" />

                        <PagerSettings Position="TopAndBottom" />
                        <PagerStyle BackColor="#CCCCCC" ForeColor="#FF3300" HorizontalAlign="Center" />
                      </asp:GridView>

余额列的数据类型为十进制(10,2)

看起来您应该有
TableCell cell=e.Row.Cells[2]而不是
TableCell cell=e.Row.Cells[3]

列索引从0开始,而不是从1开始

在您的例子中,这意味着您正试图将
Notes
字段的值解析为
Int


编辑

您还可能会发现,
DataFormatString=“{0:C0}”
使用货币符号(例如$/£)格式化数字,并且不能将该字符转换为整数


按照建议添加断点将帮助您识别此问题。

您还可以在int.parse语句周围使用Try{}catch{}块,这样错误转换就不会破坏代码

但这并不意味着单元格格式正确,只是它不会破裂

或者,使用int.TryParse方法,这通常是更好的解决方案。特里帕斯绝对更快。这有点让人困惑,下面是一个例子

// See if we can parse the 'text' string.
    // If we can't, TryParse will return false.
    // Note the "out" keyword in TryParse.
    string text1 = "x";
    int num1;
    bool res = int.TryParse(text1, out num1);
    if (res == false)
    {
        // String is not a number.
    }

    // Use int.TryParse on a valid numeric string.
    string text2 = "10000";
    int num2;
    if (int.TryParse(text2, out num2))
    {
        // It was assigned.
    }

尝试将代码更改为

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{

  if (e.Row.RowType == DataControlRowType.DataRow)
  {
        dynamic dataItem = e.Row.DataItem; 
        if(dataItem != null && dataItem.Remaining_Ballance < 0)
        {
             e.Row.Cells[2].BackColor = Color.Red;
        }    
    }
}
受保护的void OnRowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
动态数据项=e.Row.dataItem;
if(dataItem!=null&&dataItem.Remaining\u balance<0)
{
e、 Row.Cells[2]。BackColor=Color.Red;
}    
}
}

注意:我使用dataItem的动态数据类型,因为我不知道您的实际对象类型。您应该使用实际的数据类型,并且还需要进行转换。

谢谢,我确实纠正了这一点,但仍然得到相同的错误,您需要在
int ballance=int.Parse(cell.Text)上设置断点行,以便您可以检查
单元格。Text
值。。。这将使您更好地了解哪些值不能解析为int。您是否正在尝试int。解析具有货币符号的内容?您是否尝试在“int ballance=int.Parse(cell.text);”行上设置调试断点并在抛出错误时检查cell.text的值?我已删除但仍不工作我收到此错误:System.Data.DataRowView“未包含“剩余”的定义\u Balance是否已将dataItem转换为要将gridview绑定到的对象?你能告诉我你的代码在哪里绑定你的gridView数据源,这样我就可以相应地修改我的代码了。
// See if we can parse the 'text' string.
    // If we can't, TryParse will return false.
    // Note the "out" keyword in TryParse.
    string text1 = "x";
    int num1;
    bool res = int.TryParse(text1, out num1);
    if (res == false)
    {
        // String is not a number.
    }

    // Use int.TryParse on a valid numeric string.
    string text2 = "10000";
    int num2;
    if (int.TryParse(text2, out num2))
    {
        // It was assigned.
    }
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{

  if (e.Row.RowType == DataControlRowType.DataRow)
  {
        dynamic dataItem = e.Row.DataItem; 
        if(dataItem != null && dataItem.Remaining_Ballance < 0)
        {
             e.Row.Cells[2].BackColor = Color.Red;
        }    
    }
}