Error while using DataBind in RowDatabound vb.net GridView

asp net gridview rowdatabound findcontrol
onrowcreated gridview asp.net example
gridview on row data bound
gridview row created event asp net
rowdatabound dynamic gridview
hide gridview row on rowdatabound
gridview row count in rowdatabound
how to set value in gridview asp net

Please see below code. I am trying to populate dropdown list while edit in a GridView control.

 Private Function GetSiteSelection() As DataTableReader

    ''' some code to return DataTableReader

End Function

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridViewAttachedStation.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim dtrSiteSel As Data.DataTableReader = Nothing

        If e.Row.RowState = DataControlRowState.Edit Then

            Dim SiteName As DropDownList = DirectCast(e.Row.FindControl("DropDownListType"), DropDownList)

            SiteName.DataSource = GetSiteSelection()
            SiteName.DataTextField = "CODE_NAME"
            SiteName.DataValueField = "CODE_ID"
            SiteName.DataBind() <-- Error is here

        End If

    End If
End Sub

Getting error at SiteName.DataBind()

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Mockup as requestred: This works for simple text box, but not for dropdown. I referred other sources, but most of them are using the same way - which is not working.

<asp:GridView ID="GridView1"
            runat="server"
            AutoGenerateColumns="False"
            Width="100%"
            CellPadding="0"
            BorderStyle="None"
            AllowSorting="true"
            OnRowEditing="OnRowEditing">
            <AlternatingRowStyle CssClass="tblAtlData"></AlternatingRowStyle>
            <RowStyle ForeColor="Black" CssClass="tblData"></RowStyle>
            <FooterStyle CssClass="tblHeader"></FooterStyle>
            <PagerStyle Font-Bold="True" HorizontalAlign="Left" ForeColor="BlueViolet" CssClass="tblData"></PagerStyle>
            <HeaderStyle CssClass="tblHeader" ForeColor="White"></HeaderStyle>
            <Columns>

                <asp:TemplateField HeaderText="EDIT">
                    <ItemTemplate>
                        <asp:LinkButton ID="EditLinkButton" Text="Edit" Font-Bold="true" CommandName="Edit" runat="server"
                            CommandArgument='<%# Eval("STATION ID") %>' />
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" />
                    <HeaderStyle Width="5%" />
                    <EditItemTemplate>
                        <asp:LinkButton ID="UpdateLinkButton" Text="Update" Font-Bold="true" CommandName="Update" runat="server"
                            CommandArgument='<%# Eval("STATION ID") %>' />
                        <asp:LinkButton ID="CancelLinkButton" Text="Cancel" Font-Bold="true" runat="server"  OnClick="OnCancel" />

                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Site Selection Type">
                    <ItemTemplate>
                        <asp:Label ID="lblSiteSelection" runat="server" Text='<%# Eval("Site") %>' />
                    </ItemTemplate>
                    <ItemStyle Width="10%" CssClass="GeneralText" />
                    <HeaderStyle Width="10%" /> 
                    <EditItemTemplate>
                      <asp:DropDownList runat="server" ID="DropDownListTypeNameRow" CssClass="GeneralText"
                        onkeydown="typeAhead()" AutoPostBack="true" DataTextField='<%# Eval("SiteSelectionType") %>' />
                    </EditItemTemplate>
                </asp:TemplateField>

            </Columns>
        </asp:GridView>

The error indicates that you trying to use DataBinder.Eval method in a property which doesn't support data-binding.

This DropDownList setup should be work for solve the issue:

<asp:DropDownList runat="server" ID="DropDownListTypeNameRow" CssClass="GeneralText"
                  onkeydown="typeAhead()" AutoPostBack="true"
                  DataTextField="CODE_NAME" DataValueField="CODE_ID"
                  SelectedValue='<%# Eval("SiteSelectionType") %>' />

Since DataTextField property is already set to a column/field name in code-behind, you should also set it into the markup with the same name, i.e. CODE_NAME.

If you want to display default selected value in each dropdown for corresponding row, the Eval part should be placed in SelectedValue property instead.

Error while using DataBind in RowDatabound vb.net GridView, Please see below code. I am trying to populate dropdown list while edit in a GridView control. Private Function GetSiteSelection() As DataTableReader ''' some  Set the DataField property to the name of the column in the table for binding to the BoundField object and set the HeaderText value for displaying it on the GridView's Header. Add a RowDataBound Event to the GridView. Default.aspx Code <

ASP.Net GridView with DropDownList in ItemTemplate of TemplateField.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:BoundField HeaderText="Name" DataField="ContactName" />
        <asp:TemplateField HeaderText = "Country">
            <ItemTemplate>
                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' Visible = "false" />
                <asp:DropDownList ID="ddlCountries" runat="server">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        GridView1.DataSource = GetData("SELECT ContactName, Country FROM Customers")
        GridView1.DataBind()
    End If
