This is a migrated thread and some comments may be shown as answers.

grid with delete row column button and totals.

6 Answers 183 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Technology
Top achievements
Rank 1
Technology asked on 30 Nov 2010, 11:58 AM
Dear Telerik team,

I have several text and combo boxes and i use them to insert new rows into grid.
 1. The column button "Delete" in grid, it is not working.
 2. I want to convert the values in footer total to format string "{0:N2}" (ex. $5,000.00).
 3. When i create a row, the footer total is double x2 (Capture1). If i reload the page, the footer total is correct (Capture2).

Can you please help me?

Bellow you will find my VB code.
Thank you in advance for you time.

Best Regards,
Navarino Technology Department.

Protected Sub btnCreateRates_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateRates.Click
    If txtCards.Text = Nothing Then
        MsgBox("Please type number of cards, in order to continue.", MsgBoxStyle.OkOnly, "ERROR")
        Exit Sub
    Else
        'Confirmation()
 
        dtValues = DirectCast(Session("tblPendingOrders"), DataTable)
        ' retrieve DataTable from session
        Dim drValues As DataRow = dtValues.NewRow()
        GridID = grdData.MasterTableView.Items.Count + 1
        drValues("ID") = GridID
        drValues("Address1") = txtAddress1.Text
        drValues("Address2") = txtAddress2.Text
        drValues("Address3") = txtAddress3.Text
        drValues("Cards") = txtCards.Value
        drValues("CardType") = "Virtual"
        drValues("CareOf") = txtCareOf.Text
        drValues("Charge") = txtCards.Value * GetCharge()
        drValues("City") = txtCity.Text
        drValues("Country") = txtCountry.Text
        drValues("MasterOf") = cbVessels.SelectedItem.Text
        drValues("Notes") = txtNotes.Text
        drValues("OrderRef") = txtOrderRef.Text
        drValues("Owning") = txtOwning.Text
        drValues("Price") = GetCharge()
        drValues("Provider") = cbProviders.SelectedItem.Text
        drValues("Units") = cbUnits.SelectedItem.Text
        drValues("Vessel") = cbVessels.SelectedItem.Text
        drValues("VesselID") = cbVessels.SelectedValue
        drValues("ZipCode") = txtZipCode.Text
        dtValues.Rows.Add(drValues)
        dtValues.AcceptChanges()
        Session("tblPendingOrders") = dtValues
        'store DataTable in session
        grdData.Rebind()
        grdData.MasterTableView.IsItemInserted = False
        grdData.MasterTableView.Rebind()
 
        lblPendingOrders.Text = "Pending CCC Orders: " & grdData.MasterTableView.Items.Count
 
 
    End If
 
End Sub
 
Protected Sub grdData_ItemDeleted(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridDeletedEventArgs) Handles grdData.ItemDeleted
    Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
    Dim id As String = item.GetDataKeyValue("ID").ToString()
 
    If Not e.Exception Is Nothing Then
        e.ExceptionHandled = True
        SetMessage("Product with ID " + id + " cannot be deleted. Reason: " + e.Exception.Message)
    Else
        SetMessage("Product with ID " + id + " is deleted!")
 
    End If
End Sub
 
Private Sub SetMessage(ByVal message As String)
    gridMessage = message
End Sub
 
 
Protected Sub grdData_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grdData.NeedDataSource
 
    dtValues = New DataTable()
    dtValues.Columns.Add("ID")
    dtValues.Columns.Add("CardType")
    dtValues.Columns.Add("Provider")
    dtValues.Columns.Add("Vessel")
    dtValues.Columns.Add("VesselID")
    dtValues.Columns.Add("Units")
    dtValues.Columns.Add("Cards")
    dtValues.Columns.Add("Price")
    dtValues.Columns.Add("Charge")
    dtValues.Columns.Add("Address1")
    dtValues.Columns.Add("Address2")
    dtValues.Columns.Add("Address3")
    dtValues.Columns.Add("CareOf")
    dtValues.Columns.Add("City")
    dtValues.Columns.Add("Country")
    dtValues.Columns.Add("MasterOf")
    dtValues.Columns.Add("Notes")
    dtValues.Columns.Add("OrderRef")
    dtValues.Columns.Add("Owning")
    dtValues.Columns.Add("ZipCode")
    If Session("tblPendingOrders") IsNot Nothing Then
        ' retrieve DataTable from session
        dtValues = DirectCast(Session("tblPendingOrders"), DataTable)
    End If
    grdData.DataSource = dtValues
    Session("tblPendingOrders") = dtValues
 
