How to bind SQL table data to dropdown column in .NET Grid?
In some cases, there is a need for the user to bind SQL table data to a dropdown in the grid. But the column will accept only text/value pair as input.
Solution
We suggest to convert the SQL Table result to the list of text/value pair. Then this result is set to be assigned to the corresponding dropdown column.
Razor
@(Html.EJ().Grid<OrdersView>("DetailGrid") .Datasource(ds=>ds .URL("/Home/DataSource") .Adaptor(AdaptorType.UrlAdaptor)) .AllowPaging() .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing() .EditMode(EditMode.Dialog); }) .ToolbarSettings(toolbar => { toolbar.ShowToolbar().ToolbarItems(items => { items.AddTool(ToolBarItems.Add); items.AddTool(ToolBarItems.Edit); items.AddTool(ToolBarItems.Delete); items.AddTool(ToolBarItems.Update); items.AddTool(ToolBarItems.Cancel); }); }) .Columns(col => { col.Field("OrderID").IsPrimaryKey(true).HeaderText("Order ID").Add(); col.Field("EmployeeID").DataSource((IEnumerable<object>)ViewBag.dropObj) .EditType(EditingType.Dropdown).HeaderText("Employee ID").Add(); col.Field("CustomerID").HeaderText("Customer ID").Add(); col.Field("Freight").Format("{0:C}").Add(); }) )
Controller
public ActionResult Index() { SqlConnection myConnection = new SqlConnection(ConfigurationManager .ConnectionStrings["NORTHWNDConnectionString"].ToString()); DataTable dt = new DataTable("Employee"); SqlCommand cmd = new SqlCommand(); cmd.Connection = myConnection; cmd.CommandText = "select * from Employees"; cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; if (myConnection.State == ConnectionState.Closed) { myConnection.Open(); } da.Fill(dt); List<object> dropObj = new List<object>(); using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { //Convert them to text/value pair dropObj.Add(new { text = dr.GetValue(0).ToString(), value = dr.GetValue(0).ToString() }); } } ViewBag.dropObj = dropObj; return View(); }
Aspx
<ej:Grid ID="DetailGrid" runat="server" AllowPaging="True"> <ToolbarSettings ShowToolbar="True" ToolbarItems="add,edit,delete,update,cancel"></ToolbarSettings> <EditSettings AllowEditing="True" AllowAdding="True" AllowDeleting="True" EditMode="Dialog"></EditSettings> <Columns> <ej:Column Field="OrderID" HeaderText="Order ID" IsPrimaryKey="True" /> <ej:Column Field="EmployeeID" HeaderText="Employee ID" EditType="Dropdown" TextAlign="Right" Width="110" /> <ej:Column Field="CustomerID" HeaderText="Customer ID" /> <ej:Column Field="Freight" HeaderText="Freight" Format="{0:C}" /> </ej:Grid>
C#
protected void Page_Load(object sender, EventArgs e) { SqlConnection myConnection = new SqlConnection(ConfigurationManager .ConnectionStrings["NORTHWNDConnectionString"].ToString()); DataTable dt = new DataTable("Orders"); SqlCommand cmd = new SqlCommand(); cmd.Connection = myConnection; cmd.CommandText = "select * from Employees"; cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; if (myConnection.State == ConnectionState.Closed) { myConnection.Open(); } da.Fill(dt); List<object> dropObj = new List<object>(); //read data from the SQL table using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { //Convert Table rows to text/value pair dropObj.Add(new { text = dr.GetValue(0).ToString(), value = dr.GetValue(0) }); } } this.DetailGrid.Columns[2].DataSource = dropObj; Session["SqlDataSource"] = dt; BindDataSource(); }
Result
The following screenshot shows the Dialog EditMode with SQL Table bound to dropdown column.
Figure: SQL Table bound to dropdown column.
Note:
A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.
The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.
You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!