user2026041
user2026041

Reputation: 131

How to populate a DropDownList in a DataGrid?

I have a DataGrid that displays information about trailers. I decided to change the location column to a DropDownList so the location can be easily changed. But I am not sure how to populate the DropDownList.

 <asp:DataGrid runat="server" CssClass="tblResults" OnItemDataBound="dgList_ItemCreated" OnRowDataBound="OnRowDataBound" AllowSorting="true" OnSortCommand="dgTrailer_Sort" ID="dgTrailers" DataKeyField="ID" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundColumn DataField="TrailerOwner" HeaderText="Owner" SortExpression="TrailerOwner"></asp:BoundColumn>
                <asp:BoundColumn DataField="TrailerMake" HeaderText="Trailer Make" SortExpression="TrailerMake"></asp:BoundColumn>
                <asp:TemplateColumn HeaderText="Trailer Location">
                    <itemtemplate>
                        <asp:DropDownList ID="ddlLocation" runat="server">
                        </asp:DropDownList>
                 </itemtemplate>
                </asp:TemplateColumn>
                </Columns>
        </asp:DataGrid>

I have a DropDownList for location already (called ddlTrailerLocation) so the user can select the location for the trailer and then the DataGrid displays all that information.

protected void PopulateDDLs()
{
    DataSet dsTrailerLocation = DataUtils.GetAllGenSmall(Company.Current.CompanyID, "Description", "", 1, false, "Description", false, "TrailerLocationNOCODE", 0);
    if (dsTrailerLocation.Tables[0].Rows.Count > 0)
    {
        ddlTrailerLocation.DataSource = dsTrailerLocation;
        ddlTrailerLocation.DataValueField = "Description";
        ddlTrailerLocation.DataTextField = "Description";
        ddlTrailerLocation.DataBind();
        ddlTrailerLocation.Items.Insert(0, new ListItem("Select One", "0"));
    }
    else
    {
        ddlTrailerLocation.Items.Insert(0, new ListItem("No Locations Entered", "0"));
    }
}

protected void dgList_ItemCreated(object sender, DataGridItemEventArgs e)
    {
    DropDownList ddlTrailerLocation = e.Item.FindControl("ddlLocation") as DropDownList;
         DataSet dsTrailerLocation = DataUtils.GetAllGenSmall(Company.Current.CompanyID, "Description", "", 1, false, "Description", false, "TrailerLocationNOCODE", 0);
         if (ddlTrailerLocation != null)
         {
             ddlTrailerLocation.DataSource = dsTrailerLocation;
             ddlTrailerLocation.DataValueField = "Description";
             ddlTrailerLocation.DataTextField = "Description";
             ddlTrailerLocation.DataBind();
         }
}

