Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Datagrid Drop Down list

Posted on 2003-10-23
13
Medium Priority
?
6,914 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 23

Accepted Solution

by:
naveenkohli earned 1500 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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
 
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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

610 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