GridDropDownColumn datasource to point to tableadapter dataset

9 posts, 0 answers
  1. Ida
    Ida avatar
    55 posts
    Member since:
    Mar 2013

    Posted 15 Oct 2013 Link to this post

    Hi,

    I have a dropdown list set up as such:

    <

     

     

    telerik:GridDropDownColumn DataField="GroundGear" DataSourceID="getGroundGears" HeaderText="Ground Gear" ListTextField="GroundGearName" ListValueField="GroundGearCode" UniqueName="GroundGear" ColumnEditorID="GroundGear" Reorderable="False" Resizable="False" Visible="false">

     



    With the datasource embedded in the the .aspx page as such:

    <

     

     

    asp:SqlDataSource ID="getGroundGears" runat="server" ConnectionString="<%$ ConnectionStrings:FisheriesConnectionString %>"

     

     

     

    ProviderName="System.Data.SqlClient" SelectCommand="SELECT code as [GroundGearCode], name as [GroundGearName] FROM DisGroundGear">

     

     

     

    </asp:SqlDataSource>

     



    Though I was wondering how I can change the datasource to point to a tableadapter/dataset, rather than having the SQL code in the same page as the grid?

    Thanks, Ida
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 15 Oct 2013 Link to this post

    HI Ida,

    I guess you want to bind a GridDropDownColumn from code-behind,Please try the following code snippet.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" OnItemDataBound="RadGrid1_ItemDataBound"
        AutoGenerateEditColumn="true" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource">
        <MasterTableView DataKeyNames="OrderID">
            <Columns>
                <telerik:GridDropDownColumn HeaderText="ShipCity" UniqueName="ShipCity">
                </telerik:GridDropDownColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    public static String ConnString = ConfigurationManager.ConnectionStrings["Northwind_newConnectionString3"].ConnectionString;
      
    public static DataTable GetRelatedRecords(string query)
    {
        SqlConnection conn = new SqlConnection(ConnString);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(query, conn);
        DataTable myDataTable = new DataTable();
        conn.Open();
        try
        {
            adapter.Fill(myDataTable);
        }
        finally
        {
            conn.Close();
        }
        return myDataTable;
    }
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = GetRelatedRecords("SELECT * FROM Orders");
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        //Populate in EditMode
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem edititem = (GridEditableItem)e.Item;
            GridEditManager editMan = edititem.EditManager;
            GridDropDownListColumnEditor editor = (GridDropDownListColumnEditor)(editMan.GetColumnEditor("ShipCity"));
            RadComboBox rComboBox = editor.ComboBoxControl;
            string s = DataBinder.Eval(edititem.DataItem, "ShipCity").ToString();
            DataTable table = GetRelatedRecords("SELECT ShipCity FROM [Orders]");
            editor.DataSource = table;
            rComboBox.DataSource = table;
            rComboBox.DataTextField = "ShipCity";
            rComboBox.DataValueField = "ShipCity";
            rComboBox.DataBind();
            rComboBox.SelectedValue = s;
        }
        //Populate in ViewMode
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            DataRowView row = (DataRowView)e.Item.DataItem;
            item["ShipCity"].Text = row["ShipCity"].ToString();
        }
    }

    Thanks,
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Ida
    Ida avatar
    55 posts
    Member since:
    Mar 2013

    Posted 15 Oct 2013 Link to this post

    Yes Princy. I wish to bind a GridDropDownColumn from code-behind, though in VB.Net.

    I have a tableadapter set up pointing to a SQL stored procedure that extracts the list I require.

    Is that possible to do please?

    Thanks, Ida
  5. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1798 posts

    Posted 18 Oct 2013 Link to this post

    Hello Ida,

    Could you please refer to the Princy's post where he shows how to add DataTable to GridDropDownColumn:
    Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
        If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
            Dim edititem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
            Dim editMan As GridEditManager = edititem.EditManager
            Dim editor As GridDropDownListColumnEditor = DirectCast(editMan.GetColumnEditor("ShipCity"), GridDropDownListColumnEditor)
            Dim rComboBox As RadComboBox = editor.ComboBoxControl
                ' here you assign the DataTable
            editor.DataSource = ... 
            ...

    Hope that helps.

     

    Regards,
    Konstantin Dikov
    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 RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  6. Ida
    Ida avatar
    55 posts
    Member since:
    Mar 2013

    Posted 18 Oct 2013 Link to this post

    Hi,

    I must be missing something, as the dropdown called GroundGear doesn't seem to be databinding.

    Here is the RadGrid, with just the dropdown:

     <telerik:RadGrid ID="RDHauls" runat="server"  Width="500px"
              AutoGenerateEditColumn="True" GridLines="Horizontal"
              AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#ecf3f4"
              BorderColor="#28899A" EditItemStyle-BackColor="#009999"
              EditItemStyle-Font-Bold="true" AllowPaging="True" CellSpacing="0" OnItemDataBound="RDHauls_ItemDataBound" OnNeedDataSource="RDHauls_NeedDataSource">
               <AlternatingItemStyle BackColor="#ECF3F4"></AlternatingItemStyle>
                     <MasterTableView EnableViewState="false">
                       <Columns>
    <telerik:GridDropDownColumn DataField="GroundGear" HeaderText="Ground Gear" ListTextField="Name" ListValueField="Code" UniqueName="GroundGear" ColumnEditorID="GroundGear" Reorderable="False" Visible="false"></telerik:GridDropDownColumn> 
      </Columns>
                      </MasterTableView>
                  </telerik:RadGrid>

    And here is the behind-code for binding that one dropdown:

    Protected Sub RDHauls_ItemDataBound(sender As Object, e As GridItemEventArgs) 'Handles RDHauls.ItemDataBound
               
                ' Build up the Ground Gears dropdown
                If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
                    Dim edititem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
                    Dim editMan As GridEditManager = edititem.EditManager
                    Dim editor As GridDropDownListColumnEditor = DirectCast(editMan.GetColumnEditor("GroundGear"), GridDropDownListColumnEditor)
                    Dim dsGroundGears As New DSGroundGearsTableAdapters.DisGroundGearAllTableAdapter
                    editor.DataSource = dsGroundGears.GetDisGroundGearAll
                End If

            End Sub


    Can you see anything that I am missing please?

    Thank you, Ida
  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Oct 2013 Link to this post

    Hi Ida,

    Please try the following code snippet to bind the dropdown list in edit mode.

    VB:
    Protected Sub RDHauls_ItemDataBound(sender As Object, e As GridItemEventArgs)
        If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
            Dim edititem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
            Dim editMan As GridEditManager = edititem.EditManager
            Dim editor As GridDropDownListColumnEditor = DirectCast(editMan.GetColumnEditor("GroundGear"), GridDropDownListColumnEditor)
            Dim dsGroundGears As New DSGroundGearsTableAdapters.DisGroundGearAllTableAdapter()
            editor.DataSource = dsGroundGears.GetDisGroundGearAll
            Dim value As String = DataBinder.Eval(edititem.DataItem, "GroundGear").ToString()
            Dim rComboBox As RadComboBox = editor.ComboBoxControl
            rComboBox.DataSource = dsGroundGears.GetDisGroundGearAll
            rComboBox.DataTextField = "GroundGear"
            rComboBox.DataValueField = "GroundGear"
            rComboBox.DataBind()
            rComboBox.SelectedValue = value
        End If
    End Sub

    Thanks,
    Princy
  8. Ida
    Ida avatar
    55 posts
    Member since:
    Mar 2013

    Posted 18 Oct 2013 Link to this post

    Many thanks Princy!

    That worked perfect!

    Regards, Ida
  9. Tarang
    Tarang avatar
    2 posts
    Member since:
    Oct 2016

    Posted 05 Oct in reply to Princy Link to this post

    what to do in insert mode ??please help 

  10. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1798 posts

    Posted 10 Oct Link to this post

    Hi Tarang,

    Please refer to the following help topic for accessing controls in edit/insert modes of RadGrid items:
    Hope this helps.


    Regards,
    Konstantin Dikov
    Telerik by Progress
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017