正确的C#代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowIndex != -1 && ViewState["myDS"] != null){
            DataSet ds = (DataSet)ViewState["myDS"];
            for(int i=0;i<e.Row.Cells.Count;i++){
                if (i >=7 && i<=9)
                {
                    string str1 = ((Label)e.Row.Cells[i].Controls[1]).Text;
                    string str2 = "";
                    switch (i)
                    {
                        case 7:
                            str2 = ds.Tables[0].Rows[e.Row.RowIndex]["bet365_hwin"].ToString();
                            break;
                        case 8:
                            str2 = ds.Tables[0].Rows[e.Row.RowIndex]["bet365_draw"].ToString();
                            break;
                        case 9:
                            str2 =ds.Tables[0].Rows[e.Row.RowIndex]["bet365_awin"].ToString();
                            break;
                    }
                    if (str1 != str2)
                    {
                        double N1 = double.Parse(str1);
                        double N2 = double.Parse(str2);                        if (N1 != N2)
                        {
                            if (N1 > N2)
                            {
                                e.Row.Cells[i].BackColor = System.Drawing.Color.DarkSeaGreen;
                            }
                            else
                            {
                                e.Row.Cells[i].BackColor = System.Drawing.Color.Pink;
                            }
                        }                    }
                }
            }
        }
    }转成VB.NET代码后运行效果不同,代码如下:
 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowIndex <> -1 And Not (ViewState("myDS") Is Nothing) Then
            Dim ds As DataSet = CType(ViewState("myDS"), DataSet)
            Dim i As Integer
            For i = 0 To e.Row.Cells.Count - 1
                If i >= 7 And i <= 9 Then
                    Dim str1 As String = CType(e.Row.Cells(i).Controls(1), Label).Text
                    Dim str2 As String = ""
                    Select Case i
                        Case 7
                            str2 = ds.Tables(0).Rows(e.Row.RowIndex)("bet365_hwin").ToString()
                        Case 8
                            str2 = ds.Tables(0).Rows(e.Row.RowIndex)("bet365_draw").ToString()
                        Case 9
                            str2 = ds.Tables(0).Rows(e.Row.RowIndex)("bet365_awin").ToString()
                    End Select
                    If str1 <> str2 Then
                        Dim N1 As Double = Double.Parse(str1)
                        Dim N2 As Double = Double.Parse(str2)                        If N1 <> N2 Then
                            If N1 > N2 Then
                                e.Row.Cells(i).BackColor = System.Drawing.Color.Green
                            Else
                                e.Row.Cells(i).BackColor = System.Drawing.Color.Red
                            End If
                        End If
                    End If
                End If
            Next i
        End If
    End Sub我转成VB.NET后运行后和上面代码效果不一样,请问这段VB.NET代码哪里没转对,正确的应该是怎样的,谢谢!

解决方案 »

  1.   

    一个可以变颜色,一个不能变颜色,就是说下面的VB.NET代码实效了,上面的C#代码就可以,你帮忙看看是哪里错了,谢谢!
      

  2.   

    代码转换的貌似没有问题,你看看是不是这个地方少了东西:Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)在他的后面加上  Handles GridView1.RowDataBound
    否则的话,事件和代码没有绑定上!
      

  3.   

    加了Handles GridView1.RowDataBound 就可以了,但是
     If N1 > N2 Then
    e.Row.Cells(i).BackColor = System.Drawing.Color.Green
    Else
    e.Row.Cells(i).BackColor = System.Drawing.Color.Red
    End If
    这句
    e.Row.Cells(i).BackColor = System.Drawing.Color.Green 和
    e.Row.Cells(i).BackColor = System.Drawing.Color.Red
    不会变绿色或者红色,我断点看了都能看到n1 和 n2 的值。 C#代码就会改变颜色
    帮忙看下是怎么回事,谢谢!
      

  4.   

    在e.Row.Cells(i).BackColor = System.Drawing.Color.Green设断点,看看执行到没有
      

  5.   

    直接用工具转换就行啦:【ASP2ASPX 4.0 】代码转换工具
    http://dotnet.csdn.net/aspnet-list.aspx?C=2e3f4d45-f5d3-4501-b21e-c0f9d2a431f9
      

  6.   

    在e.Row.Cells(i).BackColor = System.Drawing.Color.Green设断点,看看执行到没有执行了,不会变色
      

  7.   

    为什么C#代码的这一句 e.Row.Cells[i].BackColor = System.Drawing.Color.DarkSeaGreen;
     可以变色 
    而VB.NET的 e.Row.Cells(i).BackColor = System.Drawing.Color.DarkSeaGreen 
    变不了色吗?难道VB.NET和C#不能实现同样的效果吗?
      

  8.   

    你这个是什么控件?GridView?DataGridView?我怎么没找到这个控件。
      

  9.   

    问个低能点的问题.....imports了吗?我记得颜色那个有包要imports吧
      

  10.   

    我把该imports的全部给加上了,和c#一样,可是还是没有变色
      

  11.   

    转换过的,你去试试Protected  Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If Not e.Row.RowIndex Is -1 And ViewState("myDS") Is Nothing Then
    Dim ds As DataSet = CType(ViewState("myDS"), DataSet)
    Dim i As Integer
    For  i = 0 To e.Row.Cells.Count- 1  Step i + 1
    If i >=7 And i<=9 Then
    Dim str1 As String = (CType(e.Row.Cells(i).Controls(1), Label)).Text 
    Dim str2 As String =  "" 
    Select Case i
    Case 7
    str2 = ds.Tables(0).Rows(e.Row.RowIndex)("bet365_hwin").ToString()
    Exit For
    Case 8
    str2 = ds.Tables(0).Rows(e.Row.RowIndex)("bet365_draw").ToString()
    Exit For
    Case 9
    str2 =ds.Tables(0).Rows(e.Row.RowIndex)("bet365_awin").ToString()
    Exit For
    End Select
    If str1 <> str2 Then
    Dim N1 As Double =  Double.Parse(str1) 
    Dim N2 As Double =  Double.Parse(str2) 
     
    If N1 <> N2 Then
    If N1 > N2 Then
    e.Row.Cells(i).BackColor = System.Drawing.Color.DarkSeaGreen
    Else 
    e.Row.Cells(i).BackColor = System.Drawing.Color.Pink
    End If
    End If
     
    End If
    End If
    Next
    End If
    End Sub