Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
90
Dynamic column iggrid
posted

I want to create a grid with dynamic columns that comes from database table specific column values. Column values depends on difference table column values.

And successfully create dynamic column grid but faced an error at run time in controller when add new records using iggrid.

View:

@model MessManagement.Models.GridVirtualizationModel
@using Infragistics.Web.Mvc;
@using MessManagement.Models;
@using System.Web;

@{
    ViewBag.Title = "MealInfo";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@*<script src="../../Scripts/IG/ig.ui.min.js" type="text/javascript"></script>
<script  type="text/javascript" src='@Url.Content("~/Scripts/Samples/combined/min/ig.ui.min.js")' />
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>*@

<script type="text/javascript">
          $(function () {
              $('#grdMealInfo').live('iggridupdatingrowadded', function () {
                  var ok = confirm("Are you sure to add new record.");
                  if (ok) {
                      $('#grdMealInfo').igGrid("saveChanges")
                  }
              });
              $('#grdMealInfo').live('iggridupdatingrowdeleted', function (event, ui) {
                  var ok = confirm('Do you want to delete this row');
                  if (ok) {
                      $("#grdMealInfo").igGrid("saveChanges");
                  }
              });
          });
</script>

<h2>MealInfo</h2>

<div>
     @Html.Infragistics().Grid("grdMealInfo", Model.GridVirtualization)
</div>

 

Model:

 

public class GridVirtualizationModel
    {
        public GridVirtualizationModel()
        {
            GridVirtualization = new GridModel();
        }

        public GridModel GridVirtualization { get; set; }
    }

Dynamic Property Model:

public class MealEntry : DynamicObject
    {
        // The inner dictionary.
        Dictionary<string, object> properties = new Dictionary<string, object>();
 
        // This property returns the number of elements
        // in the inner dictionary.
        public int Count
        {
            get
            {
                return properties.Count;
            }
        }

        // If you try to get a value of a property
        // not defined in the class, this method is called.
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {         
            //string name = binder.Name.ToLower();
            string name = binder.Name;
            return properties.TryGetValue(name, out result);
        }

        // If you try to set a value of a property that is
        // not defined in the class, this method is called.
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            // Converting the property name to lowercase
            // so that property names become case-insensitive.
            properties[binder.Name.ToLower()] = value;

            // You can always add a value to a dictionary,
            // so this method always returns true.
            return true;
        }
    }

Contriller:

 public class MealInfoController : Controller
    {
        //
        // GET: /MealInfo/
        public ActionResult MealInfo()
        {
            GridVirtualizationModel model = new GridVirtualizationModel();
            model.GridVirtualization.DataSourceUrl = Url.Action("BindGridVirtualization");
            this.InitializeGridVirtualization(model.GridVirtualization);
            return View(model);
        }
        public JsonResult BindGridVirtualization()
        {
            GridVirtualizationModel model = new GridVirtualizationModel();
            this.InitializeGridVirtualization(model.GridVirtualization);

            MMDataModelDataContext _db = new MMDataModelDataContext();
            var value = from c in _db.GetTable<tblMemberInfo>()
                        select c;
            model.GridVirtualization.DataSource = value;
            return model.GridVirtualization.GetData();
        }
        private void InitializeGridVirtualization(GridModel model)
        {
            //model.Height = "500px";   

            //
            var updating = new GridUpdating();
            updating.EnableAddRow = true;
            updating.EnableDeleteRow = true;
            updating.EditMode = GridEditMode.Row;
            //
            GridPaging paging = new GridPaging();
            paging.PageSize = 12;

            //updating.ColumnSettings.Add(new ColumnUpdatingSetting() { ColumnKey = "MemId", ReadOnly = true });
            updating.ColumnSettings.Add(new ColumnUpdatingSetting() { ColumnKey = "MemId" });

            List<string> MemList=GetMembers();
            int count = MemList.Count;
            string[] columns = new string[count];
            int n=0;
            foreach (string str in MemList)
            {
                columns[n] = str;
                n++;
            }


            //model.Features = list;
            for (int i = 0; i < count; i++)
            {               
                model.Columns.Add(new GridColumn(columns[i], columns[i], "number", "150px"));
            }
          "150px"));
            //model.Columns.Add(new GridColumn("PermanentAddress", "PermanentAddress", "string", "250px"));

            model.UpdateUrl = "Updating";
            model.Features.Add(updating);
            model.Features.Add(paging);
            model.AutoGenerateColumns = true;
            model.Virtualization = true;
        }
        public ActionResult Updating()
        {
            MMDataModelDataContext _db = new MMDataModelDataContext();
            var ds = _db.tblMemberInfos;
            ViewData["GenerateCompactJSONResponse"] = false;
            GridModel m = new GridModel();

            List<Transaction<MealEntry>> transactions = m.LoadTransactions<MealEntry>(HttpContext.Request.Form["ig_transactions"]);
            foreach (Transaction<MealEntry> t in transactions)
            {
                switch (t.type)
                {
                    case "row":
                        break;
                    case "newrow":
                        {
                            dynamic ob = t.row;
                            //string str = ob["MemId"].ToString();
                            string str = (string)ob.MemId;
                            break;
                        }
                    case "deleterow":
                        break;
                }
            }
            return View();
        }
        public List<string> GetMembers()
        {
            List<string> mem;
            using (MMDataModelDataContext _db = new MMDataModelDataContext())
            {
                mem = (from c in _db.GetTable<tblMemberInfo>()
                       where c.Active==true
                       select c.MemberNickName).ToList<string>();
            }
            return mem;
        }

    }

run time error occur at under line  code line

'does not contain a definition for 'Date'

please replay.

 

Thank you

Jahangir

Programmer

Interspeed Solutions Ltd.

 

Parents
No Data
Reply
  • 24671
    posted

    Hi,

    Which version of the controls are you using? If you are using 11.1, Updating was CTP at that time, therefore some things may not work well. I suggest to upgrade to the 11.2 Assemblies and check out the same code with that. 

    Let me know if that helps. Thanks

    Angel 

Children
No Data