Solved

Datagrid Drop Down list

Posted on 2003-10-23
13
6,909 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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