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

How bind my telerik:GridDropDownColumn to two different data source based on Edit and Insert mode.

10 Answers 368 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Syed
Top achievements
Rank 1
Syed asked on 07 Feb 2014, 04:13 AM


Hi Princy

I have a requirement where I would like to bind my telerik:GridDropDownColumn to two
different data source based on Edit and Insert mode.

For example if I am inserting a record I would like to
bind my telerik:GridDropDownColumn
to a data source Name “Insertdatasource” and if it is in edit/update mode I
would like to bind the same telerik:GridDropDownColumn
to “Updatedatasource”.

The reason why I would like to do this as I do not want
to show the option which my user have already inserted inside my grid. When he
insert new row only those option will appear which are not already inserted.

I hope you understand my problem.

I look forward for your response soon

Thanks in advance

Syed Qazafi

10 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 07 Feb 2014, 05:38 AM
Hi Syed,

Please try the following code snippet to set different datasource on Insert and Update for GridDropDownColumn as DropDownList:

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted)
    {
        GridEditableItem editedItem = (GridEditableItem)e.Item;
        DropDownList dropEdit = (DropDownList)editedItem["GridDropDownColumnUniqueName"].Controls[0];      
        dropEdit.DataSourceID = "Updatedatasource";
        dropEdit.DataTextField = "TextField";
        dropEdit.DataValueField = "ValueField";
        
    }
    else if (e.Item is GridEditableItem && e.Item.OwnerTableView.IsItemInserted && e.Item.IsInEditMode)
    {
        GridEditableItem editedItem = (GridEditableItem)e.Item;
        DropDownList dropInsert = (DropDownList)editedItem["GridDropDownColumnUniqueName"].Controls[0];
        dropInsert.DataSourceID = "Insertdatasource";
        dropInsert.DataTextField = "TextField";
        dropInsert.DataValueField = "ValueField";
    }
}

Thanks,
Princy
0
Syed
Top achievements
Rank 1
answered on 11 Feb 2014, 02:56 AM


Hi Princy

That is
great. It is working. The only one thing is

This is
aspx code for GridDropDownColumn

    <telerik:GridDropDownColumn DataSourceID="DSProjectPhase"   ListTextField="ProjectPhaseDetailDescription" ListValueField="ProjectPhaseID"

                    HeaderText="Phase" DataField="ProjectPhaseID" UniqueName="ProjectPhaseID"

                    DropDownControlType="DropDownList" AllowAutomaticLoadOnDemand="true"

                    AllowVirtualScrolling="true" ShowMoreResultsBox="true" ItemsPerRequest="10" EnableEmptyListItem="True" EmptyListItemValue="-1" EmptyListItemText="Select a value">

                    <HeaderStyle Font-Names="Verdana" Font-Size="10pt" Width="250px" Font-Bold="False" />

                    <ItemStyle Font-Names="Verdana" Font-Size="10pt" Width="250px" />

                    <ColumnValidationSettings EnableRequiredFieldValidation="True">

                        <RequiredFieldValidator ErrorMessage="Phase is required Field" Font-Bold="True" Font-Names="Verdana" Font-Size="10pt" ForeColor="Red" InitialValue="-1" SetFocusOnError="True" ToolTip="Phase is required Field">Phase is required Field</RequiredFieldValidator>

                        <ModelErrorMessage Text=""></ModelErrorMessage>

                    </ColumnValidationSettings>

                    <FilterTemplate>

 

                        <telerik:RadComboBox ID="RadComboBoxTitle" Skin="BlackMetroTouch" DataSourceID="DSProjectPhase"

                            DataTextField="ProjectPhaseDetailDescription"

                            DataValueField="ProjectPhaseID" Height="250px" AppendDataBoundItems="true" SelectedValue='<%# TryCast(Container, GridItem).OwnerTableView.GetColumn("ProjectPhaseTypeID").CurrentFilterValue%>'

                            runat="server" OnClientSelectedIndexChanged="TitleIndexChanged" Font-Names="verdana" Font-Size="12">

                            <Items>

                                <telerik:RadComboBoxItem Text="All" />

                            </Items>

                        </telerik:RadComboBox>

                        <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">

 

                            <script type="text/javascript">

 

                                function
