Solved

Datagrid Drop Down list

Posted on 2003-10-23
13
6,905 Views
Last Modified: 2007-12-19


I need to setup a drop down list in a datagrid. I have read the 4guysfromrolla.com article on it and it's not working for me. Take a look at the code and let me know what I am doing wrong.

<asp:datagrid id="DataGrid1" runat="server" DataKeyField="powerunit" OnItemCommand="DataGrid_ItemCommand" OnEditCommand="DataGrid_Edit" OnUpdateCommand="DataGrid_Update" OnCancelCommand="DataGrid_Cancel" OnDeleteCommand="DataGrid_Delete" AllowPaging="true" PageSize="25" OnPageIndexChanged="DataGrid_Page" ForeColor="Black" BackColor="White" CellPadding="3" GridLines="None" CellSpacing="1" width="80%" autogeneratecolumns="False">
            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
            <PagerStyle horizontalalign="Right" backcolor="#C6C3C6" mode="NumericPages" font-size="smaller"></PagerStyle>
            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
            <FooterStyle backcolor="#C6C3C6"></FooterStyle>
            <Columns>
                <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit" ItemStyle-Font-Size="smaller" ItemStyle-Width="10%"></asp:EditCommandColumn>
                <asp:ButtonColumn Text="Delete" CommandName="Delete" ItemStyle-Font-Size="smaller" ItemStyle-Width="10%"></asp:ButtonColumn>
                <EditItemTemplate>
                    <asp:DropDownList id="cmbStatuses" runat="server" datavaluefield="locname" datatextfield="Name" DataSource="<%#DropDownDataView%>"></asp:DropDownList>
                </EditItemTemplate>
            </Columns>
        </asp:datagrid>
        <br />
        <asp:LinkButton id="LinkButton1" onclick="AddNew_Click" runat="server" Text="Add new item" Font-Size="smaller"></asp:LinkButton>
        <br />
        <br />
        <asp:Label id="Message" runat="server" width="80%" forecolor="red" enableviewstate="false"></asp:Label>
    </form>



I get a parser error when trying to run this that says " System.Web.UI.WebControls.DataGridColumnCollection must have items of type 'System.Web.UI.WebControls.DataGridColumn'. 'EditItemTemplate' is of type 'System.Web.UI.HtmlControls.HtmlGenericControl'.


Any thoughts?

Thanks,

Justin
0
Comment
Question by:jlazanowski
  • 6
  • 5
  • 2
13 Comments
 
LVL 23

Accepted Solution

by:
naveenkohli earned 500 total points
ID: 9608589
You did not specify the outer TemplateColumn tag.

<asp:TemplateColumn>
<EditItemTemplate>
                    <asp:DropDownList id="cmbStatuses" runat="server" datavaluefield="locname" datatextfield="Name" DataSource="<%#DropDownDataView%>"></asp:DropDownList>
                </EditItemTemplate>
</asp:TemplateColumn>
0
 
LVL 1

Author Comment

by:jlazanowski
ID: 9608730
Ok I am going to need a lot more help with this. I thought I understood that article, obviously I don't.


I have the Templatecolumn tag problem fixed. How do I populate and bind to the drop down list?

Justin
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 9608815
In your codebehind you need to populate DropDownDataView variable. It seems that its a Dataview type variable. Do this in PageLoad
0
 
LVL 1

Author Comment

by:jlazanowski
ID: 9608859
Since I am only looking for the drop down in the edit I thought I should do this in the edit.


Here is my edit sub..

Sub DataGrid_Edit(Sender As Object, E As DataGridCommandEventArgs)

    ' turn on editing for the selected row

    If Not isEditing Then

        DataGrid1.EditItemIndex = e.Item.ItemIndex
        Dim myConnection As SqlConnection = New SqlConnection("server=xxx;UID=webuser;PWD=webpass;database=xxx;")
        Dim mySQL1 as string = "select locname from locations"
        Dim myCmd1 as New SQLCommand(mySQL1,myConnection)

        Myconnection.open()

        ddlocname.datasource = mycmd1.executereader()
        ddlocname.Databind()
        myconnection.close()

        BindGrid()

    End If

End Sub


