Dropdown column in a datagrid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kimbred
    New Member
    • Jan 2009
    • 47

    Dropdown column in a datagrid

    I have a datagrid that has a dropdown column. All works fine until I go to edit mode and it seems to forget the original value of the column in the selected row. How can I retain that value in edit mode?????
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Are you preforming a DataBind in the Page Load event....or in any function that is executed before the Event code occurs in the asp life cycle?

    If you are, then your data is being re-bound to the GridView and anything edited will be lost.

    Comment

    • kimbred
      New Member
      • Jan 2009
      • 47

      #3
      Help on dropdown

      I think I'm getting close. I've found on the internet a procedure that uses the EVAL in the source. The problem is I get an error msg. Please see the attached code.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Please post your code instead of using attachments.
        Attachments are not working at this time.

        Also, please post the error message that you are getting....stat ing that an error occurs is not going to help us in understanding the problem that you're having.

        Comment

        • kimbred
          New Member
          • Jan 2009
          • 47

          #5
          Dropdown column in a datagrid

          Code:
          <asp:GridView ID="GridView1" runat="server" Height="205px" Style="z-index: 102; left: 19px;
                  position: absolute; top: 28px" Width="325px" BorderStyle="None" TabIndex="11" AutoGenerateColumns="False" DataSourceID="PhoneList" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" BackColor="White" BorderColor="#999999" BorderWidth="1px" CellPadding="3" GridLines="Vertical" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="ddlSuper_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound" OnRowDeleted="GridView1_RowDeleted">
                  <Columns>
                      <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ButtonType="Image" DeleteImageUrl="~/App_Themes/Default/Images/Delete16x16.gif" EditImageUrl="~/App_Themes/Default/Images/SelectEdit.gif" InsertText="" NewText="" CancelImageUrl="~/App_Themes/Default/Images/Reject16x16.gif" UpdateImageUrl="~/App_Themes/Default/Images/Approve16x16.gif" />
                      <asp:TemplateField HeaderText="Seq" ShowHeader="False" Visible="False">
                          <EditItemTemplate>
                              <asp:TextBox ID="txtSeq" runat="server" Text='<%# Bind("WPRCK7") %>' Visible="False"></asp:TextBox>
                          </EditItemTemplate>
                          <ItemTemplate>
                              <asp:Label ID="lblSeq" runat="server" Text='<%# Bind("WPRCK7") %>' Visible="False"></asp:Label>
                          </ItemTemplate>
                      </asp:TemplateField>
                      <asp:TemplateField HeaderText="Type">
                          <EditItemTemplate>
                              <asp:DropDownList ID="ddlType" runat="server"  SelectedValue='<%# Eval("WPPHTP")%>' DataSourceID="PhoneTypes" DataValueField="DRKY" DataTextField="DRDL01"> </asp:DropDownList> 
                              <asp:ObjectDataSource ID="PhoneTypes" runat="server" SelectMethod="GetPhoneTypes" TypeName="Intranet.DAL.SelfServiceSql"></asp:ObjectDataSource>
                          </EditItemTemplate>
                          <ItemTemplate>
                             <asp:Label ID="lblPType" runat="server" Text='<%# Bind("WPPHTP") %>'></asp:Label>
                          </ItemTemplate>
                      </asp:TemplateField>
                      <asp:TemplateField HeaderText="Area Cd">
                          <EditItemTemplate>
                              <asp:TextBox ID="txtACode" runat="server" Text='<%# Bind("WPAR1") %>'></asp:TextBox>
                          </EditItemTemplate>
                          <ItemTemplate>
                              <asp:Label ID="lblACode" runat="server" Text='<%# Bind("WPAR1") %>'></asp:Label>
                          </ItemTemplate>
                      </asp:TemplateField>
                      <asp:TemplateField HeaderText="Nbr">
                          <EditItemTemplate>
                              <asp:TextBox ID="txtPNbr" runat="server" Text='<%# Bind("WPPH1") %>'></asp:TextBox>
                          </EditItemTemplate>
                          <ItemTemplate>
                              <asp:Label ID="lblPNbr" runat="server" Text='<%# Bind("WPPH1") %>'></asp:Label>
                          </ItemTemplate>
                      </asp:TemplateField>
                  </Columns>
                  <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                  <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                  <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                  <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                  <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                  <AlternatingRowStyle BackColor="Gainsboro" />
              </asp:GridView>

          I get the following error:
          DataBinding: 'System.Data.Da taRowView' does not contain a property with the name 'WPPHTP'.
          Last edited by Frinavale; Feb 24 '09, 07:21 PM. Reason: Added [code] tags: Please post code between [code][/code] tags

          Comment

          • bhupinder
            New Member
            • Feb 2009
            • 32

            #6
            Dropdownlist in datagrid

            Hi
            You can use this code.
            Code:
            <asp:DataGrid runat=server id=gvfilesdata>
                                    <Columns>
                                    <asp:TemplateColumn>
             <ItemTemplate>
                                                 <asp:DropDownList ID="ddPage" runat=server Width="150px">
            <asp:listitem text='1' value='1'></asp:listitem>
            <asp:listitem text='2' value='2'></asp:listitem>
            </asp:DropDownList>
            
            </itemtemplate>
            </asp:TemplateColumn>
                                       
                                        
                                        
                                    </Columns>
            
            </asp:datagrid>
            //You can perform this code rowediting of datagrid..



            Code of vb
            Code:
             Protected Sub gvFilesData_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvFilesData.RowEditing
                          Dim id As Integer
                  
            
                    pageid = CType(gvFilesData.Rows(e.NewEditIndex).Cells(4).FindControl("ddPage"), DropDownList).SelectedValue
                          CType((gvFilesData.Rows(e.NewEditIndex).Cells(4).FindControl("ddPage")), DropDownList).SelectedValue = pageid
                    bindDataOfFileUpload()
                End Sub




            Please implement this code.
            Last edited by Frinavale; Feb 25 '09, 03:52 PM. Reason: Added [code] tags: Please post code between [code][/code] tags

            Comment

            • kimbred
              New Member
              • Jan 2009
              • 47

              #7
              The dropdown list must remain dynamic

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Take the Eval statement out.
                The GridView uses it's data source's properties and methods to bind to.
                It's trying to bind the data to DataRowView, which obviously does not have a property named "WPPHTP".

                Instead, implement a method that handles the GridView's RowDataBound event and set the selected item in the DropDownList here.

                Comment

                • kimbred
                  New Member
                  • Jan 2009
                  • 47

                  #9
                  Do you have an example you could provide? I've tried other methods of setting the value of this column in edit mode, but can't get it right. Any help would be appreciated.

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    Not really....each implementation of the RowDataBound event is going to be different depending on your datasource and what your GridView is meant to display.

                    Check out the GridView RowDataBound event for more information.

                    Comment

                    • kimbred
                      New Member
                      • Jan 2009
                      • 47

                      #11
                      The article really didn't give me much in regard to setting the initial value of the column. When I use the code below, I get the Title of the row but nothing more. How do you get the value of the selected column?

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        Ok, in typing a response I realized where I made the mistake in attempting to help you....sorry.


                        You shouldn't be looking at the RowDataBound Event.
                        I think you should be looking at the Editing Event.

                        It is here that you want to set the selected value of the DropDownList.

                        Code:
                        Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles GridView1.RowEditing
                            GridView1.EditIndex = e.NewEditIndex
                            Dim thisRowsDdlType As DropDownList = Ctype(GridView1.Rows(e.NewEditIndex).FindControl(ddlType), DropDownList)
                            thisRowsDdlType.Items(thisRowsDdlType.Items(thisRowsDdlType.SelectedIndex)).Selected = False
                        
                            thisRowsDdlType.Items(3).Selected = True
                        
                        End Sub
                        If this doesn't work, then you need to do something similar, only in the RowDataBound event instead.

                        Comment

                        • kimbred
                          New Member
                          • Jan 2009
                          • 47

                          #13
                          Is there somthing similar in C#?

                          Comment

                          • Frinavale
                            Recognized Expert Expert
                            • Oct 2006
                            • 9749

                            #14
                            I'm not great with C#...it would be just as hard for me to change the syntax into C# as it would be for you.

                            But if you really want an example in C# please check out the links I showed you.


                            Here goes my attempt (not guaranteed to work...but you'll get the idea):
                            Code:
                            void GridView1_RowEditing(Object sender, GridViewEditEventArgs e)
                            {
                                GridView1.EditIndex = e.NewEditIndex;
                                DropDownList thisRowsDdlType  = (DropDownList) GridView1.Rows[e.NewEditIndex].FindControl(ddlType);
                                thisRowsDdlType.Items[thisRowsDdlType.Items[thisRowsDdlType.SelectedIndex]].Selected = false;
                             
                                thisRowsDdlType.Items[3].Selected = true;
                             
                            }

                            Comment

                            • kimbred
                              New Member
                              • Jan 2009
                              • 47

                              #15
                              What is the following line in your code attempting to do?

                              DropDownList thisRowsDdlType = (DropDownList) GridView1.Rows[e.NewEditIndex].FindControl("d dlType");

                              Comment

                              Working...