5

I am trying to populate a MVC Webgrid using a DataTable which is built up in the code behind and then made enumerable using the AsEnumerable() extension method.

However, when I call the GetHtml method, the output is not what I expect, it consists of two columns HasErrors and RowError and none of the columns I have defined.

Am I missing something?

        DataTable table = new DataTable();
        table.Columns.Add("I/Dia");

        foreach (var item in Variations.Where(item => !table.Columns.Contains(item.CrossSectionalDiameter)))
        {
            table.Columns.Add(item.CrossSectionalDiameter);
        }

        foreach (var item in Variations)
        {
            var r = table.Rows.Add();
            r["I/Dia"] = item.InternalDiameter;
            r[item.CrossSectionalDiameter] = item.Price;
        }

        return table.AsEnumerable();
1
  • 1
    Based on a post I found [here][1] it appears that this is not possible to do with a WebGrid as it has no way of extracting the content from DataTables. It applies some hieuristic to dynamic types, but for all other types it extracts public property name-value pairs from the bound type. RowError and HasError are public properties on the DataRow. Unfortunately there isn't a clean way to bind the grid to a dynamically constructed type. Bugger :( [1]: forums.asp.net/t/1673391.aspx/1?WebGrid%20and%20DataTable Commented May 29, 2011 at 17:02

2 Answers 2

11

I have the similar question. Finally make some work around according to your comment.

        var result = new List<dynamic>();
        foreach (DataRow row in table.Rows)
        {
            var obj = (IDictionary<string, object>)new ExpandoObject();
            foreach (DataColumn col in table.Columns)
            {
                obj.Add(col.ColumnName, row[col.ColumnName]);
            }
            result.Add(obj);
        }
        var grid = new WebGrid(result)
Sign up to request clarification or add additional context in comments.

Comments

0

Convert Datatable To List

    #region "Convert DataTable to List<dynamic>"

    public List<dynamic> ToDynamicList(DataTable dt)
    {
        List<string> cols = (dt.Columns.Cast<DataColumn>()).Select(column => column.ColumnName).ToList();
        return ToDynamicList(ToDictionary(dt), getNewObject(cols));
    }
    public List<Dictionary<string, object>> ToDictionary(DataTable dt)
    {
        var columns = dt.Columns.Cast<DataColumn>();
        var Temp = dt.AsEnumerable().Select(dataRow => columns.Select(column =>
                             new { Column = column.ColumnName, Value = dataRow[column] })
                         .ToDictionary(data => data.Column, data => data.Value)).ToList();
        return Temp.ToList();
    }
    public List<dynamic> ToDynamicList(List<Dictionary<string, object>> list, Type TypeObj)
    {
        dynamic temp = new List<dynamic>();
        foreach (Dictionary<string, object> step in list)
        {
            object Obj = Activator.CreateInstance(TypeObj);
            PropertyInfo[] properties = Obj.GetType().GetProperties();
            Dictionary<string, object> DictList = (Dictionary<string, object>)step;
            foreach (KeyValuePair<string, object> keyValuePair in DictList)
            {
                foreach (PropertyInfo property in properties)
                {
                    if (property.Name == keyValuePair.Key)
                    {
                        property.SetValue(Obj, keyValuePair.Value.ToString(), null);
                        break;
                    }
                }
            }
            temp.Add(Obj);
        }
        return temp;
    }       
    private Type getNewObject(List<string> list)
    {
        AssemblyName assemblyName = new AssemblyName();
        assemblyName.Name = "tmpAssembly";
        AssemblyBuilder assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
        ModuleBuilder module = assemblyBuilder.DefineDynamicModule("tmpModule");
        TypeBuilder typeBuilder = module.DefineType("WebgridRowCellCollection", TypeAttributes.Public);
        foreach (string step in list)
        {
            string propertyName = step;
            FieldBuilder field = typeBuilder.DefineField(propertyName, typeof(string), FieldAttributes.Public);
            PropertyBuilder property = typeBuilder.DefineProperty(propertyName, System.Reflection.PropertyAttributes.None, typeof(string), new Type[] { typeof(string) });
            MethodAttributes GetSetAttr = MethodAttributes.Public | MethodAttributes.HideBySig;
            MethodBuilder currGetPropMthdBldr = typeBuilder.DefineMethod("get_value", GetSetAttr, typeof(string), Type.EmptyTypes);
            ILGenerator currGetIL = currGetPropMthdBldr.GetILGenerator();
            currGetIL.Emit(OpCodes.Ldarg_0);
            currGetIL.Emit(OpCodes.Ldfld, field);
            currGetIL.Emit(OpCodes.Ret);
            MethodBuilder currSetPropMthdBldr = typeBuilder.DefineMethod("set_value", GetSetAttr, null, new Type[] { typeof(string) });
            ILGenerator currSetIL = currSetPropMthdBldr.GetILGenerator();
            currSetIL.Emit(OpCodes.Ldarg_0);
            currSetIL.Emit(OpCodes.Ldarg_1);
            currSetIL.Emit(OpCodes.Stfld, field);
            currSetIL.Emit(OpCodes.Ret);
            property.SetGetMethod(currGetPropMthdBldr);
            property.SetSetMethod(currSetPropMthdBldr);
        }
        Type obj = typeBuilder.CreateType();
        return obj;
    }

    #endregion

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.