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

Grid with Datatable

Hi everybody,
 
I would like bind a datatable to an edit grid.
When I click on a cell to edit,  I have an error : "'Id' n'est pas un membre de type 'System.Data.DataRow'"
the column "Id" is my primary key of my table.
I tried "edit.PrimaryKey(new string[] { "Id" }); " but it doesn't work...
 
 
Please could you help me.
 
 
[View]   
@(Html.Syncfusion().Grid<System.Data.DataRow>("translationgrid")
    .Datasource((System.Data.DataTable)ViewData["data"])
    //.ActionMode(Syncfusion.Mvc.Grid.ActionMode.JSON)
    .Caption("test")
    .EnablePaging()
    .EnableSorting()
    .AutoFormat(Skins.Marble)
    .AllowSearching(true)
    .Editing(edit =>
            {
                edit.AllowEdit(true);
                edit.EditMode(GridEditMode.Normal);
                edit.TimeSpan(3000);
                edit.PrimaryKey(new string[] { "Id" });    
            })
    .ToolBar(tools =>
                        {
                            tools.Add(GridToolBarItems.Search)
                                .Add(GridToolBarItems.Edit)
                                .Add(GridToolBarItems.Update)
                                .Add(GridToolBarItems.Cancel)
                                ;
                        })                       
    .Column(cols =>
    {
        cols.Add("Id",false).AllowEditing(false);
        cols.Add("French", false).AllowEditing(false);
        cols.Add("English", false).AllowEditing(false);
    })
    )
[Controller]
        public ActionResult Index()
        {
            var data = GetDataTableLocalization();
            ViewBag.Data = data;
            return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(PagingParams args)
        {
            IEnumerable data = GetDataTableLocalization().AsEnumerable();
            return data.GridJSONActions<DataRow>();
        }
        private DataTable GetDataTableLocalization()
        {
            //Customer customer = new Customer();
            DataTable table = new DataTable();
            table.Columns.Add("Id", typeof(string));
            table.Columns.Add("French", typeof(string));
            table.Columns.Add("English", typeof(string));
            DataColumn[] key = new DataColumn[1];
            key[0] = table.Columns["Id"];
            table.PrimaryKey = key;
            table.Rows.Add("lienAccueil", "Accueil", "Home");
            table.Rows.Add("notifNouveauMessage", "Vous avez un nouveau message", "You have a new message");
            table.Rows.Add("txtIdentifiant", "Identifiant", "login");
            table.Rows.Add("btnAnnulation", "Annuler", "Cancel");
            table.Rows.Add("btnValidation", "Valider", "Save");
            table.Rows.Add("btnModification", "Modifier", "Update");
            return table;
        }

       


3 Replies

BM Bala Murugan A.S Syncfusion Team March 21, 2013 11:27 AM UTC

Hi Jaouen Cécile,

Thanks for using Syncfusion Products.

We regret for the inconvenience caused. We are unable to reproduce your reported issue in our end. We have created a simple sample to demonstrate this and the same can be downloaded from the below link.

sample: JsonDataTable.zip

Could you please replicate your issue in the above sample and send back to us so that we could able to sort out the cause of this issue and provide you a better solutions quickly?.

Please try the above sample and let us know if you have any concerns.

Regards,

Bala Murugan A.S



LB Laszlo Boross March 31, 2014 08:29 AM UTC

Thanks for this example Bala.

I am not having the same issue as Jaouen, but I am having difficulty with getting the Save action working when the Grid is bound to DataTable.

If I have a Grid bound to an Entity object, there is no problem, I run into all sorts of issues however when trying to edit a Grid bound to a data table, my SaveAction is always getting null result.

Is this the correct format?

public ActionResult JSONSave([Bind(Prefix = "updatedRecords")]DataTable orders)

I have also tried with IEnumerable<DataRow> and System.Data.DataRow without any luck

Can we please get an example where edit rows is working when bound to a System.Data.DataTable using ActionMode.JSON?



SI Silambarasan I Syncfusion Team May 21, 2014 04:25 AM UTC

Hi Laszlo,

 

Thanks for using Syncfusion products.

 

We are glad to inform you that your requirement ("I am having difficulty with getting the Save action working when the Grid is bound to DataTable.") can be achieved by creating separate class for getting current record values and we suggest you to use the SetField method to update the current record values to the DataTable. Please refer the below code snippets.

 

CODE SNIPPET:

 

[CSHTML]

 

 @(Html.Syncfusion().Grid<System.Data.DataRow>("OrdersGrid")    

//. . .   

.Editing(edit =>     

{         

edit.AllowEdit(true);         

edit.EditMode(GridEditMode.Normal);         

edit.PrimaryKey(new string[]{"OrderID"});          

})     

.Mappers(map =>       

{         

map.SaveAction("OrderSave");      

})                 

)

 

[CS]

 

public ActionResult OrderSave(OrderClass order)

{           

var dataTabl = GetTable();           

dataTabl.Rows.Find(order.OrderID).SetField("CustomerID", order.CustomerID);           

dataTabl.Rows.Find(order.OrderID).SetField("OrderDate", order.OrderDate);           

dataTabl.Rows.Find(order.OrderID).SetField("ShipCity", order.ShipCity);           

dataTabl.Rows.Find(order.OrderID).SetField("ShipPostalCode", order.ShipPostalCode);           

dataTabl.AcceptChanges();           

return dataTabl.AsEnumerable().GridJSONActions<System.Data.DataRow>();       

}

 

public DataTable GetTable()       

{           

var connection = ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ConnectionString;           

using (var dataAdapter = new SqlDataAdapter("SELECT * from Orders", connection))           

{               

var dataTable = new DataTable();               

dataAdapter.Fill(dataTable);               

DataColumn[] key = new DataColumn[1];               

key[0] = dataTable.Columns["OrderID"];              

dataTable.PrimaryKey = key;               

return dataTable;           

}       

}

 

For your convenience, we have prepared a simple sample to demonstrate this and the same can be downloaded from the below link,

 

Link: SampleProject.zip

 

Please let us know if you need any further assistance.

 

Regards,

 

Silambarasan I


Loader.
Live Chat Icon For mobile
Up arrow icon