View Single Post
 
Old December 25th, 2010, 02:56 PM
walbalooshi walbalooshi is offline
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.