我试图简单地根据GridView单元格中的值更改GridView中行的背景颜色。我有两个功能。第一个命中我的数据库并从数据库数据创建DataTable
。然后DataTable
将用作GridView
的数据源。这是代码:
Protected Sub FillMipDataGridOnReqNoSearch()
Dim ReqNumber As String = Me.txtReqNo.Text
Try
Dim DbConnection As New DevConnection()
Dim Da As New SqlDataAdapter()
Using DbConnection.Conn
DbConnection.Conn.Open()
Using SqlCmd As New SqlCommand()
With SqlCmd
.Connection = DbConnection.Conn
.CommandType = CommandType.StoredProcedure
.CommandText = "MyStoredProcedure"
.Parameters.AddWithValue("@RequestNumber", SqlDbType.NVarChar).Value = ReqNumber End With
Using Da
Da.SelectCommand = SqlCmd
Using Dt As New DataTable()
Da.Fill(Dt) 'populates the dataset
Gv_MipData.DataSource = Dt
Gv_MipData.DataBind()
'the following three lines pass the cell indeces for each dateTime cell that needs to be formatted
ShortenDateTimeStrings(4) 'date from cell
ShortenDateTimeStrings(5) 'date to cell
ShortenDateTimeStrings(11) 'date submitted cell
End Using
End Using
End Using
End Using
Catch ex As Exception
End Try
End Sub
第二个函数是GridView
的{{1}}事件处理程序。这是代码:
RowDataBound
以上代码在 Protected Sub Gv_MipData_DataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gv_MipData.DataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ApprovalStatus As String = e.Row.Cells(9).Text
If ApprovalStatus = "D" Then
e.Row.BackColor = Drawing.Color.Red
End If
End If
End Sub
行的FillMipDataGridOnReqNoSearch()
函数中断。它捕获了这个例外:
无法将类型为“System.EventArgs”的对象强制转换为类型 'System.Web.UI.WebControls.GridViewRowEventArgs'。
我尝试将Gv_MipData.DataBind()
事件处理程序中的参数e
更改为RowDataBound
类型,但它引发了此错误:
'Row'不是'System.EventArgs'的成员。
我知道我已经偏离了某个地方。有什么建议吗?
更新:GridView的标记
EventArgs