We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Datasource of Bd in dropdownlist in GRis

Dear,
How can you fill database data in a dropdownlist control is part of a grid for dialog editing mode to add, edit and delete?

Greetings.

9 Replies

JK Jayaprakash Kamaraj Syncfusion Team June 3, 2016 10:17 AM UTC

Hi Carlos, 

Thanks for contacting Syncfusion support. 
 
We need to give EditType as Dropdown with column property and also bind DataSource to column using DataSource property of columns in Grid. Dropdown DataSource must have text and value pair. Please refer to the below code example and sample.   
 
@(Html.EJ().Grid<SyncfusionMvcApplication89.OrdersView>("FlatGrid") 
.. 
        .Columns(col => 
        { 
.. 
            col.Field("ShipCountry").HeaderText("Ship Country").EditType(EditingType.Dropdown).DataSource((List<object>)ViewBag.dropdown).Width(85).Add(); 
        })) 
 
GridFeatures.cs 
        public ActionResult GridFeatures() 
        { 
             
            ViewBag.dropdown = Get(); // here will get dropdown datasource 
            return View(); 
        } 
        public object Get() 
        { 
            var country = new NorthwindDataContext().EmployeeViews.Select(s => s.Country).Distinct().ToList(); // here we will get country data in database 
            var ShipCountry = new List<object>(); 
            foreach (var id in country) 
            { 
                ShipCountry.Add(new { value = id, text = id }); // here we will bind text and value pair 
            } 
            return ShipCountry; 
        } 
 
 
If we have misunderstood your requirement, please share more details about requirement to validate the issue at our end.  
 
Regards, 
Jayaprakash K. 



CP Carlos Palomino June 4, 2016 02:37 AM UTC

Thanks for the reply.
Now as you can display text instead of the value of a dropdownlist on the grid.

Greetings.

Attachment: Imagen_95f1ae3a.zip


JK Jayaprakash Kamaraj Syncfusion Team June 6, 2016 12:38 PM UTC

Hi Carlos, 

We can display text instead of the value of a dropdownlist on the grid. Please refer to the following code example and sample. 
 
public ActionResult GridFeatures() 
        { 
             
            ViewBag.dropdown = Get(); // here will get dropdown datasource 
            return View(); 
        } 
public object Get() 
        { 
            var employeeid = new NorthwindDataContext().Employees.ToList(); // here we will get Employee table data in database 
            var EmployeeID = new List<object>(); 
            foreach (var id in employeeid) 
            { 
 
                EmployeeID.Add(new { value = id.EmployeeID, text = id.FirstName }); // here we will bind text and value pair 
                                                                                     // value as EmployeeID and text as FirstName 
            } 
            return EmployeeID; 
        } 

If we have misunderstood your requirement, please share more details about requirement to validate the issue at our end.  


Regards, 

Jayaprakash K. 



CP Carlos Palomino June 6, 2016 02:38 PM UTC

Dear,
With the example sent, yet the description is shown.
How do to hide a column on the main screen grid, but if appears when you add or edit values?

Attached images.

Greetings.

Attachment: Images_a8d40cf4.zip


JK Jayaprakash Kamaraj Syncfusion Team June 7, 2016 01:15 PM UTC

Hi Carlos, 

Query1: With the example sent, yet the description is shown. 
 
Dropdown DataSource must have text and value pair. Text will be shown in dropdown and corresponding value will be saved. Please refer to the below screenshot. 
 
 
 
If you want to show id in dropdown we suggest to give idIdRol in text(text = id.iIdRol). 
 
If we have misunderstood your requirement, please share more details about requirement to validate the issue at our end.   
 
Query2: How do to hide a column on the main screen grid 

In Grid, we have visible property to show or hide columns in grid. Please refer to the below Help document and code example. 

        .Columns(col => 
        { 
            col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add(); 
            col.Field("CustomerID").HeaderText("Customer ID").Width(80).Visible(false).Add(); 
            col.Field("EmployeeID").HeaderText("Employee ID").EditType(EditingType.Dropdown).DataSource((List<object>)ViewBag.dropdown).TextAlign(TextAlign.Right).Width(75).Add(); 
            col.Field("Freight").HeaderText("Freight").TextAlign(TextAlign.Right).Width(75).Format("{0:C}").Add(); 
            col.Field("ShipCountry").HeaderText("Ship Country").Width(85).Add(); 
        })) 

Regards, 

Jayaprakash K. 



CP Carlos Palomino June 7, 2016 02:17 PM UTC

Hello such,
What you want is to display the description on the main screen grid and when the dialog to edit or add.

