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
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
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
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
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
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
That worked perfect!
Regards, Ida
what to do in insert mode ??please help
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