Hi,
We are trying to create a grid dynamically from an untyped dataset (we don't know the amount of columns in it). Basically it should be a grid that renders in edit-mode (in-place) so that it contains drop-down selections for each row/column.
We bind the grid to a datasource (ObjectDataSource1) that fills is with all selected values for each column and row, and then in the ItemDatabound we try to hook additional datasources for the dropdowns so that they contain the additional selectable values.
Now, the grid seems to render correctly and each dropdown have the correct values to select from, but the default selections in the dropdowns doesn't match the values in the original datasource. We want those two to "bind" so to speak. What's the missing peace?
Here's the aspx-code:
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="ObjectDataSource1" |
GridLines="None" Skin="Vista" AutoGenerateColumns="False" OnPreRender="RadGrid1_PreRender" AllowMultiRowEdit="True" OnItemDataBound="RadGrid1_ItemDataBound" AllowMultiRowSelection="True" OnCreateColumnEditor="RadGrid1_CreateColumnEditor"> |
<MasterTableView DataSourceID="ObjectDataSource1" EditMode="InPlace" Name="Master"> |
<RowIndicatorColumn> |
<HeaderStyle Width="20px" /> |
</RowIndicatorColumn> |
<ExpandCollapseColumn> |
<HeaderStyle Width="20px" /> |
</ExpandCollapseColumn> |
</MasterTableView> |
</telerik:RadGrid> <br /> |
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" |
SelectMethod="SelectByVersionId" TypeName="Insera.Application.Web.CorrectRent.Business.AreaValuationGrid" EnableCaching="True"> |
<SelectParameters> |
<asp:ProfileParameter Name="VersionId" PropertyName="CompanyVersionId" Type="Object" /> |
</SelectParameters> |
</asp:ObjectDataSource> |
Code behind Page_Init that will add all columns based on the returned dataset:
DataView dv = (DataView)ObjectDataSource1.Select(); |
DataSet ds = dv.Table.DataSet.Copy(); |
// Loop rows in main dataset and add dropdowncolumns |
foreach (DataRow row in ds.Tables[1].Rows) |
{ |
Guid parameterId = new Guid(row[0].ToString()); |
string parameterDescription = row[1].ToString(); |
GridDropDownColumn dropDownColumn = new GridDropDownColumn(); |
this.RadGrid1.MasterTableView.Columns.Add(dropDownColumn); |
dropDownColumn.UniqueName = parameterId.ToString(); |
dropDownColumn.HeaderText = parameterDescription; |
dropDownColumn.ListTextField = "Description"; |
dropDownColumn.ListValueField = "FactorId"; |
dropDownColumn.DataField = parameterId.ToString(); |
dropDownColumn.DropDownControlType = GridDropDownColumnControlType.RadComboBox; |
} |
Set all rows to editable upon rendering:
protected void RadGrid1_PreRender(object sender, EventArgs e) |
{ |
if (!IsPostBack) |
{ |
foreach (GridItem item in RadGrid1.MasterTableView.Items) |
{ |
if (item is GridEditableItem) |
{ |
GridEditableItem editableItem = item as GridDataItem; |
editableItem.Edit = true; |
} |
} |
RadGrid1.Rebind(); |
} |
} |
Finally bind the dropdowns to the "unique-per-column" datasources:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode) |
{ |
GridEditableItem editedItem = e.Item as GridEditableItem; |
GridEditManager editMan = editedItem.EditManager; |
foreach (GridColumn gridColumn in this.RadGrid1.MasterTableView.Columns) |
{ |
if (gridColumn is GridDropDownColumn) |
{ |
GridDropDownListColumnEditor editor = (GridDropDownListColumnEditor)(editMan.GetColumnEditor(gridColumn.UniqueName)); |
editor.DataTextField = "Description"; |
editor.DataValueField = "FactorId"; |
ObjectDataSource factorObjectDataSource; |
if (factorDataSources.ContainsKey(gridColumn.UniqueName)) |
{ |
factorObjectDataSource = factorDataSources[gridColumn.UniqueName] as ObjectDataSource; |
} |
else |
{ |
factorObjectDataSource = new ObjectDataSource(); |
factorObjectDataSource.TypeName = "Insera.Application.Web.CorrectRent.Business.Factor"; |
factorObjectDataSource.SelectMethod = "SelectByParameterId"; |
factorObjectDataSource.SelectParameters.Add(new System.Web.UI.WebControls.Parameter("ParameterId", TypeCode.Object, gridColumn.UniqueName)); |
factorObjectDataSource.EnableCaching = true; |
factorObjectDataSource.ID = gridColumn.UniqueName; |
factorDataSources.Add(gridColumn.UniqueName, factorObjectDataSource); |
} |
editor.DataSource = factorObjectDataSource; |
editor.DataBind(); |
} |
} |
} |
} |
Regards
Tomas