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

Insert / Update with Auto Columns

1 Answer 133 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David Boylan
Top achievements
Rank 2
David Boylan asked on 06 Mar 2009, 12:45 PM
Hi All
 
I will try to keep this simple as possible.  
 
I have a data source that changes constantly.  It always contains two fields: JOB_ID, TABLE_NAME.  Then a number of extra fields will exist.  For example sometimes it will contain NAME, DOB.  Some other time it might contain FAVE_COLOUR, FAVE_HAT.
 
The datasource is taken from a dropdownlist that then updates the gird. 

So for example the data source could like this:
 
JOB_ID   TABLE_NAME   NAME,  DOB
 
or like this
 
JOB_ID   TABLE_NAME   FAVE_COLOUR    FAVE_HAT
 
or like 
 
JOB_ID   TABLE_NAME   X    Y   Z  ......so on....
 
So my grid is set to automatically generate columns.
 
I also need to place onto the grid inset, edit and delete buttons which has been successfully done.
 
I have successfully written code for the update, insert and delete commands.  So that is not an issue.
 
My remaining issue comes when I handle the auto generate Edit form which is used for both the Edit and Insert operations.  When someone wishes to insert a new record they need provide all the data but when they wish to edit they only need to provide the random columns and not the JOB_ID and TABLE_NAME.  
 
When you are inserting the user should be able to choose the JOB_ID and TABLE_NAME from a drop down list so I have used the grids dropdown GridDropDownColumn for both these fields.  Which is working fine.  But I do now get the columns twice as they are being specified at design and run time now by the auto generate feature.
 
When you are editing the user should not be able to choose a JOB_ID and TABLE_NAME as they form the primary key for the update.  So they need to be hidden or displayed and locked down.
 
It seems every approach I take something does not work.  For example one approach looks for and hides the repeated columns (JOB_ID, TABLE_NAME) and hides them but this only works when the grid contains data.  When there is no data the columns appear.  Also when I go to the edit form I disable the dropdown lists but I cannot tell between an update and an insert.  So it disables them for both!

I have included the grid and the events I am using to try and extract this funtionaility

Thanks for your help
 
  <telerik:RadComboBox ID="ddTables" runat="server" EnableEmbeddedSkins="false" Skin="LBCompanion_Blue" 
        AllowCustomText="true" MarkFirstMatch="true"
    </telerik:RadComboBox> 
    <br /> 
    <br /> 
 
    <telerik:RadGrid ID="dgResults" runat="server" EnableEmbeddedSkins="false" Skin="LBCompanion_Blue" 
        EnableLinqExpressions="False" OnNeedDataSource="dgResults_NeedDataSource" AllowFilteringByColumn="True" 
        AllowSorting="True" AllowPaging="True" PageSize="40" GridLines="None"
        <HeaderContextMenu EnableTheming="True"
            <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
        </HeaderContextMenu> 
        <MasterTableView AutoGenerateColumns="True" CommandItemDisplay="Top" DataKeyNames="JOB_ID, TABLE_NAME"
            <RowIndicatorColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </RowIndicatorColumn> 
            <ExpandCollapseColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </ExpandCollapseColumn> 
            <Columns> 
                <telerik:GridDropDownColumn DataField="JOB_ID" HeaderText="JOB_ID" UniqueName="FIXED_JOB_ID" 
                    AutoPostBackOnFilter="true" ListTextField="ID" ListValueField="ID" DataSourceID="odsDataEvents" 
                    ItemStyle-Width="100" FilterControlWidth="100"
                </telerik:GridDropDownColumn> 
                <telerik:GridDropDownColumn DataField="TABLE_NAME" HeaderText="TABLE_NAME" UniqueName="FIXED_TABLE_NAME" 
                    AutoPostBackOnFilter="true" ListTextField="TABLE_NAME" ListValueField="TABLE_NAME" DataSourceID="odsTables" 
                    ItemStyle-Width="100" FilterControlWidth="100"
                </telerik:GridDropDownColumn> 
                <telerik:GridEditCommandColumn UniqueName="Edit"
                </telerik:GridEditCommandColumn> 
                <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="Delete"
                </telerik:GridButtonColumn> 
            </Columns> 
            <EditFormSettings> 
                <EditColumn InsertText="Insert value" UpdateText="Update value" UniqueName="EditCommandColumn1" 
                    CancelText="Cancel edit"
                </EditColumn> 
            </EditFormSettings> 
        </MasterTableView> 
        <FilterMenu EnableTheming="True"
            <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
        </FilterMenu> 
        <ClientSettings> 
            <Scrolling AllowScroll="True" ScrollHeight="400" UseStaticHeaders="True" SaveScrollPosition="True"
            </Scrolling> 
        </ClientSettings> 
    </telerik:RadGrid> 

 void dgResults_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
        { 
 
            if (e.Item.ItemType == GridItemType.EditFormItem) 
            { 
 
                GridEditableItem eeditedItem = e.Item as GridEditableItem; 
 
                editedItem["FIXED_JOB_ID"].Enabled = false
                editedItem["FIXED_TABLE_NAME"].Enabled = false
            } 
        }  
    } 
 
 void dgResults_PreRender(object sender, EventArgs e) 
    { 
 
 
 
 
        foreach (GridDataItem dataItem in dgResults.MasterTableView.Items) 
        { 
            foreach (GridColumn column in dgResults.MasterTableView.RenderColumns) 
            { 
                if (column is GridBoundColumn) 
                { 
 
                    if (column.UniqueName == "JOB_ID" || column.UniqueName == "TABLE_NAME") 
                    { 
                        GridBoundColumn bcol = (GridBoundColumn)column; 
                        bcol.ReadOnly = true
                        bcol.Visible = false
                    } 
                } 
            } 
        } 
 
 
        GridColumnCollection cols = dgResults.MasterTableView.Columns; 
        GridColumn colo = cols.FindByUniqueName("Edit"); 
 
        colo.OrderIndex = 0
 
        colo = cols.FindByUniqueName("Delete"); 
 
        colo.OrderIndex = 1
 
 
 
    } 
 
 

 

1 Answer, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 10 Mar 2009, 12:20 PM
Hello Geoff,

You can try hiding the column with the Display property. Just set it to "false".
There should be no rendered columns if the grid is not bound.

You chose the right approach to hide or disable the items in the auto-generated edit form. To enable the first two columns, when an Insert operation is performed, you will need to attach your code to the ItemCommand event. When it is fired you need to check whether the e.CommandName is RadGrid.UpdateCommandName or RadGrid.CancelCommandName. Then just enable the first two columns.

Here is a code snippet showing how to achieve this:
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.CommandName == RadGrid.CancelCommandName) 
        { 
            GridEditableItem editedItem = e.Item as GridEditableItem; 
 
        editedItem["FIXED_JOB_ID"].Enabled = false;  
                editedItem["FIXED_TABLE_NAME"].Enabled = false;  
 
    } 
    } 

Best regards,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
David Boylan
Top achievements
Rank 2
Answers by
Georgi Krustev
Telerik team
Share this question
or