With regard to the other question, you want to hide a column on the main screen grid, but if you show the dialog to edit or add.

Thank you.

Greetings.


JK Jayaprakash Kamaraj Syncfusion Team June 8, 2016 01:52 PM UTC

Hi Carlos, 

We deeply regret for the inconvenience. 

We have achieved your requirement using DialogTemplate feature and ActionComplete event. While using this feature, you can edit the fields that are not bound to grid columns. To edit the records using Dialog template form, set EditMode as ‘DialogTemplate’ and specify the template id to DialogEditorTemplateID property of EditSettings. While using template, you can change the elements that are defined in the Template, to appropriate JS controls based on the column type. This can be achieved by using ActionComplete event of grid. Please refer to the below Help document, code example and sample. 



@(Html.EJ().Grid<SyncfusionMvcApplication89.OrdersView>("FlatGrid") 
.. 
                  .ClientSideEvents(eve => { eve.ActionComplete("complete"); }) 
                .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing().EditMode(EditMode.DialogTemplate).DialogEditorTemplateID("#template"); }) 
        .Columns(col => 
        { 
           col.Field("EmployeeID").HeaderText("Employee ID").EditType(EditingType.Dropdown).Visible(false).TextAlign(TextAlign.Right).Width(75).Add();// this column will be hidden initial rendering of the grid 
        })) 
<script type="text/template" id="template"> 
    <b>Order Details</b> 
    <table cellspacing="10"> 
.. 
        <tr> 
            <td style="text-align: right;"> 
                Employee ID 
            </td> 
            <td style="text-align: left"> 
                <select id="EmployeeID" name="EmployeeID"> 
                </select> 
            </td> 
            .. 
 
   </table> 
</script> 
<script type="text/javascript"> 
 
    function complete(args) { 
        var data =   @(Html.Raw(Json.Encode(ViewBag.dropdown))); 
            if ((args.requestType == "beginedit" || args.requestType == "add") && args.model.editSettings.editMode == "dialogtemplate") { 
                $("#EmployeeID").ejDropDownList({dataSource: data, width: '116px' }); // here we will bind datasource for dropdown column 
.. 
                } 
            } 
        } 
</script> 



Regards, 

Jayaprakash K. 



CP Carlos Palomino replied to Jayaprakash Kamaraj June 8, 2016 09:01 PM UTC

Hi Carlos, 

We deeply regret for the inconvenience. 

We have achieved your requirement using DialogTemplate feature and ActionComplete event. While using this feature, you can edit the fields that are not bound to grid columns. To edit the records using Dialog template form, set EditMode as ‘DialogTemplate’ and specify the template id to DialogEditorTemplateID property of EditSettings. While using template, you can change the elements that are defined in the Template, to appropriate JS controls based on the column type. This can be achieved by using ActionComplete event of grid. Please refer to the below Help document, code example and sample. 



@(Html.EJ().Grid<SyncfusionMvcApplication89.OrdersView>("FlatGrid") 
.. 
                  .ClientSideEvents(eve => { eve.ActionComplete("complete"); }) 
                .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing().EditMode(EditMode.DialogTemplate).DialogEditorTemplateID("#template"); }) 
        .Columns(col => 
        { 
           col.Field("EmployeeID").HeaderText("Employee ID").EditType(EditingType.Dropdown).Visible(false).TextAlign(TextAlign.Right).Width(75).Add();// this column will be hidden initial rendering of the grid 
        })) 
<script type="text/template" id="template"> 
    <b>Order Details</b> 
    <table cellspacing="10"> 
.. 
        <tr> 
            <td style="text-align: right;"> 
                Employee ID 
            </td> 
            <td style="text-align: left"> 
                <select id="EmployeeID" name="EmployeeID"> 
                </select> 
            </td> 
            .. 
 
   </table> 
</script> 
<script type="text/javascript"> 
 
    function complete(args) { 
        var data =   @(Html.Raw(Json.Encode(ViewBag.dropdown))); 
            if ((args.requestType == "beginedit" || args.requestType == "add") && args.model.editSettings.editMode == "dialogtemplate") { 
                $("#EmployeeID").ejDropDownList({dataSource: data, width: '116px' }); // here we will bind datasource for dropdown column 
.. 
                } 
            } 
        } 
</script> 



Regards, 

Jayaprakash K. 


Very grateful for answers.


JK Jayaprakash Kamaraj Syncfusion Team June 9, 2016 04:28 AM UTC

Hi Carlos,    
 
Thanks for the update. 
 
Get back to us if you need any further assistance. 
 
Regards,   
 
Jayaprakash K. 


Loader.
Live Chat Icon For mobile
Up arrow icon