I have a RadGrid that is bound in the code behind, on on ItemDataBound I'm doing binding to a RadComboBox that is within a TemplateColumn inside the grid:
So they can edit the County Dropdown at will, and click a save button below.
The county dropdown is being populated from a database table, but only has entries that are not currently in the grid.
For instance, Suppose i have 5 Counties:
{Lancaster, York, Berks, Adams, Allegheny}
the Grid has 3 entries for the counties "York, Adams, Allegheny". Therefore, each combo box should have the two that are not present anywhere in the grid (Lancaster, Berks), as well as one entry for that grid row
So the grid would look like this:
Row 1: Combo with options {Lancaster, Berks, York}
Row 2: Combo with options {Lancaster, Berks, Adams}
Row 3: Combo with options {Lancaster, Berks, Allegheny}
However, the results I'm getting are:
Row 1: Combo with options {Lancaster, Berks, York}
Row 2: Combo with options {Lancaster, Berks, York, Adams}
Row 3: Combo with options {Lancaster, Berks, York, Adams, Allegheny}
Even though on Each row I'm rebinding the combobox and adding the missing one.
You can see I'm rebinding each combo and then adding a new value on a per-row basis. But it seems that on each row, thecombo.Items.Add() is being applied additively.
Any idea what I'm doing wrong or how to fix it?
thanks!
<
tel:RadGrid
runat
=
"server"
ID
=
"rgCounties"
OnItemCommand
=
"rgCounties_ItemCommand"
AutoGenerateColumns
=
"False"
OnItemDataBound
=
"rgCountes_ItemDataBound"
AllowMultiRowEdit
=
"True"
CellSpacing
=
"0"
GridLines
=
"None"
>
<
MasterTableView
>
<
Columns
>
<
tel:GridBoundColumn
Display
=
"false"
DataField
=
"ID"
UniqueName
=
"SectionCountyID"
/>
<
tel:GridTemplateColumn
HeaderText
=
"County"
UniqueName
=
"CountyColumn"
>
<
InsertItemTemplate
>
<
tel:RadComboBox
runat
=
"server"
ID
=
"rgrcCounty"
DataTextField
=
"CountyName"
DataValueField
=
"ID"
/>
</
InsertItemTemplate
>
<
ItemTemplate
>
<
tel:RadComboBox
runat
=
"server"
ID
=
"rgrcCounty"
DataTextField
=
"CountyName"
DataValueField
=
"ID"
/>
</
ItemTemplate
>
</
tel:GridTemplateColumn
>
</
Columns
>
</
MasterTableView
>
</
tel:RadGrid
>
So they can edit the County Dropdown at will, and click a save button below.
The county dropdown is being populated from a database table, but only has entries that are not currently in the grid.
For instance, Suppose i have 5 Counties:
{Lancaster, York, Berks, Adams, Allegheny}
the Grid has 3 entries for the counties "York, Adams, Allegheny". Therefore, each combo box should have the two that are not present anywhere in the grid (Lancaster, Berks), as well as one entry for that grid row
So the grid would look like this:
Row 1: Combo with options {Lancaster, Berks, York}
Row 2: Combo with options {Lancaster, Berks, Adams}
Row 3: Combo with options {Lancaster, Berks, Allegheny}
However, the results I'm getting are:
Row 1: Combo with options {Lancaster, Berks, York}
Row 2: Combo with options {Lancaster, Berks, York, Adams}
Row 3: Combo with options {Lancaster, Berks, York, Adams, Allegheny}
Even though on Each row I'm rebinding the combobox and adding the missing one.
protected
void
rgCountes_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
var ditem = e.Item
as
GridDataItem;
var combo = ((RadComboBox)ditem[
"CountyColumn"
].FindControl(
"rgrcCounty"
));
var sectionCountyID =
int
.Parse(ditem[
"SectionCountyID"
].Text);
var sectionCounty = Model.SectionCounties.Where(sc => sc.ID == sectionCountyID).FirstOrDefault();
combo.Bind(Model.Counties,
false
);
if
(sectionCounty !=
null
)
{
combo.AddOrSelectByValue(sectionCounty.CountyID.ToString(), sectionCounty.County.CountyName);
rnt.Text = sectionCounty.Percentage.ToString();
}
}
}
public static bool AddOrSelectByValue(this RadComboBox combo, string value, string text)
{
bool tf = combo.SelectByValue(value);
if (!tf)
{
combo.Items.Add(new RadComboBoxItem(text, value));
combo.SelectByValue(value);
}
return tf;
}
You can see I'm rebinding each combo and then adding a new value on a per-row basis. But it seems that on each row, thecombo.Items.Add() is being applied additively.
Any idea what I'm doing wrong or how to fix it?
thanks!