End Sub
 
 
Protected Sub grdData_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles grdData.ItemDataBound
 
    If TypeOf e.Item Is GridHeaderItem Then
        Dim header As GridHeaderItem = DirectCast(e.Item, GridHeaderItem)
 
    End If
 
    If TypeOf e.Item Is GridDataItem Then
        Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)
 
        Price += dataItem("Price").Text
        Charge += dataItem("Charge").Text
        Cards += dataItem("Cards").Text
 
    End If
 
    If (TypeOf e.Item Is GridFooterItem) Then
        Dim footerItem As GridFooterItem = CType(e.Item, GridFooterItem)
 
        footerItem("Price").Text = Price
        footerItem("Charge").Text = Charge
        footerItem("Cards").Text = Cards
 
        footerItem("Provider").Controls.Add(New LiteralControl("<span style='color: Black; font-weight: bold;'>Totals:</span> "))
    End If
 
 
End Sub


6 Answers, 1 is accepted

Sort by
0
Technology
Top achievements
Rank 1
answered on 03 Dec 2010, 11:20 AM
Dear support Team,

Any news??
0
Martin
Telerik team
answered on 03 Dec 2010, 12:58 PM
Hello Navarino Technology Department,

Straight onto your questions:

  1. I am not able to find any code that preforms delete operation in your snippet. Please review the following help article that describes how to implement it:
    Deleting records
  2. I would suggest that you review the following online demo (check the Unit price column):
    Footer aggregates
  3. I have noticed that you rebind your grid as well as your master table in the btnCreateRates_Click event handler. My suggestion is to remove the grid rebinding operation and leave the master table rebinding only:

    Protected Sub btnCreateRates_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateRates.Click
        If txtCards.Text = Nothing Then
            ...
        Else
            ...
            'grdData.Rebind()
            grdData.MasterTableView.IsItemInserted = False
            grdData.MasterTableView.Rebind()
            ...
        End If
    End Sub

I hope this helps.

Kind regards,
Martin
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Technology
Top achievements
Rank 1
answered on 08 Dec 2010, 10:49 AM
Dear Martin,
thank you for your help.

2 and 3 done.
But the 1 (Delete row) i cannot make it work.

Can you please help me?
I get the error: "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
Below you will find my code.

Thank you in advance for your support,

Best regards,
George.
Navarino Technology Department.

Protected Sub grdData_DeleteCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grdData.DeleteCommand
    Dim ID As String = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("ID").ToString()
    'Dim table As DataTable = InsertData()
    If Not (dtValues.Rows.Find(ID) Is Nothing) Then
        dtValues.Rows.Find(ID).Delete()
        dtValues.AcceptChanges()
    End If
