Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old December 24th, 2010, 06:53 AM
Authorized User
Points: 322, Level: 6
Points: 322, Level: 6 Points: 322, Level: 6 Points: 322, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2010
Posts: 63
Thanks: 3
Thanked 0 Times in 0 Posts
Default Dropdownlist selected value

Problem solved. Thanks

Last edited by avidan; December 24th, 2010 at 07:03 AM..
  #2 (permalink)  
Old December 25th, 2010, 02:56 PM
Authorized User
 
Join Date: Oct 2009
Posts: 22
Thanks: 2
Thanked 10 Times in 7 Posts
Default

Without seeing your code it will be hard to give you an exact answer, but I can at least give you some tips to steer you in the correct direction.

Assuming that you actually have designed the GridView in the designer, so you have a number of entries under the column section for the GridView, this would be pretty simple.

1. You would need to change the <asp:BoundField> to an <asp:TemplateFiled>

This will allow you to specify how you want that column to look like. For example it could look like this:

Code:
<asp:TemplateField HeaderText="Data">
    <ItemTemplate><%# Eval("Country") %></ItemTemplate>
    <EditItemTemplate>
         Setup your dropdownlist here.
    </EditItemTemplate>
</asp:TemplateField>
2. Once you have your grid setup you will need to hook into two of the GridView events: RowDataBound and RowUpdating to be able to fill the dropdown with the correct value when you change to edit more and to send the updated value of the table. The following is just a template:

Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // Check for a row in Edit mode
        if ((e.Row.RowState == DataControlRowState.Edit) ||
            (e.Row.RowState == (DataControlRowState.Alternate | 
                                DataControlRowState.Edit)))
        {
            System.Data.DataRowView drv = (System.Data.DataRowView)e.Row.DataItem;

            DropDownList ddl = (DropDownList)e.Row.Cells[10].FindControl("DropDownList1");
            ListItem li = ddl.Items.FindByValue(drv["Data"].ToString());
            li.Selected = true;
        }
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow gvr = GridView1.Rows[GridView1.EditIndex];
        DropDownList ddl = (DropDownList)gvr.Cells[10].FindControl("DropDownList1");
        e.NewValues["Data"] = ddl.SelectedValue;
    }

The above code is from the source code to the Pro ASP.net 4.0 in Vb.Net and C#. It is under chapter 7. You can probably download the source for yourself and take a look at the code.


Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDownList, DataSource and Selected Items kingroon ASP.NET 2.0 Basics 2 February 25th, 2008 10:41 AM
read the selected value of DropDownList from VBScr dbayona ASP.NET 2.0 Professional 0 November 11th, 2007 06:16 PM
Set selected value in GridView dropdownlist SV ASP.NET 2.0 Basics 1 October 24th, 2007 01:18 PM
DropDownList Selected Index = -1?? dorothee ASP.NET 1.0 and 1.1 Basics 4 June 11th, 2004 03:08 AM
selected value from dropdownlist control netwizard_01 ASP.NET 1.0 and 1.1 Basics 3 January 20th, 2004 09:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.