Link to home
Start Free TrialLog in
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

asked on

Dropdown column in a GridView

Hello Experts,
I have a GridView, where I want to add a DropDown column.  When the page is loaded, the drop down should be populated with its selected value.  Please let me know how to achieve it.
FYI, I am using ASP.NET and C#.  Please try to help.  Thank you in advance.

Thank you!
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Please see this link:
http://www.4guysfromrolla.com/webtech/050801-1.shtml

essentially you have: a template column with an item template and an edit item template.

 <asp:TemplateColumn headertext="Status">
    <ItemTemplate>
       <asp:Label id="lblLabelName" runat="server" 
                  Text='<%# Container.DataItem("tempField") %>'>
       </asp:Label>
    </ItemTemplate>

    <EditItemTemplate>
       <asp:DropDownList id=cmbStatuses runat="server" datavaluefield="StatusCode" 
                         datatextfield="Name" DataSource="<%#TempDataView%>">
       </asp:DropDownList>
    </EditItemTemplate>
  </asp:TemplateColumn>

Open in new window


in the code behind:
 protected DataView TempDataView = new DataView();


    protected void PopulateDropDownList()
    {
        SqlDataAdapter dataAdapter;  // sqldatasetcommand
        DataSet DS = new DataSet();
        SQLConnection NewConnection = new SQLConnection(MainDB.ConnectionString);

        dataAdapter = new SqlDataAdapter("sp_getinfo", NewConnection);
        dataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

        dataAdapter.Fill(DS, "TempInfo");  // filldataset

        TempDataView = DS.Tables("TempInfo");
    }

  // invoked by the grid.
    protected void DataEdit(object Sender, DataGridCommandEventArgs E)
    {
        PopulateDropDownList();  // load the list.
        dgExample.EditItemIndex = E.Item.ItemIndex;       //set the index so the dropdown appears.

    }

Open in new window

Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

ASKER

Kyle,
Thank you for your quick response.  In this, how would I get the selected item from the ddl when loaded?

Thank you!
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Kyle,
What you have given is, it will act when the drop down value is changed.  Which is one by one.  My requirement is Save the changed DropDown values at the end when Save button is clicked.

In other words, I am trying to read all the dropdown values in the GridView, in the loop, just like we read regular a text field (row.Cells[3].Text) in GridView.  Please let me know if we can do it.

Thank you!
Kyle Abrahams
Thank you for your help.  I really appreciate it.  It worked like charm.  I am sorry for late response.

Thank you again!