End Sub

Private Function GetData(query As String) As DataSet
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim cmd As New SqlCommand(query)
    Using con As New SqlConnection(conString)
        Using sda As New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd
            Using ds As New DataSet()
                sda.Fill(ds)
                Return ds
            End Using
        End Using
    End Using
End Function

Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If (e.Row.RowType = DataControlRowType.DataRow) Then

       'Find the DropDownList in the Row.
        Dim ddlCountries As DropDownList = CType(e.Row.FindControl("ddlCountries"), DropDownList)
        ddlCountries.DataSource = GetData("SELECT DISTINCT Country FROM Customers")
        ddlCountries.DataTextField = "Country"
        ddlCountries.DataValueField = "Country"
        ddlCountries.DataBind()

        'Add Default Item in the DropDownList.
        ddlCountries.Items.Insert(0, New ListItem("Please select"))

        'Select the Country of Customer in DropDownList.
        Dim country As String = CType(e.Row.FindControl("lblCountry"), Label).Text
        ddlCountries.Items.FindByValue(country).Selected = True
    End If
End Sub

[Solved] error Row Databound Event in Gridview, DataBind(); ddlist. After passing rowdatabound event it is getting this error: Object lblcategoryid text should be present as a value in ddlist while you are you can check before using "FindByValue()" <asp:GridView ID="gv" runat="​server" AutoGenerateColumns="false" row databound in asp.net. Private Function GetSiteSelection() As DataTableReader ''' some code to return DataTableReader End Function Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridViewAttachedStation.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then Dim dtrSiteSel As Data.DataTableReader = Nothing If e.Row.RowState = DataControlRowState.Edit Then Dim SiteName As DropDownList = DirectCast(e.Row.FindControl("DropDownListType"), DropDownList

just delete the condition

If e.Row.RowState = DataControlRowState.Edit Then

Error handling in GridView's RowDataBound() event, Hello: I have a Try-Catch block in GridView's RowDataBound event, like With ObjectdataSource DataBind() is called automatically for you  In this article I will explain how to dynamically change (modify) GridView Cell value using RowDataBound event in ASP.Net using C# and VB.Net. Concept In database tables, we have some specific codes which have specific meaning.

Gridview fails to process "databind", Dear all, I have a gridview in web user control and when i try to bind the The datatable is retreived successfully without errors( I co. the data inside using the debugger ) but once gridview.databind() process takes the rowDataBound event has ever been fired as the exception was caught by Threading. So when we use these special key words as the CommandName for the buttons in the GridView, they automatically invoke the built in functionality of the GridView when we click on the button. As our code does not handle those events hence they throw an exception.

Using RowDataBound event in ASP.Net GridView with example, Here Mudassar Ahmed Khan has explained with example, how to use the RowDataBound (OnRowDataBound) event of GridView in ASP.Net  In my previous articles I explained clearly how to export gridview data to excel or word and how to export gridview data to CSV file. Now I will explain how to export gridview data to PDF using asp.net. In asp.net we don’t have direct feature to export gridview data to PDF for that reason here I am using third party library ITextSharp reference.

Find (Access) control inside GridView in RowDataBound and , Net GridView using C# and VB.Net. This article will explain how to find of controls inside RowDataBound and RowCommand events of ASP. DataBind();. } } VB.Net. Protected Sub Page_Load(sender As Object, e As hence if such check is not performed then you might get error while finding a control. Here Mudassar Ahmed Khan has explained how to find (access) control in GridView in RowDataBound and RowCommand events of ASP.Net GridView using C# and VB.Net. This article will explain how to find controls like TextBox, DropDownList, CheckBox, RadioButton, ListBox, Label, RadioButtonList, CheckBoxList, etc. and also get and set values of controls inside RowDataBound and RowCommand events of

Comments
  • Can you show markups of both DropDownList & GridView? Possibly you're using Bind or Eval in incorrect way, such like adding conditional blocks/statements which doesn't support data binding.
  • added. Thank you in advance. Please let me know if you have any other reference.
  • Try something like this: <asp:DropDownList runat="server" ID="DropDownListTypeNameRow" ... DataTextField='<%# CStr(DataBinder.Eval(Container.DataItem, "SiteSelectionType")) %>' />. Possibly DataTextField property doesn't support data binding with direct Eval().
  • No luck. shows blank dropdown
  • nope, that did not work too. Further, I believe that we have to perform binding in code-behind. Are you suggesting to make some code behind change? if so please show me.