CalculatedColumn and Delete Command

4 posts, 0 answers
  1. robertw102
    robertw102 avatar
    265 posts
    Member since:
    Jul 2007

    Posted 08 Jan 2009 Link to this post

    I've read other posts on the forums where people have received this error: "An Item with the same key has already been added".

    In one post you said it was fixed, but I'm using the latest version 3.1125 for .NET 2.0 and I receive this error only when the CalculatedColumn is being used and I click on the Delete button.

    My RadGrid is set up like this:
    <telerik:RadGrid ID="grdExpenditure" runat="server" AutoGenerateColumns="False" Width="100%" 
            EnableEmbeddedSkins="False" PageSize="20" VirtualItemCount="20" Height="300px" 
            BackColor="#ACBCCC" ImagesPath="~/CSS/Grid" CssClass="SearchGrid"
            <MasterTableView AllowPaging="True" AllowSorting="True" ShowHeadersWhenNoRecords="True" 
                DataKeyNames="Expenditure_ID" TableLayout="Fixed"
                <Columns> 
                    <telerik:GridHyperLinkColumn DataNavigateUrlFields="Expenditure_ID" DataNavigateUrlFormatString="cClient_ExpenditureEdit.aspx?Expend_ID={0}" 
                        UniqueName="Select" Text="<img src='../images/edit.gif' style='border-style: none' alt='Select' />"
                        <ItemStyle Width="2%" HorizontalAlign="Center" BorderWidth="1px" BorderColor="#000000" 
                            BorderStyle="Solid" /> 
                        <HeaderStyle Width="2%" HorizontalAlign="Center" /> 
                    </telerik:GridHyperLinkColumn> 
                    <telerik:GridBoundColumn DataField="NameAbbr" HeaderText="Prop." SortExpression="NameAbbr"
                        <ItemStyle HorizontalAlign="Left" Width="4%" BorderWidth="1px" BorderColor="#000000" 
                            BorderStyle="Solid" /> 
                        <HeaderStyle HorizontalAlign="Left" Width="4%" /> 
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Description" HeaderText="Description" SortExpression="Description"
                        <ItemStyle HorizontalAlign="Left" Width="16%" BorderWidth="1px" BorderColor="#000000" 
                            BorderStyle="Solid" /> 
                        <HeaderStyle HorizontalAlign="Left" Width="16%" /> 
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Item" HeaderText="Expense Item" SortExpression="Item"
                        <ItemStyle HorizontalAlign="Left" Width="10%" BorderWidth="1px" BorderColor="#000000" 
                            BorderStyle="Solid" /> 
                        <HeaderStyle HorizontalAlign="Left" Width="10%" /> 
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Category" HeaderText="Expense Category" SortExpression="Category"
                        <ItemStyle HorizontalAlign="Left" Width="16%" BorderWidth="1px" BorderColor="#000000" 
                            BorderStyle="Solid" /> 
                        <HeaderStyle HorizontalAlign="Left" Width="16%" /> 
                    </telerik:GridBoundColumn> 
                    <telerik:GridCheckBoxColumn DataField="IsCapitalExpense" HeaderText="Capital Exp" 
                        SortExpression="IsCapitalExpense"
                        <ItemStyle HorizontalAlign="Center" Width="5%" BorderWidth="1px" BorderColor="#000000" 
                            BorderStyle="Solid" /> 
                        <HeaderStyle HorizontalAlign="Center" Width="5%" /> 
                    </telerik:GridCheckBoxColumn> 
                    <telerik:GridBoundColumn DataField="VendorName" HeaderText="Vendor" SortExpression="VendorName"
                        <ItemStyle HorizontalAlign="Left" Width="10%" BorderWidth="1px" BorderColor="#000000" 
                            BorderStyle="Solid" /> 
                        <HeaderStyle HorizontalAlign="Left" Width="10%" /> 
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="TransactionDate" DataFormatString="{0:MMM. dd yyyy}" 
                        HeaderText="Transaction Date" SortExpression="TransactionDate"
                        <ItemStyle HorizontalAlign="Center" Width="7%" BorderWidth="1px" BorderColor="#000000" 
                            BorderStyle="Solid" /> 
                        <HeaderStyle HorizontalAlign="Center" Width="7%" /> 
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="TaxAmount" DataFormatString="{0:c}" HeaderText="Taxes" 
                        SortExpression="TaxAmount"
                        <ItemStyle HorizontalAlign="Right" Width="9%" BorderWidth="1px" BorderColor="#000000" 
                            BorderStyle="Solid" /> 
                        <HeaderStyle HorizontalAlign="Right" Width="9%" /> 
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="TotalAmount" DataFormatString="{0:c}" HeaderText="Taxable Amount" 
                        SortExpression="TotalAmount"
                        <ItemStyle HorizontalAlign="Right" Width="9%" BorderWidth="1px" BorderColor="#000000" 
                            BorderStyle="Solid" /> 
                        <HeaderStyle HorizontalAlign="Right" Width="9%" /> 
                    </telerik:GridBoundColumn> 
                    <telerik:GridCalculatedColumn Aggregate="Sum" Expression="{0}+{1}" DataType="System.Decimal" DataFields="TotalAmount,TaxAmount" HeaderText="Total" DataFormatString="{0:c}" 
                        > 
                        <ItemStyle HorizontalAlign="Right" Width="9%" BorderWidth="1px" BorderColor="#000000" 
                            BorderStyle="Solid" /> 
                        <HeaderStyle HorizontalAlign="Right" Width="9%" /> 
                    </telerik:GridCalculatedColumn> 
                    <telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete" ImageUrl="~/images/delete.gif" 
                        ConfirmText="Are You Sure You Want To Delete The Selected Expenditure?" ConfirmTitle="Delete Expenditure?"
                        <ItemStyle Width="3%" HorizontalAlign="Center" BorderWidth="1px" BorderColor="#000000" 
                            BorderStyle="Solid" /> 
                        <HeaderStyle Width="3%" HorizontalAlign="Center" /> 
                    </telerik:GridButtonColumn> 
                </Columns> 
                <AlternatingItemStyle BackColor="#ACBCCC" CssClass="mainInvoicingTextBlue " /> 
                <ItemStyle BackColor="#FFFFFF" CssClass="mainInvoicingTextBlue" /> 
                <HeaderStyle BackColor="#ACBCCC" CssClass="mainInvoicingTextBlue linkGoRentProBlue" 
                    Height="20px" BorderWidth="1px" BorderColor="#000000" BorderStyle="Solid" /> 
                <PagerStyle Mode="NextPrevAndNumeric" BackColor="#394D6B" Font-Bold="True" CssClass="mainInvoicingTextWhite linkGoRentProWhite" 
                    BorderWidth="1px" BorderColor="#000000" BorderStyle="Solid" PagerTextFormat="Page: {4} Total Records: {5}" 
                    ShowPagerText="True" AlwaysVisible="True" /> 
                <FooterStyle BackColor="#394D6B" BorderWidth="1px" BorderColor="#000000" BorderStyle="Solid" 
                    CssClass="mainInvoicingTextWhite" Font-Bold="True" /> 
            </MasterTableView> 
            <ClientSettings> 
                <Scrolling UseStaticHeaders="True" AllowScroll="True" SaveScrollPosition="False" /> 
            </ClientSettings> 
        </telerik:RadGrid> 

    And my DeleteCommand code looks something like this:
    Protected Sub grdExpenditure_DeleteCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grdExpenditure.DeleteCommand 
            Try 
                'hold expenditure id 
                Dim Expenditure_ID As Integer = Me.grdExpenditure.MasterTableView.DataKeyValues(e.Item.ItemIndex)("Expenditure_ID") 
     
                'delete the expenditure 
                If DeleteExpenditure(Expenditure_ID) > 0 Then 
                    'alert success 
                     
                    'rebind the grid 
                    Me.grdExpenditure.Rebind() 
                Else 
                    'alert failure 
                     
                End If 
            Catch ex As Exception 
                 
            End Try 
        End Sub 

    So in my delete command I just delete the selected Expenditure from the database and then force the grid to rebind its results.

    I know its the calculated column because when I remove it the problem goes away. So I'm guessing this bug still exists or I'm doing something wrong.


  2. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 10 Jan 2009 Link to this post

    Hello robert,

    Thank you for contacting us and for your question.
    I tested the setup which you mentioned, and the control behaved as expected.
    Attached to this message, you will find the code, which I used for testing.
    Please, take a look at it and let me know how it goes and if any other questions arise.

    Kind regards,
    Yavor
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. robertw102
    robertw102 avatar
    265 posts
    Member since:
    Jul 2007

    Posted 12 Jan 2009 Link to this post

    Hello Yavor,

    Your example worked correctly for me as well, although I had to make some changes to work with .NET 2.0.

    I've checked your code and doesn't work the same way as I have mine.

    Firstly, you use enabled "AllowAutomaticDeletes", which I don't use since I bind the results in the "NeedDatasource" event and I don't use ConnectionStrings or DataSource objects.

    Now when I modified your code to use NeedDatasource and I handled the DeleteCommand for myself, did I find out what the cause of the problem was. If I call Rebind in the DeleteCommand, the error that I mentioned of occurs. If I don't call Rebind, no error is thrown. So I have to assume that is the problem.

    Here are the changes I made to the code-behind of the page you gave me:
    protected void RadGri1_DeleteCommand(object source, GridCommandEventArgs e) 
        { 
            this.RadGri1.Rebind(); 
        } 
        protected void RadGri1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
        { 
            DataTable dtTest = new DataTable(); 
     
            dtTest.Columns.Add("CategoryID"); 
            dtTest.Columns.Add("Total"); 
            dtTest.Columns.Add("Tax"); 
     
            DataRow drOne = dtTest.NewRow(); 
            drOne["CategoryID"] = 1; 
            drOne["Total"] = 12.5; 
            drOne["Tax"] = 3; 
     
            DataRow drTwo = dtTest.NewRow(); 
            drTwo["CategoryID"] = 1; 
            drTwo["Total"] = 15; 
            drTwo["Tax"] = 3.25; 
     
            dtTest.Rows.Add(drOne); 
            dtTest.Rows.Add(drTwo); 
     
     
            this.RadGri1.DataSource = dtTest
        } 
     and here is the changes I made to the RadGrid control:
    <telerik:RadGrid runat="server" ID="RadGri1" GridLines="None" OnDeleteCommand="RadGri1_DeleteCommand" 
                    OnNeedDataSource="RadGri1_NeedDataSource" AutoGenerateColumns="False"
                    <HeaderContextMenu> 
                        <CollapseAnimation Duration="200" Type="OutQuint" /> 
                    </HeaderContextMenu> 
                    <MasterTableView AutoGenerateColumns="False" DataKeyNames="CategoryID"
                        <Columns> 
                            <telerik:GridBoundColumn DataField="CategoryID" DataType="System.Int32" HeaderText="CategoryID" 
                                ReadOnly="True" SortExpression="CategoryID" UniqueName="CategoryID"
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="Total" HeaderText="Total" SortExpression="Total" 
                                UniqueName="Total"
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="Tax" HeaderText="Tax" SortExpression="Tax" UniqueName="Tax"
                            </telerik:GridBoundColumn> 
                            <telerik:GridCalculatedColumn Aggregate="Sum" DataFields="Tax,Total" Expression="{0}+{1}" 
                                DataType="System.Decimal" DataFormatString="{0:c}"
                            </telerik:GridCalculatedColumn> 
                            <telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete" Text="Delete" 
                                UniqueName="column"
                            </telerik:GridButtonColumn> 
                        </Columns> 
                    </MasterTableView> 
                    <FilterMenu> 
                        <CollapseAnimation Duration="200" Type="OutQuint" /> 
                    </FilterMenu> 
                </telerik:RadGrid> 

    Now if you could try that and tell me if you experience the same problem.

    I've always called the Rebind method in every DeleteCommand I have coded and it only throws this error when a CalculatedColumn is used in the RadGrid control.
  5. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 14 Jan 2009 Link to this post

    Hello,

    You do not actually need to rebind the grid.
    This is demonstrated in the code snippet attached to this message.
    I hope this helps.

    Kind regards,
    Yavor
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top