Solved

Datagrid Drop Down list

Posted on 2003-10-23
13
6,911 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 41
Code enhancement 4 33
Data organization issue 7 37
Need to sort columns in DataGridView 4 37
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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