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 can not add new row programmatically, when a column is defined as autoincrement.
Please see attached files.
The first added row was typing in "new row". It works!
The second added row was programmacally added to grid. For more detailed info see ProgrammacallyAdd.aspx.
Hi Michael,
Try to use the following code to add the new row:
function wbtInsert_OnClientClick(controlId, parameter) { var wbtInsert = ISGetObject(controlId); var grd = ISGetObject("WebGrid1"); var newRow = grd.RootTable.NewRow(); var cells = newRow.GetCells(); cells.GetNamedItem("ProductName").SetText("xxx", true); cells.GetNamedItem("ProductID").SetChanges("false"); newRow.AddPendingChanges(); return true; }
Hope this helps.
Best Regards,
Gordon Tumewu
Hi,
I am trying to replicate your scenario. Everything works fine, I only add Insrt Command and Insert Parameter at your SqlDataSource and the record will added automatically. It's true after you click the button the ProductID cell didn't contain "(Auto)" but if you accept all changes, then the record will be added to the Database.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [ProductID], [ProductName] FROM [Products] ORDER BY [ProductID]" InsertCommand="INSERT INTO [Products] ([ProductName]) VALUES (@ProductName)"> <InsertParameters> <asp:Parameter Name="ProductName" Type="String" /> </InsertParameters> </asp:SqlDataSource>
Hi Gordon
It's true, it's possible to add a single record.
But try to add more then one record. I get the Message The pending changes already contain a record with key ''.
Regards
Michael
Hi GordonIt's true, it's possible to add a single record.But try to add more then one record. I get the Message The pending changes already contain a record with key ''.RegardsMichael
I get the same error.
I have a button that just adds NewRows with default data. Fairly simple you would think.
There is no PK at that point as they are pending rows.
Hi Shawn,
Have you tried to use the code that I posted earlier? If yes and you still get the error, is it possible for you to send me a simple sample? so I could check and provide solution for the issue.
Thanks.
Hi Shawn,Have you tried to use the code that I posted earlier? If yes and you still get the error, is it possible for you to send me a simple sample? so I could check and provide solution for the issue.Thanks.Best Regards,Gordon Tumewu
It will work for the first one but not subsequent calls similiar to the original poster. The following error message is displayed: The pending changes already contain a record with key ''. The problem goes away if I set the WebGrid's DataKeyField field to a unique value. For new rows, this won't have a value in my scenario because the data is not committed to the database yet.
What's the purpose of the SetChanges("false") method for the ProductID field? Why would the value of an apparent integer field be "false"?
Hi Shawn, Have you tried to use the code that I posted earlier? If yes and you still get the error, is it possible for you to send me a simple sample? so I could check and provide solution for the issue. Thanks. Best Regards, Gordon Tumewu It will work for the first one but not subsequent calls similiar to the original poster. The following error message is displayed: The pending changes already contain a record with key ''. The problem goes away if I set the WebGrid's DataKeyField field to a unique value. For new rows, this won't have a value in my scenario because the data is not committed to the database yet. What's the purpose of the SetChanges("false") method for the ProductID field? Why would the value of an apparent integer field be "false"?
Hi Shawn, Have you tried to use the code that I posted earlier? If yes and you still get the error, is it possible for you to send me a simple sample? so I could check and provide solution for the issue. Thanks. Best Regards, Gordon Tumewu
TTT
Sorry for the late response. Yes, you need to set the DataKeyField. If you tried the sample given by Michael Giger, you will see that he also set the DataKeyField to RootTable. You need to set it also.
Hi Shawn,Sorry for the late response. Yes, you need to set the DataKeyField. If you tried the sample given by Michael Giger, you will see that he also set the DataKeyField to RootTable. You need to set it also.Best Regards,Gordon Tumewu
I don't understand why this is necessary. It is common for many developers to rely on the database to creat the primary key (typically, an auto increment integer field or a GUID). This is not available on the client-side until the data is committed. That forces the developer to put a random unique number within the DataKeyField to satisfy the WebGrid control. Futhermore, this design is flawed because at first glance it appears the data is an existing record (the PK field is set) and the data isn't valid data (it's just data added to satisfy the WebGrid control).
Like what you said "It is common for many developers to rely on the database to creat the primary key", the grid will need you to describe which column is the primary key using the DataKeyField property.
Eventhough it's not a valid data "(Auto)" but we still need to define which column is the primary key through the DataKeyField property.
Hi Shawn,Like what you said "It is common for many developers to rely on the database to creat the primary key", the grid will need you to describe which column is the primary key using the DataKeyField property. Eventhough it's not a valid data "(Auto)" but we still need to define which column is the primary key through the DataKeyField property.Best Regards,Gordon Tumewu
I don't understand what you are saying. I understand that the DataKeyField property must be set to the primary key field. I do not why it must be specified each time a row is added. This is a bad design approach because the data is invalid and it indicates the data exists in the database which it does not.
Actually you didn't need to specify it everytime you want to add new row. If you check the sample from Michael Giger, you will see that he only set it at the aspx page at the roottable.
<RootTable DataKeyField="ProductID">
Once you set it, then it should be worked, if not, could you send me your simple page that replicate the issue, so I can check it.
Hi,Actually you didn't need to specify it everytime you want to add new row. If you check the sample from Michael Giger, you will see that he only set it at the aspx page at the roottable.<RootTable DataKeyField="ProductID"> Once you set it, then it should be worked, if not, could you send me your simple page that replicate the issue, so I can check it.Best Regards,Gordon Tumewu
The WebGrid can contain existing rows and new rows where this data is necessary for the existing rows. However, it isn't necessary for new rows even though the Grid is requiring it.
As I said earlier, WebGrid still need the DataKeyField to make the scenario worked. Actually you just need to set it to your primary key and the problem should be solved.
Hi,As I said earlier, WebGrid still need the DataKeyField to make the scenario worked. Actually you just need to set it to your primary key and the problem should be solved.Best Regards,Gordon Tumewu
You are not understanding the problem.
I understand the DataKeyField needs to be set. The problem is that the DataKeyField needs a value for each row or the exception message that has been discussed above is displayed. The only way around that is to specify a value for the DataKeyField. This is poor design because this data will not be available until the data is committed to the database. How do you get around NOT having to set this data for newly created fields?
I am sorry but I am confused with the scenario. I will try to explain it again, using the sample that michael gave, you dont need to set the productID, the grid will fill it as (AUTO) which will be change to the correct data after we accept all the changes. Do you get the same behaviour? or do you got error or unsuccesfull adding the new record?
Hi Shawn,I am sorry but I am confused with the scenario. I will try to explain it again, using the sample that michael gave, you dont need to set the productID, the grid will fill it as (AUTO) which will be change to the correct data after we accept all the changes. Do you get the same behaviour? or do you got error or unsuccesfull adding the new record?Best Regards,Gordon Tumewu
Why would it be set to "(AUTO)"?
I see the following code by Michael but don't understand why it is set to "false"? cells.GetNamedItem("ProductID").SetChanges("false");
Here's an example. Please click on the "New Row Causing More Data To Load" button more than once. You will see the exception because the ProductID field isn't set. I don't want to set it nor do I see the need to.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BatchUpdateDeleteRow.aspx.cs" Inherits="BatchUpdateDeleteRow" %> <%@ Register TagPrefix="iswebgrid" Namespace="ISNet.WebUI.WebGrid" Assembly="ISNet.WebUI.WebGrid" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD runat=server> <title>BatchUpdateDeleteRow</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <script language="javascript"> function WebGrid_SetFieldText(field, text, updateValueField) { try { // Set field's text // NOTE: Default the value to the text if flag is not specified if (field == null) { return; } if (typeof (updateValueField) == "undefined") { updateValueField = true; } field.SetText(text, updateValueField, true); } catch (ex) { ShowJSException(ex); } } function AddNewRowForceNewData() { var grdInurings = ISGetObject("WebGrid1"); // Create new row var newRow = grdInurings.RootTable.NewRow(); if (newRow == null) { return false; } // Default new row's data // NOTE: Scroll to row before new row to avoid lag from WebGrid loading more data (if exist) grdInurings.ClearSelection(); // Address var cells = newRow.GetCells(); var address = cells.GetNamedItem("Address"); if (address != null) { WebGrid_SetFieldText(address, ""); } // Customer ID var customerID = cells.GetNamedItem("CustomerID"); if (customerID != null) { window.alert("Why does this field need to be set for a new row when the ID is not set until the data is committed?"); } newRow.AddPendingChanges(); // Focus newRow.Select(); return true; } function AddNewRows(numOfNewRows, numOfDeleteRows) { var grdInurings = ISGetObject("WebGrid1"); if (typeof (numOfDeleteRows) == "undefined") { numOfDeleteRows = 0; } for (var index = 0; index < numOfNewRows; index++) { var newRow = AddNewRow(); if (index < numOfDeleteRows) { WebGrid_DeleteRow(newRow); } } return true; } function AddNewRow() { // Create new row var grdInurings = ISGetObject("WebGrid1"); var newRow = grdInurings.RootTable.NewRow(); if (newRow == null) { return false; } // Default new row's data // NOTE: Scroll to row before new row to avoid lag from WebGrid loading more data (if exist) grdInurings.ClearSelection(); // Address var cells = newRow.GetCells(); var address = cells.GetNamedItem("Address"); if (address != null) { WebGrid_SetFieldText(address, ""); } // Customer ID var customerID = cells.GetNamedItem("CustomerID"); if (customerID != null) { var now = new Date(); WebGrid_SetFieldText(customerID, String((now.getSeconds() + now.getMilliseconds()))); } newRow.AddPendingChanges(); return newRow; } function WebGrid_GetSelectedRow() { try { // Get selected row var grid = ISGetObject("WebGrid1"); if (grid == null) { return; } var selObj = grid.GetSelectedObject(); if (selObj == null) { return; } return selObj.ToRowObject(); } catch (ex) { ShowJSException(ex); } } function WebGrid_DeleteSelectedRow() { try { WebGrid_DeleteRow(WebGrid_GetSelectedRow()); } catch (ex) { ShowJSException(ex); } } function WebGrid_DeleteRow(row) { try { if (row == null) { return; } row.Delete(); row.AddPendingChanges(); var rowElement = row.GetElement(); //if (rowElement != null) { rowElement.style.display = "none"; } } catch (ex) { ShowJSException(ex); } } </script> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <input type="button" value="New Row Causing More Data To Load" onclick="javascript:AddNewRowForceNewData();" /> <input type="button" value="Add 5 new rows and delete 2" onclick="javascript:AddNewRows(5, 2);" /> <input type="button" value="Delete Selected Row" onclick="javascript:WebGrid_DeleteSelectedRow();" /> <input type="button" value="Add 10 new rows" onclick="javascript:AddNewRows(10);" /> <asp:button runat="server" Text="Check WebRowChanges" OnClick="PerformInuringsGridBatchUpdate" /> <iswebgrid:webgrid id=WebGrid1 runat="server" Height="279px" Width="896px" OnInitializeDataSource="WebGrid1_InitializeDataSource"> <LayoutSettings AutoHeight="false" AutoWidth="false" AllowBatchUpdate="true" BatchUpdateSettings-PromptUnsavedChanges="false" BatchUpdateSettings-AutomaticObjectUpdate="false" AllowEdit="Yes" EditOnClick="True" AllowAddNew="Yes" AllowDelete="Yes" PromptBeforeDelete="true" NewRowLostFocusAction="AlwaysPrompt" ResetNewRowValuesOnError="True" RowHeightDefault="22px" AllowFilter="Yes" AllowSelectColumns="Yes" AllowSorting="Yes" HideColumnsWhenGrouped="Default" AllowExport="Yes" InProgressUIBehavior="ChangeCursorToHourGlass" ApplyFiltersKey="Enter" AllowColumnFreezing="Yes" ShowFilterStatus="True" PagingMode="VirtualLoad" VerboseEditingInformation="False" FilterBarVisible="True" PagingExportMode="ExportAllData" CellPaddingDefault="0" AlwaysShowHelpButton="False" VirtualPageSize="25"> <HeaderStyle BorderStyle="Solid" BorderWidth="1px" BackColor="#ECE9D8" ForeColor="Black" Height="20px" Font-Size="8pt" Font-Names="Verdana" BorderColor="#ACA899"> <BorderSettings> <Left Color="White"></Left> <Top Color="White"></Top> </BorderSettings> </HeaderStyle> <FrameStyle BackColor="#F1EFE2"></FrameStyle> <GroupByBox> <LabelStyle BorderStyle="Solid" BorderWidth="1px" BackColor="White" Font-Size="8pt" Font-Names="Verdana" BorderColor="Navy"></LabelStyle> <Style BackColor="Gray"> </Style> </GroupByBox> <EditTextboxStyle BorderStyle="None" BorderWidth="0px" Font-Size="8pt" Font-Names="Verdana"></EditTextboxStyle> <NewRowStyle BackColor="White" ForeColor="DarkGray" Font-Size="8pt" Font-Names="Verdana"></NewRowStyle> <FocusCellStyle BorderStyle="Solid" BorderWidth="1px" BorderColor="Navy"></FocusCellStyle> <RowStyle CustomRules="text-overflow: ellipsis; overflow-x: hidden" BackColor="White" Font-Size="8pt" Font-Names="Verdana"></RowStyle> <GroupRowInfoStyle BorderStyle="Solid" BorderWidth="1px" BackColor="#F1EFE2" Font-Size="8pt" Font-Names="Verdana" BorderColor="White"> <BorderSettings> <Bottom Color="Silver"></Bottom> <Right Color="Silver"></Right> </BorderSettings> </GroupRowInfoStyle> <SelectedRowStyle BackColor="LightSteelBlue"></SelectedRowStyle> <AlternatingRowStyle CustomRules="text-overflow: ellipsis; overflow-x: hidden" BackColor="AntiqueWhite" Font-Size="8pt" Font-Names="Verdana"></AlternatingRowStyle> <StatusBarStyle BorderStyle="Solid" BorderWidth="1px" BackColor="#ECE9D8" Font-Size="8pt" Font-Names="Verdana" BorderColor="#ACA899"> <Padding Bottom="2px" Left="2px" Top="2px" Right="2px"></Padding> </StatusBarStyle> <StatusBarCommandStyle> <Over BorderWidth="1px" BorderColor="Navy" BorderStyle="Solid" BackColor="CornflowerBlue"></Over> <Normal> <Padding Bottom="1px" Left="1px" Top="1px" Right="1px"></Padding> </Normal> <Active BackColor="RoyalBlue" BaseStyle="Over"></Active> </StatusBarCommandStyle> <PreviewRowStyle ForeColor="#0000C0"></PreviewRowStyle> </LayoutSettings> <RootTable DataKeyField="CustomerID" Caption="Customers" GridLineStyle="NotSet"> <Columns> <iswebgrid:WebGridColumn Caption="Address" DataMember="Address" Name="Address" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="City" DataMember="City" Name="City" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="CompanyName" DataMember="CompanyName" Name="CompanyName" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="ContactName" DataMember="ContactName" Name="ContactName" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="ContactTitle" DataMember="ContactTitle" Name="ContactTitle" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="Country" DataMember="Country" Name="Country" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="CustomerID" DataMember="CustomerID" Name="CustomerID" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="Fax" DataMember="Fax" Name="Fax" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="Phone" DataMember="Phone" Name="Phone" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="PostalCode" DataMember="PostalCode" Name="PostalCode" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="Region" DataMember="Region" Name="Region" Width="100px"></iswebgrid:WebGridColumn> </Columns> </RootTable> </iswebgrid:webgrid> </form> </body> </HTML>
Ok, I will describe the code that I sent earlier to Michael.
First, grid will set the ID column manually to (AUTO), means that the ID is generated automatically from database, for example if the ID is auto increment or GUID.
For this, you need to inform grid by using the following code:
cells.GetNamedItem("ProductID").SetChanges("false");
After that, when you add pending changes, the grid will set the field to AUTO and you can add as many row that you want to add without getting the error.
Have you tried to use the code? If not, please try to use the code and let me know if it's not working. Because I have test and some customers have used it without any problem.
Regards,
Hi Shawn,Ok, I will describe the code that I sent earlier to Michael. First, grid will set the ID column manually to (AUTO), means that the ID is generated automatically from database, for example if the ID is auto increment or GUID.For this, you need to inform grid by using the following code:cells.GetNamedItem("ProductID").SetChanges("false");After that, when you add pending changes, the grid will set the field to AUTO and you can add as many row that you want to add without getting the error.Have you tried to use the code? If not, please try to use the code and let me know if it's not working. Because I have test and some customers have used it without any problem.Regards,Gordon Tumewu
I still get errors.
Click "Add 10 new rows".
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BatchUpdateDeleteRow.aspx.cs" Inherits="BatchUpdateDeleteRow" %> <%@ Register TagPrefix="iswebgrid" Namespace="ISNet.WebUI.WebGrid" Assembly="ISNet.WebUI.WebGrid" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD runat=server> <title>BatchUpdateDeleteRow</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <script language="javascript"> function WebGrid_SetFieldText(field, text, updateValueField) { try { // Set field's text // NOTE: Default the value to the text if flag is not specified if (field == null) { return; } if (typeof (updateValueField) == "undefined") { updateValueField = true; } field.SetText(text, updateValueField, true); } catch (ex) { ShowJSException(ex); } } function AddNewRowForceNewData() { var grdInurings = ISGetObject("WebGrid1"); // Create new row var newRow = grdInurings.RootTable.NewRow(); if (newRow == null) { return false; } // Default new row's data // NOTE: Scroll to row before new row to avoid lag from WebGrid loading more data (if exist) grdInurings.ClearSelection(); // Address var cells = newRow.GetCells(); var address = cells.GetNamedItem("Address"); if (address != null) { WebGrid_SetFieldText(address, ""); } // Customer ID var customerID = cells.GetNamedItem("CustomerID"); if (customerID != null) { window.alert("Why does this field need to be set for a new row when the ID is not set until the data is committed?"); var now = new Date(); WebGrid_SetFieldText(customerID, String((now.getSeconds() + now.getMilliseconds()))); } newRow.AddPendingChanges(); // Focus newRow.Select(); return true; } function AddNewRows(numOfNewRows, numOfDeleteRows) { var grdInurings = ISGetObject("WebGrid1"); if (typeof (numOfDeleteRows) == "undefined") { numOfDeleteRows = 0; } for (var index = 0; index < numOfNewRows; index++) { var newRow = AddNewRow(); if (index < numOfDeleteRows) { WebGrid_DeleteRow(newRow); } } return true; } function AddNewRow() { // Create new row var grdInurings = ISGetObject("WebGrid1"); var newRow = grdInurings.RootTable.NewRow(); if (newRow == null) { return false; } // Default new row's data // NOTE: Scroll to row before new row to avoid lag from WebGrid loading more data (if exist) grdInurings.ClearSelection(); // Address var cells = newRow.GetCells(); var address = cells.GetNamedItem("Address"); if (address != null) { WebGrid_SetFieldText(address, ""); } // Customer ID var customerID = cells.GetNamedItem("CustomerID"); if (customerID != null) { var now = new Date(); //WebGrid_SetFieldText(customerID, String((now.getSeconds() + now.getMilliseconds()))); window.alert("customerID.SetChanges('false');"); customerID.SetChanges("false"); } newRow.AddPendingChanges(); return newRow; } function WebGrid_GetSelectedRow() { try { // Get selected row var grid = ISGetObject("WebGrid1"); if (grid == null) { return; } var selObj = grid.GetSelectedObject(); if (selObj == null) { return; } return selObj.ToRowObject(); } catch (ex) { ShowJSException(ex); } } function WebGrid_DeleteSelectedRow() { try { WebGrid_DeleteRow(WebGrid_GetSelectedRow()); } catch (ex) { ShowJSException(ex); } } function WebGrid_DeleteRow(row) { try { if (row == null) { return; } row.Delete(); row.AddPendingChanges(); var rowElement = row.GetElement(); //if (rowElement != null) { rowElement.style.display = "none"; } } catch (ex) { ShowJSException(ex); } } function WebGrid_UpdateSelectedRow() { try { var grdInurings = ISGetObject("WebGrid1"); var updateRow = WebGrid_GetSelectedRow(); var cells = updateRow.GetCells(); var now = new Date() // Get grid // Indicate edit is occurring grdInurings.ClearSelection(); updateRow.Select(); updateRow.BeginEdit(); WebGrid_SetFieldText(cells.GetNamedItem("PortfolioNameList"), String("Hello World" + (now.getSeconds() + now.getMilliseconds()))); // Indicate row's data has been changed and set row stats to edit updateRow.Update(); updateRow.SetDataChanged(); updateRow.AddPendingChanges(); grdInurings.MarkEdit(); } catch (ex) { ShowJSException(ex); } } //================================================================= // Function : grdInurings_OnRowContextMenu // Description : This method will create Context menu based on the // : row. //================================================================= function grdInurings_OnRowContextMenu(controlId, rowType, rowElement, menuObject) { try { // Manage Portfolios var managePortfolios = new WebMenuItem(); managePortfolios.Text = "Manage Portfolios"; managePortfolios.Name = "itmManagePortfolios"; managePortfolios.OnClick = "WebGrid_UpdateSelectedRow"; menuObject.Items.Add(managePortfolios); } catch (ex) { ShowJSException(ex); } } </script> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <input type="button" value="Update Selected Row" onclick="javascript:WebGrid_UpdateSelectedRow();" /> <input type="button" value="New Row Causing More Data To Load" onclick="javascript:AddNewRowForceNewData();" /> <input type="button" value="Add 5 new rows and delete 2" onclick="javascript:AddNewRows(5, 2);" /> <input type="button" value="Delete Selected Row" onclick="javascript:WebGrid_DeleteSelectedRow();" /> <input type="button" value="Add 10 new rows" onclick="javascript:AddNewRows(10);" /> <asp:button runat="server" Text="Check WebRowChanges" OnClick="PerformInuringsGridBatchUpdate" /> <iswebgrid:webgrid id=WebGrid1 runat="server" Height="279px" Width="896px" DefaultStyleMode="Elegant" UseDefaultStyle="True" OnInitializeDataSource="WebGrid1_InitializeDataSource"> <LayoutSettings AutoHeight="false" AutoWidth="false" AllowBatchUpdate="true" BatchUpdateSettings-PromptUnsavedChanges="false" BatchUpdateSettings-AutomaticObjectUpdate="false" AllowEdit="Yes" EditOnClick="True" AllowAddNew="Yes" AllowDelete="Yes" PromptBeforeDelete="true" NewRowLostFocusAction="AlwaysPrompt" ResetNewRowValuesOnError="True" RowHeightDefault="22px" AllowFilter="Yes" AllowSelectColumns="Yes" AllowSorting="Yes" HideColumnsWhenGrouped="Default" AllowExport="Yes" InProgressUIBehavior="ChangeCursorToHourGlass" ApplyFiltersKey="Enter" AllowColumnFreezing="Yes" ShowFilterStatus="True" PagingMode="VirtualLoad" VerboseEditingInformation="False" FilterBarVisible="True" PagingExportMode="ExportAllData" CellPaddingDefault="0" AlwaysShowHelpButton="False" VirtualPageSize="25" > <ClientSideEvents OnRowContextMenu="grdInurings_OnRowContextMenu" /> <HeaderStyle BorderStyle="Solid" BorderWidth="1px" BackColor="#ECE9D8" ForeColor="Black" Height="20px" Font-Size="8pt" Font-Names="Verdana" BorderColor="#ACA899"> <BorderSettings> <Left Color="White"></Left> <Top Color="White"></Top> </BorderSettings> </HeaderStyle> <FrameStyle BackColor="#F1EFE2"></FrameStyle> <GroupByBox> <LabelStyle BorderStyle="Solid" BorderWidth="1px" BackColor="White" Font-Size="8pt" Font-Names="Verdana" BorderColor="Navy"></LabelStyle> <Style BackColor="Gray"> </Style> </GroupByBox> <EditTextboxStyle BorderStyle="None" BorderWidth="0px" Font-Size="8pt" Font-Names="Verdana"></EditTextboxStyle> <NewRowStyle BackColor="White" ForeColor="DarkGray" Font-Size="8pt" Font-Names="Verdana"></NewRowStyle> <FocusCellStyle BorderStyle="Solid" BorderWidth="1px" BorderColor="Navy"></FocusCellStyle> <RowStyle CustomRules="text-overflow: ellipsis; overflow-x: hidden" BackColor="White" Font-Size="8pt" Font-Names="Verdana"></RowStyle> <GroupRowInfoStyle BorderStyle="Solid" BorderWidth="1px" BackColor="#F1EFE2" Font-Size="8pt" Font-Names="Verdana" BorderColor="White"> <BorderSettings> <Bottom Color="Silver"></Bottom> <Right Color="Silver"></Right> </BorderSettings> </GroupRowInfoStyle> <SelectedRowStyle BackColor="LightSteelBlue"></SelectedRowStyle> <AlternatingRowStyle CustomRules="text-overflow: ellipsis; overflow-x: hidden" BackColor="AntiqueWhite" Font-Size="8pt" Font-Names="Verdana"></AlternatingRowStyle> <StatusBarStyle BorderStyle="Solid" BorderWidth="1px" BackColor="#ECE9D8" Font-Size="8pt" Font-Names="Verdana" BorderColor="#ACA899"> <Padding Bottom="2px" Left="2px" Top="2px" Right="2px"></Padding> </StatusBarStyle> <StatusBarCommandStyle> <Over BorderWidth="1px" BorderColor="Navy" BorderStyle="Solid" BackColor="CornflowerBlue"></Over> <Normal> <Padding Bottom="1px" Left="1px" Top="1px" Right="1px"></Padding> </Normal> <Active BackColor="RoyalBlue" BaseStyle="Over"></Active> </StatusBarCommandStyle> <PreviewRowStyle ForeColor="#0000C0"></PreviewRowStyle> </LayoutSettings> <RootTable DataKeyField="CustomerID" Caption="Customers" GridLineStyle="NotSet"> <Columns> <ISWebGrid:WebGridColumn Caption="Portfolio Name" Name="PortfolioNameList" DataType="System.String" ColumnType="Text" EditType="NoEdit" NewRowEditType="NoEdit" FilterEditType="TextBox" Width="100px"> </ISWebGrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="Address" DataMember="Address" Name="Address" Width="100px"></iswebgrid:WebGridColumn> <ISWebGrid:WebGridColumn Caption=" " Name="AddPortfolio" DataMember="" ButtonText="Add Portfolio" DataType="System.String" ColumnType="Template" EditType="NoEdit" NewRowEditType="SameAsEditType" FilterEditType="NoEdit" Width="65px"> <ButtonStyle BackColor="AliceBlue"> <Padding Top="2px" Left="1px" Right="1px" Bottom="1px" /> </ButtonStyle> <CellTemplate> <img runat="server" id="imgPortfolio" alt="Manage this treaty's portfolios" border="0" style="padding-top: 1px;" /> </CellTemplate> </ISWebGrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="City" DataMember="City" Name="City" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="CompanyName" DataMember="CompanyName" Name="CompanyName" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="ContactName" DataMember="ContactName" Name="ContactName" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="ContactTitle" DataMember="ContactTitle" Name="ContactTitle" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="Country" DataMember="Country" Name="Country" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="CustomerID" DataMember="CustomerID" Name="CustomerID" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="Fax" DataMember="Fax" Name="Fax" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="Phone" DataMember="Phone" Name="Phone" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="PostalCode" DataMember="PostalCode" Name="PostalCode" Width="100px"></iswebgrid:WebGridColumn> <iswebgrid:WebGridColumn Caption="Region" DataMember="Region" Name="Region" Width="100px"></iswebgrid:WebGridColumn> </Columns> </RootTable> </iswebgrid:webgrid> </form> </body> </HTML>
The error occured because Customer ID is not auto increment column, that's why you need to set the value. the solutions only works for auto increment column. I have tried to use your code on Michael's sample and it works normally.
I'm having this same problem. I have a stored procedure that has Select aKey as Indentity(5), TestID, TestName from TestTable.
Now, I have a grid with no rows that the users should have the ability to add as many rows as they want then click the save button (BatchUpdate). They aKey column doesn't need to show because it makes no sense to the user. I too had the same problem with the AddCustomer.aspx example. I insert a row then another row and I get the error message "the pending changes already contain a record with key ''. This is a huge problem for us because our work around has been to make the user save after each row entry. That's not the approach we are looking for.
Hi Tenika,
I modified the previous sample from Michael to adjust with your scenario, but I didn't get the same error. Could you try my sample and let me know if the problem occur or not at your end? If you have different scenario could you modify my sample so it will replicate the issue?
To run my sample, you need to put it in WebGridSamples.
Thanks Gordon,
It appears to be working when there are existing records in the data object that is bound to the grid. However, if there are no rows two things happen: 1) I'm not able to save the data. I get an Communication Error and 2) I see the "There are no rows in this view" as I enter data.
Any ideas?
I have attached a sample. I couldn't get it to replicate the Communication Error, but the save just sits there. I'm also not using a IDataSource either in my actual page.
Gordon,
I found a workaround. What I did to get around this issue was I return a dummy row and on the InitializeRow I set that row Visible = false.
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