0

我正在尝试根据条件对背景颜色更改 GridView 行使用脚本。它适用于所有具有值的单元格,但相同的脚本生成错误

“异常详细信息:System.FormatException:输入字符串的格式不正确”

当一个或多个单元格为空 (NULL) 时。请建议,怎么办?我正在使用以下代码:

<script runat="server">

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        int D_inshed = int.Parse(e.Row.Cells[11].Text);



        foreach (TableCell cell in e.Row.Cells)
        {

            if (D_inshed >= 6)
            {

                cell.BackColor = System.Drawing.Color.Orange;

            }



            }

        }

    }

  </script>

   <asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager>
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                        BackColor="White" BorderColor="#CC9966" BorderStyle="Solid" BorderWidth="1px" 
                        CellPadding="4" DataKeyNames="ID" 
                        DataSourceID="SqlDataSource1"  AllowSorting="True" 
                        OnRowDataBound="OnRowDataBound" Width="100%" >
                        <Columns>

                        <asp:TemplateField HeaderText="">
                            <ItemTemplate>
                                <%# Container.DataItemIndex + 1 %>
                            </ItemTemplate>
                        </asp:TemplateField>
                            <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" 
                                SortExpression="ID" Visible="false" ></asp:BoundField>
                            <asp:TemplateField HeaderText="Loco no." SortExpression="Loco_no">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Loco_no") %>' Width="80px"></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Loco_no") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Type of Loco" SortExpression="Loco_type">
                                <EditItemTemplate>
                                <asp:DropDownList ID="TextBox2" runat="server" Text='<%# Bind("Loco_type") %>' Width="80px">
                                <asp:ListItem Text="WAP-4" Value="WAP4"></asp:ListItem>
                                <asp:ListItem Text="WAP-5" Value="WAP5"></asp:ListItem>
                                <asp:ListItem Text="WAP-7" Value="WAP7"></asp:ListItem>
                                <asp:ListItem Text="WAG-7" Value="WAG7"></asp:ListItem>
                                <asp:ListItem Text="WAG-9" Value="WAG9"></asp:ListItem>
                                <asp:ListItem Text="WAP-7 BHEL" Value="WAG7BHEL"></asp:ListItem>
                                <asp:ListItem Text="" Value=""></asp:ListItem>
                                </asp:DropDownList>

                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Loco_type") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Nominated Rly/Shed" SortExpression="rly_shed">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="TextBox3" runat="server" Text='<%# Bind("rly_shed") %>' Width="80px">
                                <asp:ListItem Text="BSL" Value="BSL/CR"></asp:ListItem>
                                <asp:ListItem Text="AQ" Value="AQ/CR"></asp:ListItem>
                                <asp:ListItem Text="KYN" Value="KYN/CR"></asp:ListItem>
                                <asp:ListItem Text="ASN" Value="ASN/ER"></asp:ListItem>
                                <asp:ListItem Text="HWH" Value="HWH/ER"></asp:ListItem>
                                <asp:ListItem Text="MGS" Value="MGS/ECR"></asp:ListItem>
                                <asp:ListItem Text="GMO" Value="GMO/ECR"></asp:ListItem>
                                <asp:ListItem Text="WAT" Value="WAT/ECoR"></asp:ListItem>
                                <asp:ListItem Text="ANGL" Value="ANGL/ECoR"></asp:ListItem>
                                <asp:ListItem Text="GZB" Value="GZB/NR"></asp:ListItem>
                                <asp:ListItem Text="LDH" Value="LDH/NR"></asp:ListItem>
                                <asp:ListItem Text="JHS" Value="JHS/NCR"></asp:ListItem>
                                <asp:ListItem Text="CNB" Value="CNB/NCR"></asp:ListItem>
                                <asp:ListItem Text="AJJ" Value="AJJ/SR"></asp:ListItem>
                                <asp:ListItem Text="ED" Value="ED/SR"></asp:ListItem>
                                <asp:ListItem Text="RPM" Value="RPM/SR"></asp:ListItem>
                                <asp:ListItem Text="BZA" Value="BZA/SCR"></asp:ListItem>
                                <asp:ListItem Text="LGD" Value="LGD/SCR"></asp:ListItem>
                                <asp:ListItem Text="KZJ" Value="KZJ/SCR"></asp:ListItem>
                                <asp:ListItem Text="TATA" Value="TATA/SER"></asp:ListItem>
                                <asp:ListItem Text="BNDM" Value="BNDM/SER"></asp:ListItem>
                                <asp:ListItem Text="BKSC" Value="BKSC/SER"></asp:ListItem>
                                <asp:ListItem Text="SRC" Value="SRC/SER"></asp:ListItem>
                                <asp:ListItem Text="BIA" Value="BIA/SECR"></asp:ListItem>
                                <asp:ListItem Text="BRC" Value="BRC/WR"></asp:ListItem>
                                <asp:ListItem Text="BL" Value="BL/WR"></asp:ListItem>
                                <asp:ListItem Text="TKD" Value="TKD/WCR"></asp:ListItem>
                                <asp:ListItem Text="ET" Value="ET/WCR"></asp:ListItem>
                                <asp:ListItem Text="NKJ" Value="NKJ/WCR"></asp:ListItem>
                                <asp:ListItem Text="" Value=""></asp:ListItem>
                                </asp:DropDownList>

                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("rly_shed") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle Wrap="false" Width="80px"/>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Turned out Date" SortExpression="DO_clwout">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("DO_clwout", "{0:dd-MMM-yy}") %>' Width="80px"></asp:TextBox>
                                    <asp:CalendarExtender ID="CalendarExtender1" TargetControlID="TextBox4" Format="dd-MMM-yy" runat="server">
                                    </asp:CalendarExtender>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label4" runat="server" 
                                        Text='<%# Bind("DO_clwout", "{0:dd-MMM-yy}") %>'></asp:Label>
                                </ItemTemplate>
                               <ItemStyle Wrap="false" Width="50px"/>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Shed In Date" SortExpression="DO_shedIN">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("DO_shedIN", "{0:dd-MMM-yy}") %>' Width="80px"></asp:TextBox>
                                    <asp:CalendarExtender ID="CalendarExtender4" TargetControlID="TextBox5" Format="dd-MMM-yy" runat="server">
                                    </asp:CalendarExtender>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label5" runat="server" 
                                        Text='<%# Bind("DO_shedIN", "{0:dd-MMM-yy}") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle Wrap="false" Width="50px"/>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Date of Commissioning" SortExpression="DO_comm" Visible="false">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("DO_comm", "{0:dd-MMM-yy}") %>' Width="80px"></asp:TextBox>
                                    <asp:CalendarExtender ID="CalendarExtender5" TargetControlID="TextBox6" Format="dd-MMM-yy" runat="server">
                                    </asp:CalendarExtender>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label6" runat="server" 
                                        Text='<%# Bind("DO_comm", "{0:dd-MMM-yy}") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle Wrap="false" Width="50px"/>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Remarks" SortExpression="Remarks">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox61" runat="server" Text='<%# Bind("Remarks") %>' Width="200px" TextMode="MultiLine" Height="70px"></asp:TextBox>
                                 </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label61" runat="server" 
                                        Text='<%# Bind("Remarks") %>' ></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="PDC" SortExpression="PDC">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox66" runat="server" Text='<%# Bind("PDC", "{0:dd-MMM-yy}") %>' Width="80px"></asp:TextBox>
                                    <asp:CalendarExtender ID="CalendarExtender7" TargetControlID="TextBox66" Format="dd-MMM-yy" runat="server">
                                    </asp:CalendarExtender>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label66" runat="server" 
                                        Text='<%# Bind("PDC", "{0:dd-MMM-yy}") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle Wrap="false" Width="50px"/>
                            </asp:TemplateField>
                            <asp:BoundField DataField="D_CLW" HeaderText="No. of Days after turned out" ReadOnly="True" SortExpression="D_CLW"  ></asp:BoundField>
                            <asp:TemplateField HeaderText="No. of Days after Shed IN" 
                                SortExpression="D_inshed">
                                <ItemTemplate>
                                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("D_inshed") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("D_inshed") %>'></asp:Label>
                                </EditItemTemplate>
                            </asp:TemplateField>
                           </Columns>



                        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                        <HeaderStyle BackColor="#990000" Font-Bold="True" Font-Names="Tahoma" 
                            Font-Size="Small" ForeColor="#FFFFCC" HorizontalAlign="Justify" 
                            VerticalAlign="Middle" Wrap="true" />
                        <PagerStyle ForeColor="#330099" HorizontalAlign="Center" BackColor="#FFFFCC" />
                        <RowStyle BackColor="White" Font-Names="Tahoma" Font-Size="Smaller" 
                            ForeColor="Black" HorizontalAlign="Center" VerticalAlign="Middle" />
                        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                        <sortedascendingcellstyle backcolor="#FEFCEB" />
                        <sortedascendingheaderstyle backcolor="#AF0101" />
                        <sorteddescendingcellstyle backcolor="#F6F0C0" />
                        <sorteddescendingheaderstyle backcolor="#7E0000" />
                    </asp:GridView>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server"  CancelSelectOnNullParameter="false"
    ConnectionString="<%$ ConnectionStrings:Loco_bankConnectionString %>" 
    DeleteCommand="DELETE FROM [Allotment] WHERE [ID] = @ID" 
    InsertCommand="INSERT INTO [Allotment] ([ID], [Loco_no], [DO_clwout], [Loco_type], [rly_shed], [DO_shedIN], [DO_comm], [Remarks], [PDC]) VALUES (@ID, @Loco_no, @DO_clwout, @Loco_type, @rly_shed, @DO_shedIN, @DO_comm, @Remarks, @PDC)" 
    SelectCommand="SELECT [ID], [Loco_no], [DO_clwout], [Loco_type], [rly_shed], [DO_shedIN], [DO_comm], [Remarks], [PDC], 

    CAST(DATEDIFF(DAY, DO_clwout, Getdate()) AS varchar(4)) AS 'D_CLW', CAST(DATEDIFF(DAY, DO_shedIN, Getdate()) AS varchar(4)) AS 'D_inshed'


     FROM [Allotment] WHERE ([DO_clwout] IS NOT NULL AND [DO_comm] IS NULL) ORDER BY DO_clwout"    
4

1 回答 1

1

考虑为该行提供 css 类

if (e.Row.RowType == DataControlRowType.DataRow)
{
  if (true) // your condition
   {
      e.Row.CssClass = "MyCSSClas"; 
   }
}
于 2013-08-13T13:10:30.517 回答