EDIT Added the code in protected void dgList_ItemCreated. The dropdownlist now displays a location but it is not the correct location {

Upvotes: 0

Views: 5804

Answers (2)

WeeklyDad
WeeklyDad

Reputation: 317

I´m not sure if you´ve actually googled. I used DataSet couple weeks ago, and I used MSDN for this issue.

sqlDataAdapter1.Fill(dataset1.Tables["Customers"]);

You should typically provide the name of the DataTable to load the data into. If you pass in the name of a DataSet instead of a specific data table, a DataTable named Table1 is added to the dataset and loaded with the results from the database (as opposed to loading the data in an existing DataTable in the dataset). For more information, see Populating a DataSet from a DataAdapter.

Upvotes: 1

Deepak Joshi
Deepak Joshi

Reputation: 1066

First of all add one hidden field control in data-grid for save the location Id of the row. After that when bind the drop-down you need to get the value from hidden field and set in drop-down as below.

 <asp:DataGrid runat="server" CssClass="tblResults" OnItemDataBound="dgList_ItemCreated" OnRowDataBound="OnRowDataBound" AllowSorting="true" OnSortCommand="dgTrailer_Sort" ID="dgTrailers" DataKeyField="ID" AutoGenerateColumns="false">
                <HeaderStyle CssClass="tblResultsHeader" />
                <AlternatingItemStyle BackColor="#EEEEEE" />
                <Columns>
                    <asp:HyperLinkColumn ItemStyle-CssClass="loading" DataNavigateUrlFormatString="Trailer.aspx?TrailerID={0}" DataNavigateUrlField="ID" DataTextField="Reg" HeaderText="Registration" SortExpression="Reg"></asp:HyperLinkColumn>
                    <asp:BoundColumn DataField="TrailerOwner" HeaderText="Owner" SortExpression="TrailerOwner"></asp:BoundColumn>
                    <asp:BoundColumn DataField="TrailerMake" HeaderText="Trailer Make" SortExpression="TrailerMake"></asp:BoundColumn>
                    <asp:TemplateColumn HeaderText="Trailer Location">
                        <itemtemplate>
                            <asp:DropDownList ID="ddlTrailerLoc" runat="server">
                            </asp:DropDownList>
                            <asp:HiddenField ID="hdlTrailerLoc" runat="server" Value='<%#Eval("LocationId")%>' />
                     </itemtemplate>
                    </asp:TemplateColumn>
                    <asp:BoundColumn DataField="Year" HeaderText="Year" SortExpression="Year"></asp:BoundColumn>
                    <asp:BoundColumn DataField="TrailerNumber" HeaderText="Trailer Number" SortExpression="TrailerNumber"></asp:BoundColumn>
                    <asp:BoundColumn DataField="DateOfLastService" HeaderText="Last Service" SortExpression="DateOfLastService"></asp:BoundColumn>
                    <asp:BoundColumn DataField="DateOfNextService" HeaderText="Next Service" SortExpression="DateOfNextService"></asp:BoundColumn>
                    <asp:BoundColumn DataField="IsActive" HeaderText="Is Active" SortExpression="IsActive"></asp:BoundColumn>
                    </Columns>
            </asp:DataGrid>

Please bind drop-down and set the value on data-grid "ItemDataBound" event as below

 protected void dgList_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Pager && e.Item.ItemType != ListItemType.Footer)
        {
            int count = 1;
            foreach (TableCell c in e.Item.Cells)
            {
                bool b = Convert.ToBoolean(((DataRowView)e.Item.DataItem).Row["IsActive"]);

                if (count == e.Item.Cells.Count)
                {
                    c.Text = "<input DISABLED type=\"checkbox\" " + ((b) ? "checked" : "") + "/>";
                }
                DateTime dt = new DateTime();
                if (DateTime.TryParse(c.Text, out dt))
                {
                    c.Text = dt.ToShortDateString();
                }
                count++;
            }

             DropDownList ddlTrailerLocation = e.Item.FindControl("ddlTrailerLoc") as DropDownList;
             //DataSet dsTrailerLocation = DataUtils.GetAllGenSmall(Company.Current.CompanyID, "Description", "", 1, false, "Description", false, "TrailerLocationNOCODE", 0);
             if (ddlTrailerLocation != null)
             {
                 PopulateDDLs(ddlTrailerLocation);
                 //set the value in dropdown
                 HiddenField hdlTrailerLoc = e.Item.FindControl("hdlTrailerLoc") as HiddenField;
                 if (hdlTrailerLoc != null)
                 {
                     ddlTrailerLocation.SelectedValue = hdlTrailerLoc.Value;
                 }
             }
        }
    }

Below code for bind location drop-down

protected void PopulateDDLs(DropDownList ddlTrailerLoc)
{
    DataSet dsTrailerLocation = DataUtils.GetAllGenSmall(Company.Current.CompanyID, "Description", "", 1, false, "Description", false, "TrailerLocationNOCODE", 0);
    if (dsTrailerLocation.Tables[0].Rows.Count > 0)
    {
        ddlTrailerLoc.DataSource = dsTrailerLocation;
        ddlTrailerLoc.DataValueField = "Description";
        ddlTrailerLoc.DataTextField = "Description";
        ddlTrailerLoc.DataBind();
        ddlTrailerLoc.Items.Insert(0, new ListItem("Select One", "0"));
    }
    else
    {
        ddlTrailerLoc.Items.Insert(0, new ListItem("No Locations Entered", "0"));
    }
}

Upvotes: 1

Related Questions