When I run this I get ddlocname undeclared. It is the name of the ddlist in the edit tag.

I think I am just not understanding how to bind to this... just looking for some help

Thanks,

Justin
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 9608890
Edit event will be fired once you have populated the control. You always populate the controls in Page_Load control. This will be when page lods the firsr time. You can check this condition by checking IsPostBack property value of the page. Once your page has been populated the firs ttime, then user will perform some steps and depending on some action your edit event will be fired.
0
 
LVL 1

Author Comment

by:jlazanowski
ID: 9608920
Ok I put

DataGrid1.EditItemIndex = e.Item.ItemIndex
        Dim myConnection As SqlConnection = New SqlConnection("server=xxx;UID=webuser;PWD=webpass;database=xxx;")
        Dim mySQL1 as string = "select locname from locations"
        Dim myCmd1 as New SQLCommand(mySQL1,myConnection)

        Myconnection.open()

        ddlocname.datasource = mycmd1.executereader()
        ddlocname.Databind()
        myconnection.close()

into the page load..

but I still get ddlocname undeclared as my parser error....

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 23

Expert Comment

by:naveenkohli
ID: 9608937
ddlocname is name of the variable for dropdownlist (guessing).... so make sure that your variable names match with the control ID that you have on the page.
0
 
LVL 1

Author Comment

by:jlazanowski
ID: 9608953
Yes it is the name for the dropdown list.


Here is the edit column where it is defined.

 <EditItemTemplate>
                <asp:DropDownList id="ddlocid" runat="server" datavaluefield="locname" datatextfield="Name" DataSource="<%#DropDownDataView%>"></asp:DropDownList>
                </edititemtemplate>


It matches.

0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 9608987
As per your posted code... the name should be "ddlocid" and not "ddlocname"
0
 
LVL 1

Author Comment

by:jlazanowski
ID: 9609046
Sorry this was after I cut it out and paste it back into the code.. they match now

<EditItemTemplate>
                        <asp:DropDownList id="ddlocname" runat="server" datavaluefield="locname" datatextfield="Name" DataSource="<%#DropDownDataView%>"></asp:DropDownList>
                    </edititemtemplate>


im myConnection As SqlConnection = New SqlConnection("server=xxx;UID=webuser;PWD=webpass;database=xxx;")
        Dim mySQL1 as string = "select locname from locations"
        Dim myCmd1 as New SQLCommand(mySQL1,myConnection)

        Myconnection.open()

        ddlocname.datasource = mycmd1.executereader()
        ddlocname.Databind()
        myconnection.close()



Still get the same parser error ddlocname not declared.

0
 
LVL 1

Author Comment

by:jlazanowski
ID: 9609339
For anyone else trying this give this a shot


Sub PopulateDropDownList()
      Dim dataAdapter As SqlDataAdapter  ' sqldatasetcommand
      Dim DS As New DataSet()
      Dim NewConnection As SQLConnection = New SqlConnection("server=YOGURT;UID=webuser;PWD=webpass;database=fueltracking;")

      dataAdapter = New SqlDataAdapter("select location from powerunit", NewConnection)


      dataAdapter.Fill(DS, "TempInfo")  'filldataset

     dropdownDataView = DS.Tables("TempInfo")
  End Sub


In your edit sub put this

PopulateDropDownList()

You will also need this in your update sub

Dim TempList As DropDownList
TempList = E.Item.FindControl("ddlocname")
location = TempList.SelectedItem.Value


This will do it. Your form code should look like this

<asp:TemplateColumn>
                <ItemTemplate>
                <asp:Label id="lblLabelName" runat="server" Text="Location">
                 </asp:Label>
                </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList id="ddlocname" runat="server" datavaluefield="location" datatextfield="Location" DataSource="<%#DropDownDataView%>"></asp:DropDownList>
                    </edititemtemplate>
                </asp:TemplateColumn>


Hope this helps

Justin
0
 
LVL 4

Expert Comment

by:meet_zorrer
ID: 9666613
man you have already accepted an anwer before what can i say about it now
0
 
LVL 4

Expert Comment

by:meet_zorrer
ID: 9666614
Hi man you have already accepted an anwer before what can i say about it now....

Do you still want the solution or not?

Regards
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now