How to Bind a DropDownList control to a DataGrid in Template Column

  • Thread starter Thread starter Jeff Petter
  • Start date Start date
J

Jeff Petter

I'm having a tough time with this, and I can't find any resources that address it. I have a DataGrid comprised of Template Columns, in which I have both TextBox and DropDownList controls. The purpose of the DataGrid is to allow users to edit previously made selections, and I am using a DataSet and a DataView to load the XML data into. The idea is so that when the grid loads, it is populated with the data from the XML source. It works great for the TextBoxes, but I can't get the syntax down right for the DropDownList controls. Depending upon how I edit the code, I either get an error informing me that the control doesn't exist in the namespace, which it clearly does. Or I get the list items: one letter per row, in the box. In my previous and limited experience with asp.net, I've done all my binding in the "code behind" page, but all the examples I saw embedded the code in the HTML code, so that is what I've done.

Here is one binding statement:
<asp:DropDownList id=ddExceptions runat="server" DataSource='<%# DataBinder.Eval(Container, "DataItem.exception") %>' Width="173px"> - This results in displaying the list items, but only one letter per row. So, I decided to try the SelectedIndex property and have the binding statement:
<asp:DropDownList id=ddExceptions runat="server" SelectedIndex='<%# ddExceptions.SetIndex(DataBinder.Eval(Container, "DataItem.exception")) %>' Width="173px">
This results in error essages stating that 'ddExceptions' does not exist in the namespace. And I've played with the syntax for over a day, all to no avail. Does anyone know ANY way to do this, or have any ideas that I might try?

I appreciate it a bunch. BTW, FWIW, the code is in c#.

Thanks,
Jeff
 
I'm having a tough time with this, and I can't find any resources that address it. I have a DataGrid comprised of Template Columns,
in which I have both TextBox and DropDownList controls. The purpose
of the DataGrid is to allow users to edit previously made selections,
and I am using a DataSet and a DataView to load the XML data into. The
idea is so that when the grid loads, it is populated with the data
from the XML source. It works great for the TextBoxes, but I can't get
the syntax down right for the DropDownList controls. Depending upon
how I edit the code, I either get an error informing me that the
control doesn't exist in the namespace, which it clearly does. Or I
get the list items: one letter per row, in the box. In my previous and
limited experience with asp.net, I've done all my binding in the "code
behind" page, but all the examples I saw embedded the code in the HTML
code, so that is what I've done.
Here is one binding statement:
<asp:DropDownList id=ddExceptions runat="server" DataSource='<%# DataBinder.Eval(Container, "
DataItem.exception") %>' Width="173px"> - This results in displaying
the list items, but only one letter per row. So, I decided to try the
SelectedIndex property and have the binding statement:
<asp:DropDownList id=ddExceptions runat="server" SelectedIndex='<%# ddExceptions.
SetIndex(DataBinder.Eval(Container, "DataItem.exception")) %>'
Width="173px">
This results in error essages stating that 'ddExceptions' does not exist in the namespace.
And I've played with the syntax for over a day, all to no avail. Does
anyone know ANY way to do this, or have any ideas that I might try?
I appreciate it a bunch. BTW, FWIW, the code is in c#.

Thanks,
Jeff
Ive busted the above as it was all in about 3 lines. Please could you
set your line length to something more reasonable.

This is in the aspx page.

<EditItemTemplate>
<asp:DropDownList runat="server" id="EditlstLabels"
DataValueField="LabelID" DataTextField="LabelName" DataSource="<%#
GetLabels() %>" />
</EditItemTemplate>

This is in the code behind. Don't worry about the sql code but it
returns a dataset containg the data for the drop down.

protected DataSet GetLabels()
{
string strConn =
ConfigurationSettings.AppSettings["ConnectionString"];
string spName = "SelectRecordLabel";
ddlDataSet = SqlHelper.ExecuteDataset(strConn,spName,"%");
return ddlDataSet;
}

this function sets the correct value

private void dgPackage_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
DropDownList Myddl;
Myddl =
(DropDownList)e.Item.FindControl("EditlstLabels");
Myddl.SelectedIndex =
Myddl.Items.IndexOf(Myddl.Items.FindByText(sCurrentLabel));
}
}

sCurrentLabel is a string set on the editCommand so

private void dgPackage_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgPackage.EditItemIndex = e.Item.ItemIndex;
Label MyLabel;
MyLabel = (Label)
e.Item.FindControl("LabelID");
sCurrentLabel = MyLabel.Text;
FillGrid();
}

Hopefully this will get you started.
 
Back
Top