TitleIndexChanged(sender, args) {

 

                                    var tableView = $find("<%# TryCast(Container,GridItem).OwnerTableView.ClientID
%>");

 

                                   
tableView.filter("ProjectPhaseTypeID", args.get_item().get_value(), "EqualTo");

 

 

 

                                }

 

                            </script>

 

                        </telerik:RadScriptBlock>

 

                    </FilterTemplate>

                </telerik:GridDropDownColumn>

When I
assign the insert data source it just have all the row from the database which
is not inserted in the grid yet But I would like to retain  my default setting for  EmptyListItemValue="-1" EmptyListItemText="Select
a value"

Just to
make sure that by when we are insert mode the default value is ‘select a value’

I hope you
understand.

Many thanks

Syed

0
Accepted
Princy
Top achievements
Rank 2
answered on 11 Feb 2014, 04:33 AM
Hi Syed,

Please try the following code snippet to have EmptyListItemText displayed during insert.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
  if (e.Item is GridEditableItem && e.Item.OwnerTableView.IsItemInserted && e.Item.IsInEditMode)
    {
        GridEditableItem editedItem = (GridEditableItem)e.Item;
        GridEditManager edit = editedItem.EditManager;
        GridDropDownListColumnEditor editor = edit.GetColumnEditor("ProjectPhaseID") as GridDropDownListColumnEditor;
        DropDownList dropInsert = (DropDownList)editedItem["ProjectPhaseID"].Controls[0];
        DropDownList ddList = editor.DropDownListControl;      
        ddList.AppendDataBoundItems = true;
        dropInsert.DataSourceID = "InsertDataSource";
        dropInsert.DataTextField = "TextField";
        dropInsert.DataValueField = "ValueField";        
    }
}

Thanks,
Princy
0
Syed
Top achievements
Rank 1
answered on 11 Feb 2014, 07:19 PM

Hi Princy
Awesome.
That is working. Much appreciate your help

Cheers
Syed
0
Syed
Top achievements
Rank 1
answered on 21 Feb 2014, 10:15 AM
Hi Princy

I hope you are doing well. I have a quick question for you
but it is related to Telerik Scheduler control. I have a situation in web application, say I have a
project Task which my application user  I would like assign it to Project Manager. This project task has start
and end date. But before assigning the task I would like to give my user a capability
to see what other task the current Project Manager has been working on during that period
by using Scheduler/ resource availability .So user has combo box with all the
Project Manager option and a button to see the scheduler/ resources availability using pop-up.
 On top of that I would like to give him opportunity
to see if any other Project Managera has free time slot during that period.

I know that you have a very good understanding of Telerik components
and you might be able to answer this question quickly.

I will appreciate your help and look forward to hear from
you soon.

Many thanks

Syed

0
Plamen
Telerik team
answered on 26 Feb 2014, 09:12 AM
Hi,

You can achieve similar functionality as it is done in this on-line demo and filter the appointments in the AppointmentDataBound event as for example it is done here.

Hope this information will be helpful.


Regards,
Plamen
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the UI for ASP.NET AJAX, subscribe to the blog feed now.
0
Roger
Top achievements
Rank 1
Veteran
answered on 28 Apr 2014, 01:01 PM
Hi all

I am trying to do something similar. In my case data source for telerik:GridDropDownColumn should be changed depending on edit mode - if in edit mode, then only active items should be available in corresponding RadComboBox, otherwise data source should show all items (cell in grid would be empty, but value should be shown independent from active flag).

