Click here to Skip to main content
15,071,878 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="ObjectDataSource1"
            OnRowDataBound="RowDataBound" >
            <Columns>
                <asp:CommandField HeaderText="EDIT" ShowEditButton="True" />
                <asp:BoundField DataField="SNO" HeaderText="SNO" SortExpression="SNO" />
                <asp:TemplateField HeaderText="BASIC ICONURL" SortExpression="BASIC_ICONURL">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server"
                            SelectedValue='<%# Bind("BASIC_ICONURL") %>' ></asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("BASIC_ICONURL") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="BASIC_FILEURL" HeaderText="BASIC FILEURL"
                    SortExpression="BASIC_FILEURL" />
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
                    ReadOnly="True" SortExpression="ID" Visible="False" />
            </Columns>
        </asp:GridView>





Imports System.Data
Imports MySql.Data
Imports MySql
Imports MySql.Data.MySqlClient
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim conn As MySqlConnection
    Dim cmd As MySqlCommand
    Dim connstr As String
    Dim strSQL As String
    Dim reader As MySqlDataReader

    Protected Sub RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView1.EditIndex = e.Row.RowIndex Then

            Dim GV As DropDownList = e.Row.FindControl("DropDownlist1")
            connstr = "server=localhost;User Id=xxxx;password=xxx;Persist Security Info=True;database=xxx"
            strSQL = "select * from pdfmap"
            conn = New MySqlConnection(connstr)
            cmd = New MySqlCommand(strSQL, conn)
            conn.Open()
            reader = cmd.ExecuteReader
            GV.DataSource = reader
            GV.DataTextField = "NAME"
            GV.DataValueField = "BASIC_ICONURL"
            GV.DataBind()
        End If

    End Sub
End Class



When i am trying to Bind in this way im getting error, when i remove "selectedvalue=<%#Bind("BASIC_ICONURL")%>" im not getting error, but data is not updating.
Posted

Hi,
The easiest way for your case is to use a SqlDataSource for your DropDownList in the EditItemTemplate tag. You need to remove RowDataBound event handler and let this codes fill and bind data to your DropDownList:

XML
<EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server"
                            DataSourceID="SqlDataSource2" DataTextField="NAME" DataValueField="BASIC_ICONURL"
                            SelectedValue='<%# Bind ("BASIC_ICONURL") %>'>
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource2" runat="server"
                            ConnectionString="<%$ ConnectionStrings:YourConnectionStringKey %>"
                            SelectCommand="SELECT [BASIC_ICONURL], [NAME] FROM [pdfmap]"></asp:SqlDataSource>
                    </EditItemTemplate>



I hope it helps,
Cheers
   
Comments
nikhlesh565 3-Apr-12 2:08am
   
Hey thanks for reply...yeah im aware of this method.. but i want to dynamically bind contents of the folder to dropdownlist edit template...that is reason why im trying out with simple example in rowdatabound event..if i succeed with this example later i will bind the contents of folder to dropdownlist
Hi,
Well, as I said you need to fill your DropDownList sooner. You must use RowCreated event handler instead of RowDataBound(as it fires sooner) so it is very important to know Page LifeCycle and event raise orders. Here is the new code:

VB
Protected Sub RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView1.EditIndex = e.Row.RowIndex Then

            Dim GV As DropDownList = e.Row.FindControl("DropDownlist1")
            connstr = "server=localhost;User Id=xxxx;password=xxx;Persist Security Info=True;database=xxx"
            strSQL = "select * from pdfmap"
            conn = New MySqlConnection(connstr)
            cmd = New MySqlCommand(strSQL, conn)
            conn.Open()
            reader = cmd.ExecuteReader
            GV.DataSource = reader
            GV.DataTextField = "NAME"
            GV.DataValueField = "BASIC_ICONURL"
            GV.DataBind()
        End If

    End Sub



I mean, just replace RowDataBound with RowCreated. It will probably work as it is the main problem.
I suggest you to examine different event handlers of the GridView. It can help you realize their fire orders.

Feel free to tell me if it does not work,
Cheers
   
Comments
nikhlesh565 3-Apr-12 13:23pm
   
http://www.mediafire.com/?ca52amn72tq15c5

Hello Reza.. can u plz download this sample code and fix it..
Thanks in advance
Reza Ahmadi 3-Apr-12 13:35pm
   
Sorry I can't as I don't have VS available right now and at work I'm very busy. I hope my solution can help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900