iSeller Commerce
iSeller POS Retail
iSeller POS F&B
iSeller POS Express
Crosslight
WebUI
ClientUI
What's New
Download Trial
Web Solution
Mobile Solution
Enterprise Solution
Custom Development
Blog
Community
Latest Development Blogs
ForumPostTopic
Browse By Tag
Hi There,
Is there anyway to set composite key to RootTable DataKeyField programatically?
I've tried to mark the property as primarykey.
e.g.
object
public class Person { public Person(string _Id, string _Name, string _Sex) { this._Id = _Id; this._Name = _Name; this._Sex = _Sex; } private string _Id = string.Empty; private string _Name = string.Empty; private string _Sex = string.Empty; [PrimaryKey()] public string Id { set { this._Id = value; } get { return this._Id; } } public string Name { set { this._Name = value; } get { return this._Name; } } public string Sex { set { this._Sex = value; } get { return this._Sex; } } }
dataobject
[DataObject] public class GenericObjectFactory<T> where T : class, IEntity { public GenericObjectFactory() { } [DataObjectMethodAttribute(DataObjectMethodType.Select, true)] public IList<Person> Select(int startRowIndex, int maximumRows, string sortExpression) { IList<Person> list = new List<Person>(); list.Add(new Person("1", "Name 1", "Male 1")); list.Add(new Person("1", "Name 1", "Male 1")); list.Add(new Person("1", "Name 1", "Male 1")); return list; } }
WebGrid DataSourceId
<ISWebGrid:WebGrid ID="EntityWebGrid" runat="server" DataSourceID="EntityWebGridObjectDataSource"><ISWebGrid:WebGrid ID="EntityWebGrid" runat="server" DataSourceID="EntityWebGridObjectDataSource" OnPrepareDataBinding="EntityWebGrid_PrepareDataBinding"></ISWebGrid:WebGrid> <rbs:GenericObjectDataSource ID="EntityWebGridObjectDataSource" runat="server"></rbs:GenericObjectDataSource> </ISWebGrid:WebGrid> <rbs:GenericObjectDataSource ID="EntityWebGridObjectDataSource" runat="server"></rbs:GenericObjectDataSource>
<ISWebGrid:WebGrid ID="EntityWebGrid" runat="server" DataSourceID="EntityWebGridObjectDataSource" OnPrepareDataBinding="EntityWebGrid_PrepareDataBinding"></ISWebGrid:WebGrid> <rbs:GenericObjectDataSource ID="EntityWebGridObjectDataSource" runat="server"></rbs:GenericObjectDataSource>
And call WebGrid.RetrieveStructure() within OnPrepareDatabinding event.
I've tried above but still get DataKeyField as null.
Any idea ?
Hi Dennis,
Yes, it will not be a problem. Attached is a running simple sample of getting composite keys. Please ensure to put the SimpleList.cs in your App_Code folder.
I hope it helps. Thank you and have a nice day.
Best Regards,
Andi Santoso
Even though you set that particular column as a [PrimaryKey()], you will need to make sure that on server side of PrepareDataBinding, you have set the DataKeyField for the WebGrid.
protected void WebGrid1_PrepareDataBinding(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { if (!IsPostBack) { WebGrid1.RetrieveStructure(); } WebGrid1.RootTable.DataKeyField = "CustomerID"; }
Then, if you try to debug it, you will see that the DataKeyField is null. That is our default behavior. However, if you look deeper you will see that each row already set its KeyValue.
Different case if you are trying to create a composite key. You will need to set two Primary Keys in your class. For this case, in PrepareDataBinding, you will not need to set any DataKeyField. Just leave it blank and it will automatically set the composite key. The composite key is returned as an array.
Hi Andi,
I've tried to mark 2 PrimaryKeys on my class, when i tried to retrieve keyvalues on selectedrow via javascript, it still return null.
var webGrid = ISGetObject(controlId); var selectedObject = webGrid.GetSelectedObject(); if (selectedObject != null) { var selectedRow = selectedObject.ToRowObject(); alert(selectedRow.KeyValue); alert(selectedRow.KeyValues);
any ideas ?
Hi Dennis Chee,
If that is the scenario, please ensure that you do not declare the DataKeyField in your server side PrepareDataBinding event. Once you set two Primary Keys on your class, WebGrid will automatically set its composite key values. Those key values will be set as an array, here are the snippet to get those key values:
var grid = ISGetObject("WebGrid1"); var selectedObject = grid.GetSelectedObject(); if (selectedObject != null) { var toRowObject = selectedObject.ToRowObject(); alert("Key Values are " + toRowObject.KeyValues[0] + " and " + toRowObject.KeyValues[1]); }
I do exactly as you said, it still not working. Is it possible for you to supply a simple working sample?
Regards,
Dennis
or
Choose this if you're already a member of Intersoft Community Forum. You can link your OpenID account to your existing Intersoft Social ID.
Choose this if you don't have an Intersoft account yet. Your authenticated OpenID will be automatically linked to your new Intersoft account.
Enter your Wordpress Blogname