Dynamic Objects for ASPxGridview
Posted
by
André Snede Hansen
on Stack Overflow
See other posts from Stack Overflow
or by André Snede Hansen
Published on 2012-06-25T09:04:15Z
Indexed on
2012/06/25
9:16 UTC
Read the original article
Hit count: 309
I have a dictionary that is populated with data from a table, we are doing this so we can hold multiple SQL tables inside this object. This approached cannot be discussed.
The Dictionary is mapped as a , and contains SQL column name and the value, and each dictionary resembles one row entry in the Table.
Now I need to display this on a editable gridview, preferably the ASPxGridView. I already figured out that I should use Dynamic Objects(C#), and everything worked perfectly, up to the part where I find out that the ASPxGridview is built in .NET 2.0 and not 4.0 where Dynamic objects where implemented, therefor I cannot use it... As you cannot, to my knowledge, add rows to the gridview programmatically, I am out of ideas, and seek your help guys!
protected void Page_Load(object sender, EventArgs e)
{
UserValidationTableDataProvider uvtDataprovider = _DALFactory.getProvider<UserValidationTableDataProvider>(typeof(UserValidationTableEntry));
string[] tableNames = uvtDataprovider.TableNames;
UserValidationTableEntry[] entries = uvtDataprovider.getAllrecordsFromTable(tableNames[0]);
userValidtionTableGridView.Columns.Clear();
Dictionary<string, string> firstEntry = entries[0].Values;
foreach (KeyValuePair<string, string> kvp in firstEntry)
{
userValidtionTableGridView.Columns.Add(new GridViewDataColumn(kvp.Key));
}
var dynamicObjectList = new List<dynamic>();
foreach (UserValidationTableEntry uvt in entries)
{
//dynamic dynObject = new MyDynamicObject(uvt.Values);
dynamicObjectList.Add(new MyDynamicObject(uvt.Values));
}
}
public class MyDynamicObject : DynamicObject
{
Dictionary<string, string> properties = new Dictionary<string, string>();
public MyDynamicObject(Dictionary<string, string> dictio)
{
properties = dictio;
}
// 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)
{
// Converting the property name to lowercase
// so that property names become case-insensitive.
string name = binder.Name.ToLower();
string RResult;
// If the property name is found in a dictionary,
// set the result parameter to the property value and return true.
// Otherwise, return false.
bool wasSuccesfull = properties.TryGetValue(name, out RResult);
result = RResult;
return wasSuccesfull;
}
// 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.ToString();
// You can always add a value to a dictionary,
// so this method always returns true.
return true;
}
}
Now, I am almost certain that his "Dynamic object" approach, is not the one I can go with from here on.
I hope you guys can help me :)!
© Stack Overflow or respective owner