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

Bind dropdown selected value to a data source

3 Answers 129 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Felice
Top achievements
Rank 1
Felice asked on 06 Sep 2013, 08:06 AM
Hi,
I have added a drop down column in my grid and I managed to get the values that populate it from SqlDataSource2 as following:
<telerik:GridDropDownColumn DataSourceID="SqlDataSource2" ListTextField="Tipo" ListValueField="Tipo" UniqueName="ddl1" HeaderText="A-Tipo" DataField="Tipo"  >
Now what I would like to achieve is to post the selected value, when in edit mode, to a field in a different table that is  DataSource1 "Tipo"
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Test1_managDocDBConnectionString %>" DeleteCommand="DELETE FROM [TableCasaFi] WHERE [TransazioneID] = @TransazioneID" InsertCommand="INSERT INTO [TableCasaFi] ([Tipo], [Scadenza], [InizioPeriodo], [FinePeriodo], [Note], [Pagata]) VALUES (@Tipo, @Scadenza, @InizioPeriodo, @FinePeriodo, @Note, @Pagata)" SelectCommand="SELECT * FROM [TableCasaFi]" UpdateCommand="UPDATE [TableCasaFi] SET [Tipo] = @Tipo, [Scadenza] = @Scadenza, [InizioPeriodo] = @InizioPeriodo, [FinePeriodo] = @FinePeriodo, [Note] = @Note, [Pagata] = @Pagata WHERE [TransazioneID] = @TransazioneID">
        <DeleteParameters>
            <asp:Parameter Name="TransazioneID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Tipo" Type="String" />
            <asp:Parameter DbType="Date" Name="Scadenza" />
            <asp:Parameter DbType="Date" Name="InizioPeriodo" />
            <asp:Parameter DbType="Date" Name="FinePeriodo" />
            <asp:Parameter Name="Note" Type="String" />
            <asp:Parameter Name="Pagata" Type="Boolean" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Tipo" Type="String" />
            <asp:Parameter DbType="Date" Name="Scadenza" />
            <asp:Parameter DbType="Date" Name="InizioPeriodo" />
            <asp:Parameter DbType="Date" Name="FinePeriodo" />
            <asp:Parameter Name="Note" Type="String" />
            <asp:Parameter Name="Pagata" Type="Boolean" />
            <asp:Parameter Name="TransazioneID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
So the user can select the value from the drop down list and save it in the record.
How can I achieve that? 

3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 06 Sep 2013, 12:25 PM
Hi Felice,

I guess you take data into GridDropDownColumn from DataSource2,and you have other columns which are populated with DataSource1.In the edit mode you want to get the value for one of the column from the GridDropDownColumn value.Please try the below code snippet,which shows how to get values from dropdown to textbox in edit.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="true" AutoGenerateColumns="false"     AllowAutomaticUpdates="true" DataSourceID="SqlDataSource1" AutoGenerateEditColumn="true"    OnUpdateCommand="RadGrid1_UpdateCommand">
    <MasterTableView DataKeyNames="OrderID">
        <Columns>
            <telerik:GridBoundColumn UniqueName="ID" DataField="CustomerID" HeaderText="CustomerID" />
            <telerik:GridDropDownColumn DataSourceID="SqlDataSource2" ListTextField="CustomerID"
                ListValueField="CustomerID" UniqueName="CustomerID" HeaderText="CustomerID" DataField="CustomerID">
            </telerik:GridDropDownColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind_newConnectionString3 %>"
    SelectCommand="SELECT * FROM [Orders]" UpdateCommand="UPDATE [Orders] SET [CustomerID] = @CustomerID WHERE [OrderID] = @OrderID">
    <UpdateParameters>
        <asp:Parameter Name="CustomerID" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind_newConnectionString3 %>"
    SelectCommand="SELECT DISTINCT [CustomerID] FROM [Customers]"></asp:SqlDataSource>

C#:
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem edit = (GridEditableItem)e.Item;
            string OrderID = edit.GetDataKeyValue("OrderID").ToString();
            RadComboBox drop = (RadComboBox)edit["CustomerID"].Controls[0];
            string value = drop.Text;
            TextBox txt = (TextBox)edit["ID"].Controls[0];
            txt.Text = value;          
        }
    }

Thanks,
Princy
0
Jayesh Goyani
Top achievements
Rank 2
answered on 06 Sep 2013, 12:41 PM
Hello,

