A
Andrea Williams
I'm trying to bind to a Template Item that I created and fill the dropdown
with a value from the datagrid DataSet. However, if the value is "", then I
want to Insert a "[Select One]" option to the list. I'm trying to do this
as the binding takes place, but it's not working. The extra option is not
added. Any advice?
The goal: do in-line editing inside the datagrid without adding code to the
ASPX page. I want it all in the code behind. It's got to be possible, I
just haven't figured it out yet.... The labels and textboxes are working
fine, but the DropDownList box is giving me headaches.
Andrea
PS: Here's the code in the custom class which inherits ITemplate.
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);
}
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;
}
}
with a value from the datagrid DataSet. However, if the value is "", then I
want to Insert a "[Select One]" option to the list. I'm trying to do this
as the binding takes place, but it's not working. The extra option is not
added. Any advice?
The goal: do in-line editing inside the datagrid without adding code to the
ASPX page. I want it all in the code behind. It's got to be possible, I
just haven't figured it out yet.... The labels and textboxes are working
fine, but the DropDownList box is giving me headaches.
Andrea
PS: Here's the code in the custom class which inherits ITemplate.
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);
}
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;
}
}