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,
I want to use the Batch Update functionality for Web Grid. For this i set the DataKeyField of RootTable.Columns and add a column with DataMember of DataKeyField, and set its AutoIncrement property to true.
I allow user to Add,Update or Delete any record in the grid. Everything works great when there is no data in the grid.
But suppose if there are any existing records in the grid, It gives a problem.
The AutoIncrement column always starts its counter from 0, irrespective of data that already exists in that field.
Suppose i have set DataKeyField to "Id" Column.. Here's the structure
Id FirstName LastName
1 John Doe
2 Tim Brooklin
This record comes up in the grid once i open the page, now when i add a new record the new record is given the Id of 0, Now again i enter second new record its given an id of 1 by Auto Increment column.
If you see the Id 1 already existed in grid that was rendered in first page load and after that i enter 2 new records i get the same id of 1, which is a big problem..
Why is it happening so, why is the AutoIncrement not taking into picture the existing records and generate the new id based on the highest value of that column.
I dont want to update the database every time when a new record is entered, I want to update the database only when the user has finished updating the whole grid data.
How can i accomplish this?
Thanks,
Huzefa
A developer have another suggestion for the scenario you are facing, you could use the AddPendingRow client side event handler in order to insert the row KeyValue to the RowChange object if you wish to hide the primary key cell. However during the BatchUpdate server side event, you will need to retrieve the primary key from the KeyValue property instead of the cell since the cell is hiddden. Here is the snippet for the AddPendingRow client side event handler:
function wgTest_OnAddPendingChanges(controlId, table, rowChange){ var grid = ISGetObject(controlId); setTimeout(function() { rowChange.KeyValues = idTemp + ""; idTemp++; }, 10); }
Assuming the idTemp is the latest ID count you retrieved from the database.
The snippet to retrieve the primary key during server side event handler is, assuming the added row is in index 0 in the pending changes object:
((ISNet.WebUI.WebGrid.WebGridRowChanges)e.PendingChanges[0]).KeyValue
Hi Huzefa,
It should be not happened. Our WebGrid also handle ID as AutoIncrement. Does the ID reflect the real or (Auto) when you insert a row at batch update mode? Please open our WebGrid sample BatchUpdate_Enterprise.aspx and you can see Orders table in there. OrderID is also AutoIncrement which you also don't need to input. When using AutoIncrement, you would need to specify the method to retrieve the identityID. For Orders Table, the method is handled inside dsNorthwind_Extended.cs (DoInsert method). By handling select identity inside the method, WebGrid should return the correct ID. So, when update and delete, it would find the correct ID.
Regards,Handy
I am attaching a sample to demostrate the problem that i am facing.
I would give a brief understanding of what i am trying to achieve.
I have taken a Custom Object i.e. List<SysUser> and have 3 records in it at the start. Here is a outline
SysUserId LoginName Password
1 Admin 123
2 SysUser 123
3 PowerUser 123
For me when the user Clicks the AcceptChanges of Grid it goes to server and updates the Object and saves the updated object in Session variable, the updated changes are reflected back on to the grid.
Now there is a button below WebGrid on click of which the whole page data with grid data is saved into the database.
There are Couple of situation I would demonstrate them One by one.
1st Situation:
I have added columns to WebGrid and attached a DataSource to it on InitializeDataSource event. I have also made SysUserId as dataKeyField and set following property to SysUserId column, namely
IsAutoIncrement = true;
Visible = true;
When i set the DataKeyField column to be visible its fine the WebGrid understands the KeyField and sets the Text as Auto when trying to insert any new row.
Internally the id assigned to DataKeyField column starts from 0 (though i already have sysuserid of 1, 2,3).
After adding 3 new rows and post the grid data to server through the click of AcceptChanges button in WebGrid then i get the Keys as 0 , 1, 2
Q. Now this Keys already exists, why doesn't the Webgrid understand the data in the attached object for dataKeyField. It should basically assign the highest possible value to newly added row.
This newly generated keys of webGrid now conflicts with existing keys in the object.
2nd situation:
Now the properties for the dataKeyField Column is as follows,
Visible = false;
The KeyFieldColumn is now hidden so that User dont see that Auto text again nd again. Now if i add 3 new rows in the WebGrid, it will add and internally it will assign the id of 0,1,2 respectively.
Now if i delete the 1st Row in WebGrid then the 1st row wont be deleted instead the 2nd Row of newly added row is deleted. This because the 2nd row of newly added rows is given an id of 1 and the 1st row in the grid also has a id of 1.
Here it doesn't understand and deletes the 2nd record of newly added row.
Now tell me how should i handle this 2 situations.
Also can you tell me how disable/Hide the Accept All Changes button of WebGrid???
Hello Huzefa,
As far I know, List binding does not have auto increment feature. So the ID property is just a simple number property, which doesn't know about any logics. The ID return incorrectly from List itself. If you look into our sample, when we don't handle identity, the ID would always return incorrect ID, until you refresh the grid. HowEver, we can retrieve it back from database but List or the data itself not.
Currently, you could not hide Accept all changes button. If you don't show the statusbar, it will raise an error for batch update scenario. The possible way is hide the element, such as the code below:
function WebGrid1_OnInitialize(controlId) { var WebGrid1 = ISGetObject(controlId); document.getElementById("dvStatus_WebGrid1").style.display = "none"; return true; }
What about Situation 2, I dont wanna show the Auto Increment column to user. Instead hide it from user but internally it should keep auto incrementing.
Making the Auto Increment to Visible=false is giving problem as mentioned in my earlier post. Please tell me how can i overcome it?
Based on the sample you sent me before, I could not replicate the issue. It runs well when I add and delete the added rows. So, would you mind to show me step by step to replicate the issue? e.g in recording video.I think List binding can cause the issue because it is not suppossed in autoincrement scenario.
Hi Handy,
I will explain my application archtecture a bit first:
In my application UI is seperated from Business Components. The UI contains dispay related logic whereas the Business Component contains Business related logic. So the UI doesn't know anything about the database except that it recieves data in form of DTO (data transfer object) which is like a set of class containing subclasses to show dependencies.
Now i cannot use your DataSource controls as i don't directly connect to database. Thats why I directly bind List<> object to WebGrid.
Now to use the batch update feature i need to have an Id field which should always Auto-Increment(as asking user to put in Id wouldn't be great).
Once the user has added, updated or deleted any row in the WebGrid and wants to save the changes to database then on batchupdate event i again make a List<> object to be passed to Business Component.
The user should be able to Add, Update or Delete in Bulk for which the Auto-Increment of DataKeyField is very important.
But you say that Auto-Increment for List<> object Field is not properly supported in WebGrid.
So will it be never be supported or you guys working on resolving it?
Can i have an Auto-Increment field which won't be bound to List<> but is seperate and work as DataKeyField? In this case it doesn't have to understand the List<> object.
It is not our WebGrid that is not supporting this. It is List<> binding itself that is not fully supporting for auto-increment scenario.
Is there no way i can manually handle the Auto-Increment of DatKeyField through javascript??
You could try using the OnEndRowEditing client side event handler in order to modify the newly added cell primary key field using the SetText and SetValue function. You will also need to be able to retrieve the latest incremental value of the primary key in the clietn side (perhaps using hidden HTML input to store the value from database). Attached is a simple demonstration of the suggestion, for this sample the incremental value is set to 10.
The primary key column must be shown in the WebGrid in order to invoke the SetText and SetValue function. If we hide it using the visible property the cell object in the OnEndRowEditing event handler will return null. As a workaround, you could set the column EditType to NoEdit as demonstrated in the sample.
Thanks the solution did work perfectly, but i have a little problem here.
In Herarchical grid which is bound to List<> Custom object, when i add a new row in the parent table then the script is run and new keyValue is given to the new row. now when i am trying to add a new row inside this newly created row in Hierarchical grid then the ParentId is coming as 0 and not the new KeyValue given to parent record.
This way when the batchUpdate is been processed then i get error as parentId with 0 is not found.
Can you please tell me how to overcome this situation.
I assume you are talking about the workaround where the DataKeyField of the parent table is hidden. For such scenario, you will need to manually insert the parent key value into the HTML child table new row cell during BeginRowEditing client side event handler.
THe snippet provide below assume the parent table is named CustomerObj while the child table is named OrderObj. The parent key is located on the second column of the child table.
In order to correctly retrieve the parent key value from the child table, you will need to modify the AddPendingChanges client side event handler into:
function WebGrid1_OnAddPendingChanges(controlId, table, rowChange){ var grid = ISGetObject(controlId); if (table.Name == "CustomerObj") { setTimeout(function () { //debugger; rowChange.Element.setAttribute("KeyValue", idTemp); rowChange.KeyValues = idTemp + ""; rowChange.Row.KeyValue = idTemp + ""; idTemp++; }, 10); }}
Here is the snippet for BeginRowEditing client side event handler:
function WebGrid1_OnBeginRowEditing(controlId, row){ var grid = ISGetObject(controlId); if (row.Type == "NewRow" && row.Table.Name == "OrderObj") { var cells = row.GetElement().getElementsByTagName("td"); var idx = 0; for (var i = 0; i < cells.length; i++) { if(cells[i].getAttribute("type") == "Cell") { if (idx == 1) { var parentKey = row.GetParentRow().KeyValue; cells[i].innerHTML = parentKey; cells[i].setAttribute("cellValue", parentKey); break; } idx++; } } }}
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