DropDownList within a DataGrid
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcontrolscs' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Ray Houston
(If there is a better list to post this problem, please let me know.) I'm
building a control that contains a DataGrid control which has a Template
column which contains a DropDownList. The DropDownList renders fine with
it's DataSource, but I would like to add a blank row to the items and select
a default item.

He is a stripped down code of my control:

protected override void OnInit(EventArgs E)
{
    myTemplateColumn = new TemplateColumn();
    myTemplateColumn.HeaderText = "Role";
    myTemplateColumn.ItemTemplate = new RoleColumn();
    SourceDataGrid.Columns.Add(myTemplateColumn);
    base.OnInit(E);
}
protected override void CreateChildControls()
{
    SourceDataGrid.SortCommand += new
DataGridSortCommandEventHandler(this.Sort);
    SourceDataGrid.ItemDataBound += new
DataGridItemEventHandler(this.ItemDataBound);

    this.Controls.Add(SourceDataGrid);
}

Here is my ITemplate class that builds the DropDown in the DataGrid:

public class RoleColumn : ITemplate
{
    public void InstantiateIn(Control container)
    {
        DropDownList myDropDownList = new DropDownList();
        myDropDownList.ID = "RoleID";
        myDropDownList.DataSource = BaseDB.GetList(0,"Role");
        myDropDownList.DataTextField = "Text";
        myDropDownList.DataValueField = "Value";
        myDropDownList.CssClass = "formTB";
        myDropDownList.DataBinding += new EventHandler(this.BindRoleColumn);

        container.Controls.Add(new LiteralControl("<center>"));
        container.Controls.Add(myDropDownList);
        container.Controls.Add(new LiteralControl("</center>"));

    }
    public void BindRoleColumn(object sender, EventArgs e)
    {
        int SelectedRoleID;
        DropDownList dropdownlist = (DropDownList)sender;

        dropdownlist.Items.Insert(0, " ");
        dropdownlist.Items[0].Value = "0";

        DataGridItem container = (DataGridItem)dropdownlist.NamingContainer;
        SelectedRoleID =
Convert.ToInt32(((DataRowView)container.DataItem)["RoleID"]);

        if (dropdownlist.Items.FindByValue(SelectedRoleID.ToString()) != null)
            dropdownlist.Items.FindByValue(SelectedRoleID.ToString()).Selected =
true;
    }
}

It doesn't even add the new Item to the DropDownList. I had though that my
'BindRoleColumn' wasing even being executed, but I force an exception there
and it throws it. Thanks for the help.

-Ray

Reply to this message...
 
    
Charles M. Carroll (VIP)
http://www.aspfriends.com/aspfriends/aspngdata.asp
is a much better list for that kind of question.

If they don't answer you there follow the rules @
http://www.aspfriends.com/aspfriends/aspngescalate.asp?tab=rules" target="_blank">http://www.aspfriends.com/aspfriends/aspngescalate.asp?tab=rules

http://www.aspfriends.com/aspfriends/aspngescalate.asp
is where tough unanswered questions can get escalated after 2 attempts fail.

At 12:24 PM 12/28/2001 -0600, you wrote:
[Original message clipped]

Reply to this message...
 
    
Andrea Williams
I'm having the same issue, however I've discovered that the Item is being added, but not showing up in the list. The reason I think it's added is that if my DataSet value that is to be bound with the control is equal to "", then I don't get an error. Howver, if I take out the line:

cboSender.Items.Insert(0, new ListItem("[Select One]", ""));

then the page errors b/c there's not a value with which to match an item in the DropDownList.

I don't see a date on this post so I'm not sure when it was posted, but I really need this to work!

My ITemplate code is as follows:
        /// <summary>    Instantiation of the Item Template</summary>
        public override void InstantiateIn(Control container)
        {
            // Bind a string to the event
            DropDownList cboResult = new DropDownList();

            if (this.ControlID != null)
                cboResult.ID = this.ControlID;

            cboResult.DataSource        = this.mobjDataSource;
            cboResult.DataTextField        = this.mstrDataTextField;
            cboResult.DataValueField    = this.mstrDataValueField;

            cboResult.DataBinding += new EventHandler(this.BindData);

            container.Controls.Add(cboResult);
        }

        /// <summary>    Binds the DataField to the specified Label</summary>
        protected void BindData(object sender, System.EventArgs e)
        {
            if (!Common.Global.isNothing(this.DataField))
            {
                DropDownList cboSender = (DropDownList) sender;

                DataGridItem container = (DataGridItem) cboSender.NamingContainer;
                string strDataFieldValue = ((System.Data.DataRowView)container.DataItem)[this.DataField].ToString();

                cboSender.Items.Insert(0, new ListItem("[Select One]", ""));//This is the line that isn't working
//                BTW, if I leave this line out and the value of strDataFieldValue = "", I get an error b/c the value "" doesn't exist in the DataSet.

                    cboSender.SelectedValue = strDataFieldValue;

                cboSender = null;
            }
        }

--------------------------------
From: Andrea Williams
Reply to this message...
 
    
Andrea Williams
I also needed this functionality and found the solution with help from the author of this article:

http://www.codeproject.com/aspnet/ASPNET_DataGrid_creation.asp?msg=890291#xx890291xx

The post and response are located at the bottom of the article. Hope it helps!
--------------------------------
From: Andrea Williams
Reply to this message...
 
 
System.Convert
System.Data.DataRowView
System.Data.DataSet
System.EventArgs
System.EventHandler
System.Web.UI.ITemplate
System.Web.UI.LiteralControl
System.Web.UI.WebControls.DataGrid
System.Web.UI.WebControls.DataGridItem
System.Web.UI.WebControls.DataGridItemEventHandler
System.Web.UI.WebControls.DataGridSortCommandEventHandler
System.Web.UI.WebControls.DropDownList
System.Web.UI.WebControls.ListItem
System.Web.UI.WebControls.TemplateColumn
System.Windows.Forms.DataGrid




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com