End Sub
 
 
Protected Sub btnCreateRates_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateRates.Click
    If txtCards.Text = Nothing Then
        MsgBox("Please type number of cards, in order to continue.", MsgBoxStyle.OkOnly, "ERROR")
        Exit Sub
    Else
        'Confirmation()
 
        dtValues = DirectCast(Session("tblPendingOrders"), DataTable)
        ' retrieve DataTable from session
        Dim drValues As DataRow = dtValues.NewRow()
        GridID = grdData.MasterTableView.Items.Count + 1
        drValues("ID") = GridID
        drValues("Address1") = txtAddress1.Text
        drValues("Address2") = txtAddress2.Text
        drValues("Address3") = txtAddress3.Text
        drValues("Cards") = txtCards.Value
        drValues("CardType") = "Virtual"
        drValues("CareOf") = txtCareOf.Text
        drValues("Charge") = txtCards.Value * GetCharge()
        drValues("City") = txtCity.Text
        drValues("Country") = txtCountry.Text
        drValues("MasterOf") = cbVessels.SelectedItem.Text
        drValues("Notes") = txtNotes.Text
        drValues("OrderRef") = txtOrderRef.Text
        drValues("Owning") = txtOwning.Text
        drValues("Price") = GetCharge()
        drValues("Provider") = cbProviders.SelectedItem.Text
        drValues("Units") = cbUnits.SelectedItem.Text
        drValues("Vessel") = cbVessels.SelectedItem.Text
        drValues("VesselID") = cbVessels.SelectedValue
        drValues("ZipCode") = txtZipCode.Text
        dtValues.Rows.Add(drValues)
        dtValues.AcceptChanges()
        Session("tblPendingOrders") = dtValues
        'store DataTable in session
        grdData.MasterTableView.IsItemInserted = False
        grdData.MasterTableView.Rebind()
 
        lblPendingOrders.Text = "Pending CCC Orders: " & grdData.MasterTableView.Items.Count
 
    End If
 
End Sub
 
 
Protected Sub grdData_ItemDeleted(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridDeletedEventArgs) Handles grdData.ItemDeleted
    Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
    Dim id As String = item.GetDataKeyValue("ID").ToString()
 
    If Not e.Exception Is Nothing Then
        e.ExceptionHandled = True
        SetMessage("Product with ID " + id + " cannot be deleted. Reason: " + e.Exception.Message)
    Else
        SetMessage("Product with ID " + id + " is deleted!")
 
    End If
End Sub
 
 
Private Sub SetMessage(ByVal message As String)
    gridMessage = message
End Sub
 
 
Protected Sub grdData_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grdData.NeedDataSource
 
    dtValues = New DataTable()
    dtValues.Columns.Add("ID")
    dtValues.Columns.Add("CardType")
    dtValues.Columns.Add("Provider")
    dtValues.Columns.Add("Vessel")
    dtValues.Columns.Add("VesselID")
    dtValues.Columns.Add("Units")
    dtValues.Columns.Add("Cards")
    dtValues.Columns.Add("Price")
    dtValues.Columns.Add("Charge")
    dtValues.Columns.Add("Address1")
    dtValues.Columns.Add("Address2")
    dtValues.Columns.Add("Address3")
    dtValues.Columns.Add("CareOf")
    dtValues.Columns.Add("City")
    dtValues.Columns.Add("Country")
    dtValues.Columns.Add("MasterOf")
    dtValues.Columns.Add("Notes")
    dtValues.Columns.Add("OrderRef")
    dtValues.Columns.Add("Owning")
    dtValues.Columns.Add("ZipCode")
    If Session("tblPendingOrders") IsNot Nothing Then
        ' retrieve DataTable from session
        dtValues = DirectCast(Session("tblPendingOrders"), DataTable)
    End If
    grdData.DataSource = dtValues
    Session("tblPendingOrders") = dtValues
    'store DataTable in session
 
End Sub

