Problem databinding gridview to datatable
Hi all,
basically I had a repeater that i needed to be able to now update row by row. I thought the easiest way would be to replace with a datagrid.
Here is the code:
Code:
<asp:GridView ID="grdNonEligible"
runat="server"
AutoGenerateColumns="False"
OnRowEditing="grdNonEligible_RowEditing"
OnRowCancelingEdit="grdNonEligible_RowCancelingEdit"
OnRowUpdating="grdNonEligible_RowUpdating" DataKeyNames="BidID" >
<Columns>
<asp:TemplateField HeaderStyle-CssClass="FormDesciptionBox" ItemStyle-CssClass="FormContentBox" HeaderText="Name">
<ItemTemplate>
<asp:Literal ID="litName" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Registered" ReadOnly="true" DataField="registrationdate" HeaderStyle-CssClass="FormDesciptionBox" ItemStyle-CssClass="FormContentBox" />
<asp:BoundField HeaderText="Username" ReadOnly="true" DataField="username" HeaderStyle-CssClass="FormDesciptionBox" ItemStyle-CssClass="FormContentBox" />
<asp:BoundField HeaderText="Band Date" ReadOnly="true" DataField="bandingdate" HeaderStyle-CssClass="FormDesciptionBox" ItemStyle-CssClass="FormContentBox" />
<asp:BoundField HeaderText="Other Offers" ReadOnly="true" DataField="offers" HeaderStyle-CssClass="FormDesciptionBox" ItemStyle-CssClass="FormContentBox" />
</Columns>
</asp:GridView>
In code behind I then pull the data, fill a repater with one half of the data and the other half I want into the datagrid (Split by a boolean value using datagrid select)
I want to control some of the data on row bound so have put this in
Code:
Protected Sub grdNonEligible_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdNonEligible.RowDataBound
Dim drv As DataRow = CType(e.Row.DataItem, DataRow)
If e.Row.RowType = DataControlRowType.DataRow AndAlso drv IsNot Nothing AndAlso grdNonEligible.EditIndex = -1 Then
CType(e.Row.FindControl("litname"), Literal).Text = drv("forename") & " " & drv("surname")
End If
End Sub
Now, when I databind like this:
Code:
ds = BServies.getAdvertBids(Request.QueryString("AdID"))
If Not ds Is Nothing Then
If ds.Tables(0).Rows.Count > 0 Then
Bidders.DataSource = ds.Tables(0).Select("eligible=1")
Bidders.DataBind()
grdNonEligible.DataSource = ds.Tables(0).Select("eligible=0")
grdNonEligible.DataBind()
Else
Bids.Visible = False
BidsNon.Visible = False
NoBidsMade.Visible = True
End If
Else
Bids.Visible = False
BidsNon.Visible = False
NoBidsMade.Visible = True
End If
I get this error:
DataBinding: 'System.Data.DataRow' does not contain a property with the name 'BidID'.
Even though this field is part of my data.
If I take away the select filtering, I then get this error on rowdatabound
Unable to cast object of type 'System.Data.DataRowView' to type 'System.Data.DataRow'.
Help please
Re: Problem databinding gridview to datatable
Hello,
Is it possible that you can show the code behind this:
Code:
ds = BServies.getAdvertBids(Request.QueryString("AdID"))
Gary