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

GridDropDownColumn datasource to point to tableadapter dataset

8 Answers 91 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ida
Top achievements
Rank 1
Ida asked on 15 Oct 2013, 09:12 AM
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

8 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 15 Oct 2013, 10:33 AM
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
0
Ida
Top achievements
Rank 1
answered on 15 Oct 2013, 10:55 AM
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
0
Konstantin Dikov
Telerik team
answered on 18 Oct 2013, 06:49 AM
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.
0
Ida
Top achievements
Rank 1
answered on 18 Oct 2013, 08:56 AM
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
0
Princy
Top achievements
Rank 1
answered on 18 Oct 2013, 09:18 AM
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
0
Ida
Top achievements
Rank 1
answered on 18 Oct 2013, 09:22 AM
Many thanks Princy!

That worked perfect!

Regards, Ida
0
Tarang
Top achievements
Rank 1
answered on 05 Oct 2016, 06:51 AM

what to do in insert mode ??please help 

0
Konstantin Dikov
Telerik team
answered on 10 Oct 2016, 06:06 AM
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.
Tags
Grid
Asked by
Ida
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Ida
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Tarang
Top achievements
Rank 1
Share this question
or