DataGrid绑定给出异常

DataGrid绑定给出异常

本文介绍了DataGrid绑定给出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行下面的代码,并在gvAvailability.DataBind上获取异常无法使用System.EventArgs类型的对象来键入GridRowEventArgs".我正在寻找有关此信息,但没有发现太多.试图寻找解决方案三天.尝试过的解决方案#1和仍然相同的异常不断出现.

我的vb.net代码:

I am running the code below and getiing an exception "Unable to use object of type System.EventArgs to type GridRowEventArgs" on the gvAvailability.DataBind. I am looking for info on this and am not finding that much. Tried of looking for solution for three days. Tried solution #1 and still same exception keeps coming.

My vb.net code:

Public Sub BindGridView()
        Try
            Dim objConnection As SqlConnection
            Dim ObjReader As SqlDataReader
            Dim com As New SqlCommand

            Dim dAvailabilityDate As Date
            Dim dStartTime As Date
            Dim dEndTime As Date
            Dim dNotes As String = ""

            ' Creating columns for the table
            Dim dc As New DataColumn("AvailabilityDate")
            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)

            objConnection = New SqlConnection(ConnectionStringFansilDB)
            objConnection.Open()

            com.CommandText = "SELECT * FROM TBL_EmployeeAvailability WHERE EmployeeID=@employeeID AND AvailabilityDate BETWEEN @dSelectedStartDate AND @dSelectedEndDate"
            com.Parameters.Add("@employeeID", SqlDbType.Int).Value = employeeID
            com.Parameters.Add("@dSelectedStartDate", SqlDbType.DateTime).Value = dSelectedStartDate
            com.Parameters.Add("@dSelectedEndDate", SqlDbType.DateTime).Value = dSelectedEndDate
            com.Parameters.Add("@dNote", SqlDbType.VarChar).Value = dNotes


            com.Connection = objConnection

            ObjReader = com.ExecuteReader

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

                        If Not DBNull.Value.Equals(ObjReader.GetValue(2)) Then dAvailabilityDate = 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 dNotes = ObjReader.GetValue(5)

                        Dim dr As DataRow = dtAvailability.NewRow()

                        dr("AvailabilityDate") = Format(dAvailabilityDate, "dd/mm/yyyy")
                        dr("StartTime") = dStartTime.ToString("hh:mm tt")
                        dr("EndTime") = dEndTime.ToString("hh:mm tt")
                        dr("Notes") = dNotes

                        dtAvailability.Rows.Add(dr)

                    End While

                End If

                ObjReader.Close()
            End If

            If Not objConnection.State = ConnectionState.Closed Then
                objConnection.Close()
            End If

            gvAvailability.DataSource = dtAvailability.DefaultView
            gvAvailability.DataBind()

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

        End Try

    End Sub


还有我的DataGridView


And my DataGridView

<asp:GridView ID="gvAvailability" runat="server" AutoGenerateColumns="True"

                        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">

                        <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="AvailabiltyDate" HeaderText="Availability Date"

                             ReadOnly="True" />
                         <asp:BoundField DataField="Start Time" HeaderText="Start Time" />
                         <asp:BoundField DataField="End Time" HeaderText="End Time" />
                         <asp:BoundField DataField="Note" HeaderText="Note" />

            </Columns>
                    </asp:GridView>

推荐答案


gvAvailability.DataSource = dtAvailability.DefaultView




Do

gvAvailability.DataSource = dtAvailability



另一个问题是DataTable列名称应与GridView BoundField的DataField属性匹配.

所以,而不是



Another problem is the DataTable Column names should match with the GridView BoundField''s DataField Property.

So, instead of

<asp:BoundField DataField="AvailabiltyDate" HeaderText="Availability Date"

                            ReadOnly="True" />
<asp:BoundField DataField="Start Time" HeaderText="Start Time" />
<asp:BoundField DataField="End Time" HeaderText="End Time" />
<asp:BoundField DataField="Note" HeaderText="Note" />




Do

<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" />



这篇关于DataGrid绑定给出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 21:09