How to bind SQL table data to dropdown column in ASP.NET MVC Grid?
In some cases, there is a need for the user to bind SQL table data to a dropdown in the ASP.NET MVC 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.
Conclusion
I hope you enjoyed learning about how to bind SQL table data to dropdown column in .NET MVC Grid.
You can refer to our ASP.NET MVC Grid
feature tour page to know about its other groundbreaking feature
representations and documentation, and how to quickly get started for configuration
specifications.
You can also explore our ASP.NET
MVC Grid example to understand how to create and manipulate data.
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!