Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27212

Unable to cast object of type System.EventArgs to type GridRowEventArgs

$
0
0
I am getting an exception 'Unable to cast object of type System.EventArgs to type System.Web.UI.WebControls.GridViewRowEventArgs'
when I do my DataGridView.DataBind(). I bolded the row where this exception pops up. Tried to switch AutoGenerateColumnsTired for True and False, tried to go with or without OnRowDataBound="qvAvailability_RowDataBound" option, nothing changes. Cannot procees with my project for theree days. Tried for looking for the solution.

Below is my sub:
Public Sub BindGridView()

Try

' Create Datatable
Dim dtAvailability As New DataTable("dtAvailability")

' Creating columns for the table
Dim dc As New DataColumn("EmployeeAvailID")
dtAvailability.Columns.Add(dc)

dc = New DataColumn("AvailabiltyDate")
dtAvailability.Columns.Add(dc)

dc = New DataColumn("StartTime")
dtAvailability.Columns.Add(dc)

dc = New DataColumn("EndTime")
dtAvailability.Columns.Add(dc)

dc = New DataColumn("Notes")
dtAvailability.Columns.Add(dc)


Dim dSelectedStartDate As Date = CDate(lblSelectedStartDate.Text)
Dim dSelectedEndDate As Date = CDate(lblSelectedEndDate.Text)


Dim SELECT_Command As String = ""
Dim ObjReader As SqlDataReader

SELECT_Command = "SELECT * FROM TBL_EmployeeAvailability WHERE EmployeeID = " & employeeID & " AND AvailabilityDate BETWEEN '" & dSelectedStartDate & "' AND '" & dSelectedEndDate & "' "

T_OpenDBConnection()

ObjReader = fDBGetData(SELECT_Command)

Dim dAvailabiltyDate As Date
Dim dStartTime As Date
Dim dEndTime As Date
Dim strNotes As String = ""


If ObjReader IsNot Nothing Then
If ObjReader.HasRows Then
While ObjReader.Read

If Not DBNull.Value.Equals(ObjReader.GetValue(2)) Then dAvailabiltyDate = ObjReader.GetValue(2)
If Not DBNull.Value.Equals(ObjReader.GetValue(3)) Then dStartTime = ObjReader.GetValue(3)
If Not DBNull.Value.Equals(ObjReader.GetValue(4)) Then dEndTime = ObjReader.GetValue(4)
If Not DBNull.Value.Equals(ObjReader.GetValue(5)) Then strNotes = ObjReader.GetValue(5)

Dim dr As DataRow = dtAvailability.NewRow()

dr("AvailabiltyDate") = Format(dAvailabiltyDate, "MM/dd/yyyy")
dr("StartTime") = Format(dStartTime, "MM/dd/yyyy hh:mm tt")
dr("EndTime") = Format(dEndTime, "MM/dd/yyyy hh:mm tt")
dr("Notes") = strNotes

dtAvailability.Rows.Add(dr)

End While
End If
ObjReader.Close()
End If

T_CloseDBConnection()


dtAvailability.DefaultView.Sort = "AvailabiltyDate"
gvAvailability.DataSource = dtAvailability.DefaultView
gvAvailability.DataBind()

Catch ex As Exception
MsgBox("Error loading Employee's Available Time: " & ex.Message)
End Try

End Sub

And this is my asp datagrid:

<asp:GridView ID="gvAvailability" runat="server" AutoGenerateColumns="False"
PageSize="50" AllowSorting="True" ShowFooter="True"
BackColor="White" BorderColor="#CC9966" BorderStyle="None"
BorderWidth="1px" CellPadding="4" AutoGenerateEditButton="True"
AutoGenerateDeleteButton="True" EmptyDataText="No records found!"
EnableSortingAndPagingCallbacks="True" GridLines="None"
OnRowDataBound="qvAvailability_RowDataBound">

<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<RowStyle BackColor="#FFFFFF" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<Columns>

<asp:BoundField DataField="AvailabilityDate" HeaderText="Availability Date"
ReadOnly="True" />
<asp:BoundField DataField="StartTime" HeaderText="Start Time" />
<asp:BoundField DataField="EndTime" HeaderText="End Time" />
<asp:BoundField DataField="Notes" HeaderText="Note" />

</Columns>
</asp:GridView>

Could anyone tell me how to fix the problem?

Viewing all articles
Browse latest Browse all 27212

Trending Articles