As data sources I am using 2 different instances of asp:EntityDataSource:
<asp:EntityDataSource ID="TrainingsAllDataSource"
runat="server"<BR> ConnectionString="name=HSEEmployeeEntities"<BR> DefaultContainerName="HSEEmployeeEntities"
EnableFlattening="false"<BR> EntitySetName="Trainings" OrderBy="it.training
asc"><BR></asp:EntityDataSource><BR><asp:EntityDataSource
ID="TrainingsDropDownDataSource"
runat="server"<BR> ConnectionString="name=HSEEmployeeEntities"<BR> DefaultContainerName="HSEEmployeeEntities"
EnableFlattening="false"<BR> EntitySetName="Trainings"
Where="it.inactive=false" OrderBy="it.training
asc"><BR></asp:EntityDataSource><BR>

TrainingsAllDataSource is the default for corresponding telerik:GridDropDownColumn (DataSourceID set declaratively).

In ItemDataBound event there is following logic: 
protected void MemberTrainingsGrid_ItemDataBound(object sender,
GridItemEventArgs e)<BR>{<BR> if (e.Item is GridEditableItem &&
e.Item.IsInEditMode)<BR> {<BR>  GridEditableItem editedItem =
(GridEditableItem)e.Item;<BR>  RadComboBox ddl =
(RadComboBox)editedItem["GridDropDownColumnTraining"].Controls[0];<BR>  ddl.DataSource
= null;<BR>  ddl.DataSourceID =
"TrainingsDropDownDataSource";<BR> }<BR>}

DataSource is set to null otherwise exception "Both DataSource and DataSourceID are defined" is thrown.

But this logic here does not work either. You get following exception:
"Data properties on data control 'RCB_GridDropDownColumnTraining' such as DataSource, DataSourceID, and DataMember cannot be changed during the databinding phase of the control."

How to achieve this "simple" task?

Thanks,
Roger

0
Eyup
Telerik team
answered on 01 May 2014, 06:14 AM
Hello Roger,

You can use two approaches to achieve the requested functionality:

1. Using GridTemplateColumn:
<telerik:GridTemplateColumn DataField="ShipCountry"
    FilterControlAltText="Filter ShipCountry column" HeaderText="ShipCountry"
    SortExpression="ShipCountry" UniqueName="ShipCountryTemplate">
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("ShipCountry") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadComboBox ID="RadComboBox1" runat="server" DataSourceID="SqlDataSource2"
            DataTextField="ShipCountry" DataValueField="ShipCountry">
        </telerik:RadComboBox>
    </EditItemTemplate>
    <InsertItemTemplate>
        <telerik:RadComboBox ID="RadComboBox2" runat="server" DataSourceID="SqlDataSource3"
            DataTextField="ShipCountry" DataValueField="ShipCountry">
        </telerik:RadComboBox>
    </InsertItemTemplate>
</telerik:GridTemplateColumn>

2. Using GridDropDownColumn:
<telerik:GridDropDownColumn DataField="ShipCountry"
    ListTextField="ShipCountry" ListValueField="ShipCountry"
    FilterControlAltText="Filter ShipCountry column" HeaderText="ShipCountry"
    SortExpression="ShipCountry" UniqueName="ShipCountryDropDown">
</telerik:GridDropDownColumn>
C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item.IsInEditMode)
    {
        GridEditableItem item = e.Item as GridEditableItem;
        RadComboBox combo = item["ShipCountryDropDown"].Controls[0] as RadComboBox;
 
        combo.DataTextField = "ShipCountry";
        combo.DataValueField = "ShipCountry";
        combo.DataSource = (item is IGridInsertItem) ? SqlDataSource3 : SqlDataSource2;
    }
}

Hope this helps. Please give it a try and let me know if it works for you.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Roger
Top achievements
Rank 1
Veteran
answered on 01 May 2014, 12:54 PM
Thanks Eyup

You showed me the right direction, but my scenario is a little bit more complicated.
  • In display mode of row value should be shown independent from existence in RadComboBox.
  • In insert mode of row only "active" values should be shown in RadComboBox.
  • In edit mode of row only "active" values should be shown in RadComboBox, but if current value is "inactive", then it should be added dynamically at first position (otherwise other values in same row could not be updated).

