Solved

Datagrid Drop Down list

Posted on 2003-10-23
13
6,906 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help parsing JSON in my VB.Net application 4 43
C# application error "Parameter Is Not Valid". 3 79
Name space syntax error 12 40
Error on Add method 1 37
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

912 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

21 Experts available now in Live!

Get 1:1 Help Now