3

1.TimeReceivedがNullの場合は赤(または)2。TimeReceivedがnullではなくTimeReadがNullの場合はオレンジ(または)3。TimeReceivedがnullでない場合は緑

エラーをスローします

Input string was not in a correct format. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error: 


Line 86:         {
Line 87:             Image img = (Image)e.Row.FindControl("image1");
Line 88:             switch (int.Parse(e.Row.Cells[1].Text))
Line 89:             {
Line 90:                 case 0:

どこがおかしいのですが、どうすれば状況に応じて画像を表示できますか。rowdataboundを正しく実行していないと思います。助けてください。

4

4 に答える 4

2

nullまたは空の文字列をintとして解析しようとしている可能性があります。int.Parse行を次のように変更します。

switch (int.Parse(string.IsNullOrEmpty(e.Row.Cells[1].Text)?"0":e.Row.Cells[1].Text))

更新: グリッドがどのように見えるかの実際の画像を貼り付けたので、Joel Ethertonは正しいと思います。あなたは、日付を整数として解析しようとしています。Cell [1](左側に非表示の列がないと仮定)は日付であり、整数ではないため、int.Parseを試行すると、解析できないため例外がスローされます。また、条件によっては、MyGrid_RowDataBoundロジックが正しくありません。実装をこれに変更してみてください。

protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Image img = (Image)e.Row.FindControl("image1");
        //condition for red image; Neither TimeReceived and TimeRead are populated
        if(string.IsNullOrEmpty(e.Row.Cells[1].Text) &&  
           string.IsNullOrEmpty(e.Row.Cells[2].Text))
        {
            img.ImageUrl = "/images/Red.gif";
            img.Visible = true;
        }
        //condition for amber image; TimeReceived not null and TimeRead is null
        else if (!string.IsNullOrEmpty(e.Row.Cells[1].Text) &&  
                 string.IsNullOrEmpty(e.Row.Cells[2].Text))
        {
            img.ImageUrl = "/images/Amber.gif";
            img.Visible = true;
        }
        //condition for green image; TimeReceived not null and TimeRead not null
        else if (!string.IsNullOrEmpty(e.Row.Cells[1].Text) &&  
                 !string.IsNullOrEmpty(e.Row.Cells[2].Text))
        {
           img.ImageUrl = "/images/Green.gif";
           img.Visible = true;
        }
        else //default case
        {
            img.Visible = false;
        }
    }
}
于 2011-09-12T14:04:20.147 に答える
1

ここで問題が発生していることがわかります。率直に言って、このコードは少し混乱しています。

switch(int.Parse(string.IsNullOrEmpty(e.Row.Cells[1].Text)?"0":e.Row.Cells[1].Text))

ここではあまりにも多くのことが役に立たないことがあります。まず、e.Row.Cells [1]はDateTimeを提供しているように見えるので、int.Parseはここで使用するのは絶対に間違っています。あなたが望むものについてのあなたの説明に基づいて、私はこれがどのようにそれを達成するのかわかりません。

これが私の刺し傷です:

Image img = (Image)e.Row.FindControl("image1");

DateTime received;
DateTime read;

DateTime.TryParse(e.Row.Cells[1].Text, received);   // If exception it will produce DateTime.MinValue
DateTime.TryParse(e.Row.Cells[2].Text, read);

if (received == DateTime.MinValue)
{
    img.ImageUrl = "/images/Red.gif";
}
else if (read == DateTime.MinValue)
{
    img.ImageUrl = "/images/Amber.gif";
}
else
{
    img.ImageUrl = "/images/Green.gif";
}

img.Visible = true;

適切な場合はifステートメントを使用します。あなたがやろうとしていることは日付を含むので、日付を使用してください。式を単純化して、読みやすくします。すべてを1行で行う必要はありません。switchステートメントに渡された式は、すべてを1回のショットで実行しすぎています。引き抜くことが不可能だと言っているわけではありませんが、生成されたエラーがどこから来ているのかについて、多くの灰色の領域が生成されます。

于 2011-09-12T14:38:54.267 に答える
1

これらの複雑なソリューションを理解するのに苦労したので、少し調査しました。ここに私のソリューションがあります。誰かを助けることを願ってここに残します

このようにグリッドコードを変更しました

<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="false" OnRowCommand="RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="Commands">
            <ItemTemplate><asp:ImageButton ID="btnDelete" Visible='<%# ActionPermitted("DELETE") %>' runat="server" ImageUrl="images/bin.png" ToolTip="Delete" CommandName="Delete" CommandArgument='<%# Eval("Id")%>' /></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="ASPImage">
            <ItemTemplate><asp:Image runat="server" ID="rowImage" ImageUrl='<%# ImageView(((System.Data.DataRowView) Container.DataItem).Row)%>' /></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="HTMLImage">
            <ItemTemplate><img src='<%# ImageView(((System.Data.DataRowView) Container.DataItem).Row)%>' /></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="SetVisible">
            <ItemTemplate>
                <img src="green.gif" style='display:<%# Eval("aColumn") == "Value1"? "inline":"none" %>' />
                <img src="red.gif" style='display:<%# Eval("aColumn") == "Value2"? "none":"inline" %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

私のコードビハインドには、次の機能があります

protected boolean ActionPermitted(string action){
  return (action=="Delete" || user == "admin");
}

protected string ImageView(DataRow row){
    if (row["SomeData"] == DBNull.Value){
        return "red.gif";
    } else if (row["SomeData"] != DBNull.Value && row["SomeOtherData"] == DBNull.Value){
        return "amber.gif";
    } else {
        return "green.gif";
    }
}

これは実際には 4 つの異なるソリューションであることに注意してください。

  • コード ビハインド関数から返された値に基づいて asp ボタンの表示を変更するには (二重引用符ではなく単一引用符に注意してください)
  • 現在の DataRow をコード ビハインドに渡し、それに基づいて結果を返すには
  • HTML タグのプロパティを変更するには (分離コードで生成されたデータを列に挿入するために使用できます)
  • 表示/非表示および HTML 画像 (任意のタグ)

最後の TemplateField は、質問に答えるものです。

于 2013-03-13T03:15:32.663 に答える
0

Icarusに同意しますが、int.Parseの代わりにint.TryParseを使用した方がよいでしょう。

Image img = (Image)e.Row.FindControl("image1");
int val = 0;
int.TryParse(e.Row.Cells[1].Text , out val);
        switch (int.Parse(e.Row.Cells[1].Text))
        {
            case 0:
                img.ImageUrl = "/images/Red.gif";
                img.Visible = true;
                break;
            case 1:
                img.ImageUrl = "/images/Amber.gif";
                img.Visible = true;
                break;
            case 2:
                img.ImageUrl = "/images/Green.gif";
                img.Visible = true;
                break;               
            default:
                img.Visible = false;
                break;
        }
于 2011-09-12T14:12:59.700 に答える