I have a RadGrid, displays strings and ints, and lets you edit them easily.
When I insert, i require a drop down box to be able to select a product, and another drop down box which will let you select units (count, box of 12, case of 4 boxes) etc. Each Unit is dependent on the Product selected.
Currently the first drop down box has an SelectedIndexChanged Event, which will update the ObjectDataSource and requery.
Now I need to be able to grab the second (unit) Combobox, and do a rebind.
It seems since it's in an insert row, it's difficult to find the control in the grid.
Any Suggestions?
When I insert, i require a drop down box to be able to select a product, and another drop down box which will let you select units (count, box of 12, case of 4 boxes) etc. Each Unit is dependent on the Product selected.
Currently the first drop down box has an SelectedIndexChanged Event, which will update the ObjectDataSource and requery.
Now I need to be able to grab the second (unit) Combobox, and do a rebind.
It seems since it's in an insert row, it's difficult to find the control in the grid.
Any Suggestions?
7 Answers, 1 is accepted
0
Jayesh Goyani
Top achievements
Rank 2
answered on 06 Sep 2011, 05:06 PM
Hello,
Thanks,
Jayesh Goyani
protected
void
ddlProduct_indexchanged(
object
sender, EventArgs e)
{
DropDownList ddlProduct = (DropDownList)sender;
GridEditFormItem editItem = (GridEditFormItem)ddlProduct.NamingContainer;
DropDownList ddlUnit = (DropDownList)editItem.FindControl(
"ddlUnit"
);
// bind your dropdown here
ddlUnit.DataSource =
"Your DataSource"
;
ddlUnit
.DataTextField = "text";
ddlUnit
.DataValueField = "value";
ddlUnit.DataBind();
}
Thanks,
Jayesh Goyani
0
Corey
Top achievements
Rank 1
answered on 06 Sep 2011, 05:41 PM
Thanks Jayesh.
Below is my code:
This works wonderfully, However, Since RadComboBox1 is bound to the new row, when i hit the databind, it returns:
Any suggestions on how to still stay bound and update the data? The values returned are 1, 2 or 3, and the text changes.
Below is my code:
protected void cboItem_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
ODSUnitList.SelectParameters["id"].DefaultValue = e.Value;
ODSUnitList.Select();
GridDataInsertItem editItem = (GridDataInsertItem)((RadComboBox)sender).NamingContainer;
((RadComboBox)editItem.FindControl("RadComboBox1")).DataSource = ODSUnitList;
((RadComboBox)editItem.FindControl("RadComboBox1")).DataBind();
}
This works wonderfully, However, Since RadComboBox1 is bound to the new row, when i hit the databind, it returns:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Any suggestions on how to still stay bound and update the data? The values returned are 1, 2 or 3, and the text changes.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 06 Sep 2011, 05:57 PM
Hello,
Yes, Please provide your ASPX page code.
or
Your code should like.
Let me know if any concern.
Thanks,
Jayesh Goyani
Yes, Please provide your ASPX page code.
or
Your code should like.
<
telerik:GridTemplateColumn
UniqueName
=
"ContactTitle"
HeaderText
=
"ContactTitle"
>
<
EditItemTemplate
>
<
asp:DropDownList
ID
=
"DropDownList1"
runat
=
"server"
AutoPostBack
=
"true"
SelectedValue='<%# Bind("ContactTitle") %>' DataSourceID="SqlDataSource1" DataTextField="ContactTitle" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
</
asp:DropDownList
>
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridTemplateColumn
UniqueName
=
"TemplateColumn"
>
<
EditItemTemplate
>
<
asp:DropDownList
ID
=
"DropDownList2"
runat
=
"server"
>
</
asp:DropDownList
>
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
protected
void
DropDownList1_SelectedIndexChanged(
object
sender, EventArgs e)
{
DropDownList ddl1 = (DropDownList)sender;
GridEditableItem editedItem = ddl1.NamingContainer
as
GridEditableItem;
DropDownList ddl2 = editedItem.FindControl(
"DropDownList2"
)
as
DropDownList;
// change the data source for ddl2 here
.....
}
Let me know if any concern.
Thanks,
Jayesh Goyani
0
Corey
Top achievements
Rank 1
answered on 06 Sep 2011, 06:23 PM
Below is the aspx snippet:
<
telerik:GridTemplateColumn
DataField
=
"ItemDescription"
FilterControlAltText
=
"Filter ItemDescription column"
HeaderText
=
"ItemDescription"
SortExpression
=
"ItemDescription"
UniqueName
=
"ItemDescription"
>
<
EditItemTemplate
>
<
asp:Label
runat
=
"server"
ID
=
"lblItemDescription"
Text='<%# Bind("ItemDescription") %>'></
asp:Label
>
</
EditItemTemplate
>
<
ItemTemplate
>
<
asp:Label
runat
=
"server"
ID
=
"lblItemDescription"
Text='<%# Bind("ItemDescription") %>'></
asp:Label
>
</
ItemTemplate
>
<
InsertItemTemplate
>
<
telerik:RadComboBox
ID
=
"cboItem"
runat
=
"server"
DataSourceID
=
"ODSItems"
DataTextField
=
"Description"
DataValueField
=
"InventoryItemID"
SelectedValue='<%# Bind("ItemID") %>' AutoPostBack="true" OnSelectedIndexChanged="cboItem_SelectedIndexChanged" />
</
InsertItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridTemplateColumn
HeaderText
=
"Unit"
SortExpression
=
"Unit"
UniqueName
=
"Unit"
>
<
ItemTemplate
>
<
asp:Label
runat
=
"server"
ID
=
"lblUnit"
Text='<%# Bind("Unit") %>'></
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
telerik:RadComboBox
ID
=
"RadComboBox1"
runat
=
"server"
DataSourceID
=
"ODSUnitList"
DataTextField
=
"UnitDescription"
DataValueField
=
"UnitValue"
SelectedValue='<%# Bind("UnitID") %>' />
<
asp:RequiredFieldValidator
ID
=
"RequiredFieldValidator1"
runat
=
"server"
ErrorMessage
=
"*"
ControlToValidate
=
"RadComboBox1"
ForeColor
=
"Red"
SetFocusOnError
=
"true"
Display
=
"Dynamic"
/>
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
0
Jayesh Goyani
Top achievements
Rank 2
answered on 06 Sep 2011, 07:07 PM
Hello,
and assign selectedvalue to radcombobox in ItemDataBound Event.
Thanks,
Jayesh Goyani
<
telerik:RadComboBox
ID
=
"RadComboBox1"
runat
=
"server"
/>
protected void cboItem_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
...............
...............
...............
((RadComboBox)editItem.FindControl("RadComboBox1")).DataSource = ODSUnitList;
((RadComboBox)editItem.FindControl("RadComboBox1")).DataTextField = "UnitDescription
";
((RadComboBox)editItem.FindControl("RadComboBox1")).DataValueField = "UnitValue
";
((RadComboBox)editItem.FindControl("RadComboBox1")).DataBind();
}
and assign selectedvalue to radcombobox in ItemDataBound Event.
Thanks,
Jayesh Goyani
0
Corey
Top achievements
Rank 1
answered on 06 Sep 2011, 08:11 PM
This doesn't work because it hits the RadGrid1_InsertCommand, before the RadGrid1_ItemDataBound.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 07 Sep 2011, 05:41 AM
Hello,
You also have to bind Your DropDown in ItemDataBound also.
Thanks,
Jayesh Goyani
You also have to bind Your DropDown in ItemDataBound also.
<
MasterTableView
DataKeyNames
=
"UnitID"
/>
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
RadComboBox cmb = item.FindControl(
"RadComboBox11"
)
as
RadComboBox;
cmb.DataSource =
"Your datasource"
;
cmb.DataTextField =
"UnitDescription"
;
cmb.DataValueField =
"UNitValue"
;
cmb.DataBind();
if
(
string
.IsNullOrEmpty(Convert.ToString(item.GetDataKeyValue(
"UnitID"
))))
{
cmb.SelectedValue = item.GetDataKeyValue(
"UnitID"
).ToString();
}
}
}
Thanks,
Jayesh Goyani