ASPX

                <telerik:RadGrid ID="grdData"
                                 runat="server"
                                 Width="670px"
                                 PageSize="20"
                                 ShowFooter="True"
                                 ShowStatusBar="True"
                                 Height="400px"
                                 Skin="Windows7"
                                 AllowAutomaticDeletes="True"
                                 AllowAutomaticInserts="True"
                                 AllowAutomaticUpdates="True">
                                <HeaderContextMenu EnableImageSprites="True">
                                </HeaderContextMenu>
                                 <GroupPanel Text="Drag a column header and drop it here to group by that column. Then click button 'Show Report'">
                                 </GroupPanel>
                                 <ExportSettings HideStructureColumns="True">
                                 </ExportSettings>
                                 <MasterTableView AutoGenerateColumns="true">
                                 <Columns>
                                    <telerik:GridButtonColumn ConfirmText="Delete this product?"
                                                              ConfirmDialogType="RadWindow"
                                                              ConfirmTitle="Delete"
                                                              ButtonType="ImageButton"
                                                              CommandName="Delete"
                                                              Text="Delete"
                                                              UniqueName="DeleteColumn">
                                        <ItemStyle HorizontalAlign="Center" />
                                    </telerik:GridButtonColumn>
                                 </Columns>
                                    <CommandItemSettings ExportToPdfText="Export to Pdf"
                                                         ShowExportToCsvButton="True"
                                                         ShowExportToExcelButton="True"
                                                         ShowExportToWordButton="True">
                                    </CommandItemSettings>
                                 </MasterTableView>
                                 <GroupingSettings ShowUnGroupButton="True" />
                                 <ClientSettings AllowDragToGroup="True">
                                 <Scrolling AllowScroll="true"
                                            UseStaticHeaders="True"
                                            SaveScrollPosition="False" />
                                </ClientSettings>
                </telerik:RadGrid>   


0
Martin
Telerik team
answered on 09 Dec 2010, 05:25 PM
Hello George,

Since your delete command uses the DataKeyValues collection to track the correct item to delete, you must set DataKeyNames to your MasterTableView:

...
<MasterTableView ... DataKeyNames="ID" ...>
...
</MasterTableView>
...

I hope this helps.

Regards,
Martin
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Technology
Top achievements
Rank 1
answered on 10 Dec 2010, 08:28 AM
Hi Martin, thank you for your respond.

now i get the error: "Object reference not set to an instance of an object." See attached image.
I changed what you said and i also changed the unique name.
I'm sure that i forget something.
Below is the changed code.

and another question.
How can i make visible=false a column from the above example?
the "grdData.MasterTableView.GetColumn("VesselID").Visible = False" is not working (with no error).

Thank you in advance for your time,

Best Regards,
George.
Navarino Technology Department.

<MasterTableView AutoGenerateColumns="true" DataKeyNames="ID" >
    <Columns>
       <telerik:GridButtonColumn ConfirmText="Delete this product?"
                                 ConfirmDialogType="RadWindow"
                                 ConfirmTitle="Delete"
                                 ButtonType="ImageButton"
                                 CommandName="Delete"
                                 Text="Delete"  
                                 UniqueName="ID">
           <ItemStyle HorizontalAlign="Center" />
       </telerik:GridButtonColumn>
    </Columns>
       <CommandItemSettings ExportToPdfText="Export to Pdf"
                            ShowExportToCsvButton="True"
                            ShowExportToExcelButton="True"
                            ShowExportToWordButton="True">
       </CommandItemSettings>
</MasterTableView>
0
Martin
Telerik team
answered on 15 Dec 2010, 05:17 PM
Hello George,

It looks like the dtValues table is not present in the grdData_DeleteCommand event handler. I have reviewed your code, but was not able to find where this table is initialized. Additionally note that the dtValues.Rows.Find(ID) method requires that the ID field of the dtValues table is set as a primary key. For more information on the matter, I would suggest that you review the following MSDN articles:

DataRowCollection.Find Method (Object)
DataTable.PrimaryKey Property

Also, mind that the DataKeyNames property refers to fields from the grid source, and not to the columns the grid contains. Therefore, to avoid any ambiguity, my suggestion is to change the delete column's UniqueID to something different than "ID".

As to "How can i make visible=false a column from the above example?", please review the following help article (pay special attention to the "Customizing columns programmatically" section):

Using columns

I hope this helps.

Regards,
Martin
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Technology
Top achievements
Rank 1
Answers by
Technology
Top achievements
Rank 1
Martin
Telerik team
Share this question
or