As per your code i have tried with below code snippet and it works perfectly.

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
    </telerik:RadAjaxLoadingPanel>
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1"
        AllowAutomaticInserts="true" AllowAutomaticUpdates="true" AllowAutomaticDeletes="true">
        <MasterTableView CommandItemDisplay="Top" DataSourceID="SqlDataSource1" DataKeyNames="TransazioneID">
            <Columns>
                <telerik:GridBoundColumn UniqueName="TransazioneID" DataField="TransazioneID" HeaderText="TransazioneID"
                    ReadOnly="true">
                </telerik:GridBoundColumn>
                <telerik:GridDropDownColumn DataSourceID="SqlDataSource2" ListTextField="Tipo" ListValueField="Tipo"
                    UniqueName="Tipo" HeaderText="A-Tipo" DataField="Tipo">
                </telerik:GridDropDownColumn>
                <telerik:GridDateTimeColumn UniqueName="Scadenza" DataField="Scadenza" HeaderText="Scadenza"
                    PickerType="DatePicker">
                </telerik:GridDateTimeColumn>
                <telerik:GridDateTimeColumn UniqueName="InizioPeriodo" DataField="InizioPeriodo"
                    HeaderText="InizioPeriodo" PickerType="DatePicker">
                </telerik:GridDateTimeColumn>
                <telerik:GridDateTimeColumn UniqueName="FinePeriodo" DataField="FinePeriodo" HeaderText="FinePeriodo"
                    PickerType="DatePicker">
                </telerik:GridDateTimeColumn>
                <telerik:GridBoundColumn DataField="Note" UniqueName="Note" HeaderText="Note">
                </telerik:GridBoundColumn>
                <telerik:GridCheckBoxColumn DataField="Pagata" UniqueName="Pagata" HeaderText="Pagata">
                </telerik:GridCheckBoxColumn>
                <telerik:GridEditCommandColumn>
                </telerik:GridEditCommandColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:JayeshTestConnectionString %>"
        DeleteCommand="DELETE FROM [TableCasaFi] WHERE [TransazioneID] = @TransazioneID"
        InsertCommand="INSERT INTO [TableCasaFi] ([Tipo], [Scadenza], [InizioPeriodo], [Note], [FinePeriodo], [Pagata]) VALUES (@Tipo, @Scadenza, @InizioPeriodo, @Note, @FinePeriodo, @Pagata)"
        SelectCommand="SELECT [TransazioneID], [Tipo], [Scadenza], [InizioPeriodo], [Note], [FinePeriodo], [Pagata] FROM [TableCasaFi]"
        UpdateCommand="UPDATE [TableCasaFi] SET [Tipo] = @Tipo, [Scadenza] = @Scadenza, [InizioPeriodo] = @InizioPeriodo, [Note] = @Note, [FinePeriodo] = @FinePeriodo, [Pagata] = @Pagata WHERE [TransazioneID] = @TransazioneID">
        <DeleteParameters>
            <asp:Parameter Name="TransazioneID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Tipo" Type="String" />
            <asp:Parameter DbType="Date" Name="Scadenza" />
            <asp:Parameter DbType="Date" Name="InizioPeriodo" />
            <asp:Parameter Name="Note" Type="String" />
            <asp:Parameter DbType="Date" Name="FinePeriodo" />
            <asp:Parameter Name="Pagata" Type="Boolean" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Tipo" Type="String" />
            <asp:Parameter DbType="Date" Name="Scadenza" />
            <asp:Parameter DbType="Date" Name="InizioPeriodo" />
            <asp:Parameter Name="Note" Type="String" />
            <asp:Parameter DbType="Date" Name="FinePeriodo" />
            <asp:Parameter Name="Pagata" Type="Boolean" />
            <asp:Parameter Name="TransazioneID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:JayeshTestConnectionString %>"
        SelectCommand="SELECT [Tipo] FROM [TipoTable]"></asp:SqlDataSource>

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TipoTable](
    [Tipo] [nvarchar](50) NOT NULL
) ON [PRIMARY]
GO
INSERT [dbo].[TipoTable] ([Tipo]) VALUES (N'abc')
INSERT [dbo].[TipoTable] ([Tipo]) VALUES (N'xyz')
/****** Object:  Table [dbo].[TableCasaFi]    Script Date: 09/06/2013 18:11:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TableCasaFi](
    [TransazioneID] [int] IDENTITY(1,1) NOT NULL,
    [Tipo] [nvarchar](50) NULL,
    [Scadenza] [date] NULL,
    [InizioPeriodo] [date] NULL,
    [FinePeriodo] [date] NULL,
    [Note] [nvarchar](50) NULL,
    [Pagata] [bit] NULL,
 CONSTRAINT [PK_TableCasaFi] PRIMARY KEY CLUSTERED
(
    [TransazioneID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[TableCasaFi] ON
INSERT [dbo].[TableCasaFi] ([TransazioneID], [Tipo], [Scadenza], [InizioPeriodo], [FinePeriodo], [Note], [Pagata]) VALUES (1, N'abc', CAST(0x80360B00 AS Date), CAST(0x80360B00 AS Date), CAST(0x80360B00 AS Date), N'Test', 1)
INSERT [dbo].[TableCasaFi] ([TransazioneID], [Tipo], [Scadenza], [InizioPeriodo], [FinePeriodo], [Note], [Pagata]) VALUES (2, N'abc', CAST(0x87370B00 AS Date), CAST(0x88370B00 AS Date), CAST(0x89370B00 AS Date), N'Jayesh', 1)
SET IDENTITY_INSERT [dbo].[TableCasaFi] OFF

Let me know if any concern.

Thanks,
Jayesh Goyani
0
Felice
Top achievements
Rank 1
answered on 06 Sep 2013, 12:48 PM
Thank you a lot Princy.
It worked first time no problems.
I am starting to appreciate these controls.

Thanks again for your help.

Felice
Tags
Grid
Asked by
Felice
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Felice
Top achievements
Rank 1
Share this question
or