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; |
} |