This is a migrated thread and some comments may be shown as answers.

Create grid dynamically problems

1 Answer 127 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tompa
Top achievements
Rank 1
Tompa asked on 26 Aug 2008, 07:27 AM

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>&nbsp;<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

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 26 Aug 2008, 09:32 AM
Hello Tomas,

The provided sample code seems fine as far as we can go through it without knowledge of the data source structure. Please note, however, that when you are manually binding your dropdowns in edit mode inside the ItemDataBound event, the dropdowns do not default to the current value in the row. It is your responsibility to ensure that the dropdowns have the correct item selected.

Automatic binding of dropdown columns is implemented only when you directly bind your GridDropDownColumn to some data source control, either through the DataSourceID or the ListDataMember property and define its relation to the main RadGrid data source through the DataMember and ListValueField properties. For more information on the GridDropDownColumn specifics, you may refer to the following help articles:

Column types

Customize/Configure GridDropDownColumn

Best wishes,
Veli
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Tompa
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or