Category / Section
How to bind data from ObjectDataSource control to Grid DropdownEdit Column?
1 min read
To bind the ObjectDataSource to the Grid columns, SelectMethod of the ObjectDataSource can be used. DataObjectMethod of the ObjectDataSource is defined and later in the required place, its data can be utilized. In the Page_Load method, Columns DataSource can be assigned with the ObjectDataSource. This has been discussed in the following steps.
Render the Grid and ObjectDataSource:
<ej:Grid ID="Grid" runat="server" AllowPaging="true" > <EditSettings AllowEditing="True" AllowAdding="True" AllowDeleting="True"></EditSettings> <ToolbarSettings ShowToolbar="True" ToolbarItems="add,edit,delete,update,cancel"></ToolbarSettings> <Columns> <ej:Column Field="OrderID" IsPrimaryKey ="true" TextAlign ="Right" HeaderText="Order ID" /> <ej:Column Field="EmployeeID" HeaderText="Employee ID" EditType="Dropdown" TextAlign ="Right" > <DropDownEditOptions ></DropDownEditOptions> </ej:Column> <ej:Column Field="Freight" HeaderText="Freight" /> <ej:Column Field="CustomerID" HeaderText="Customer ID" /> <ej:Column Field="ShipCity" HeaderText="ShipCity" /> </Columns> </ej:Grid> <asp:ObjectDataSource ID="ObjectData" runat="server" TypeName="Employees" SelectMethod="GetRecords"> </asp:ObjectDataSource>
Assign DataSource to the Grid and Required Columns:
namespace sqlbinding { public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { Grid.DataSource = order; Grid.DataBind(); this.Grid.Columns[1].DataSource = (List<object>)this.ObjectData.Select(); } } }
Under App_Data folder, place the DataObjectMethod definition with the TypeName property of the ObjectDatasource.
public class Employees { [DataObjectMethod(DataObjectMethodType.Select)] public List<object> GetRecords() { List<object> drpObj = new List<object>(); for (int row = 1; row < 10; row++) { drpObj.Add(new { text = row.ToString(), value = row }); } return drpObj; } }
Figure: Grid with Dropdown bound from ObjectDataSource.