Finally, I could implement this scenario, but there were some pitfalls (I explain below).

<telerik:GridTemplateColumn DataField="TrainingID" HeaderText="Training" UniqueName="GridColumnTraining">
    <ItemTemplate>
        <asp:Label ID="TrainingLabel" runat="server" Text='<%# Eval("Training.TrainingText") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadComboBox ID="TrainingEditComboBox" runat="server" DataSourceID="TrainingsComboBoxDataSource" DataTextField="TrainingText" DataValueField="TrainingID" Width="300" />
    </EditItemTemplate>
    <InsertItemTemplate>
        <telerik:RadComboBox ID="TrainingInsertComboBox" runat="server" DataSourceID="TrainingsComboBoxDataSource" DataTextField="TrainingText" DataValueField="TrainingID" SelectedValue='<%#Bind("TrainingID") %>' Width="300" EmptyMessage="Please select" />
    </InsertItemTemplate>
</telerik:GridTemplateColumn>


In OnNeedDataSource of RadGrid child entity "Training" has to be included in corresponding linq query. This is needed that TrainingText can be displayed in TrainingLabel (ItemTemplate) - bound entity has only foreign key (TrainingID).

The datasource of RadComboBoxes (for insert and edit) only show "active" trainings.

<asp:EntityDataSource ID="TrainingsComboBoxDataSource" runat="server"
    ConnectionString="name=HSEEmployeeEntities"
    DefaultContainerName="HSEEmployeeEntities" EnableFlattening="false"
    EntitySetName="Trainings" Where="it.Inactive=false" OrderBy="it.TrainingText asc">
</asp:EntityDataSource>


As you can see in markup of GridTemplateColumn only for TrainingInsertComboBox field TrainingID has been bound to SelectedValue. This was necessary, because when row is in edit mode and training of current row is "inactive", then there would be an exception (out of range as item is not found in TrainingEditComboBox).

In ItemDataBound event missing training can be added to corresponding RadComboBox and "bound" to property TrainingID of current entity.

protected void MemberTrainingsGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode && !(e.Item is IGridInsertItem))
    {
        // Edit mode
        GridEditableItem editableItem = (GridEditableItem)e.Item;
        RadComboBox cbx = (RadComboBox)editableItem["GridColumnTraining"].FindControl("TrainingEditComboBox") as RadComboBox;
        if (cbx != null)
        {
            var trainingJoin = e.Item.DataItem as MemberTrainingJoin;
            if (trainingJoin != null)
            {
                if (cbx.Items.FindItemByValue(trainingJoin.TrainingID.ToString()) == null)
                {
                    // Training is not available in TrainingEditComboBox => insert missing item
                    cbx.Items.Insert(0, new RadComboBoxItem(trainingJoin.Training.TrainingText, trainingJoin.TrainingID.ToString()));
                }
                // "Bind" training
                cbx.SelectedValue = trainingJoin.TrainingID.ToString();
            }
        }
    }
}


As training is not bound declaratively when row is in edit mode, you have to get value from RadComboBox and set it yourself. This can be done in UpdateCommand event of RadGrid.

editableItem.UpdateValues(memberTrainingJoin);
// Additionally set training
int trainingID = Int32.Parse(cbx.SelectedValue);
memberTrainingJoin.TrainingID = trainingID;


I hope this information helps somebody. If anybody has an easier solution for this scenario, please let me know.

Cheers,
Roger

0
Eyup
Telerik team
answered on 06 May 2014, 06:06 AM
Hello Roger,

I'm glad that you've managed to find a viable solution for your scenario.
Thank you for sharing this approach with the rest of the community.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Syed
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Syed
Top achievements
Rank 1
Plamen
Telerik team
Roger
Top achievements
Rank 1
Veteran
Eyup
Telerik team
Share this question
or