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

RadGrid on Submit commit to Database

3 Answers 126 Views
Grid
This is a migrated thread and some comments may be shown as answers.
AName
Top achievements
Rank 1
AName asked on 23 Dec 2013, 04:07 PM

Hello there,


Our requirement is for a RadGrid to provide inserts and edits to the rows.  At any given time if the user does not like what they have added, they can hit a "reload" button and the original dataset is reloaded to the grid.  This piece works fine.







However



Upon completion of inserted rows and edits to existing rows, the user will click a Submit button on a form and all of the new and edited data will be committed to the database. 







I am struggling with reading the radgrid to update to the database.



How do I loop through the rows...I have tried a couple methods to capture each row and so I may execute a SP to insert the row.







However I cannot seem to be able to read the rows, while they are int's strings etc.  my latest attempt was to just capture the text from each GridDataItem, based on a forum post I found.







For Each row As GridDataItem In rgMyGrid.Items

Dim ID As String = row.Item("ID").Text

Dim Rate As String = row.Item("Rate").Text

Dim Description As String = row.Item("Description").Text

Next

What are my options?  It does not appear I can pull the radgrid into a datatable. And my attempts along those lines failed.







So I am attempting to go row by row, which will work provided i can



1. identify each row



2. extract the data value from each column



3. convert to appropriate datatype







Thank you for suggestions



ps - i forgot to mention this is intended for VB codebehind.  thanks.

















3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 24 Dec 2013, 12:39 PM
Hi,

I guess you want to save the entire RadGrid on a button click, below is a sample i tried:

VB:
Public Shared connection As String = WebConfigurationManager.ConnectionStrings("Northwind_newConnectionString3").ConnectionString
Private conn As New SqlConnection(connection)
Protected Sub SaveRadBtn_Click(sender As Object, e As EventArgs)
    For Each item As GridDataItem In RadGrid1.MasterTableView.Items
        Dim id As String = item.GetDataKeyValue("ID").ToString()
        Dim rate As String = item("Rate").Text
        conn.Open()
        Dim query As String = (Convert.ToString((Convert.ToString("UPDATE Orders SET Rate='") & rate) + "' WHERE ID = '") & id) + "'"
        Dim cmd As New SqlCommand(query, conn)
        cmd.ExecuteNonQuery()
        conn.Close()
    Next
    RadGrid1.Rebind()
End Sub

Thanks,
Princy
0
AName
Top achievements
Rank 1
answered on 03 Jan 2014, 08:44 PM

Thank you Princy for responding, I apologize for the slow response myself.


I am noticing I am facing an issue, I can access the ID the next line when attempting to access the

rate the following exception is thrown...{"Object variable or With block variable not set."}



My radgrid looks like the following



<telerik:RadGrid OnItemEvent="rgrate_ItemEvent" ID="rgrate" Width="100%" AllowPaging="True" PageSize="8" runat="server" AllowSorting="True" GridLines="None"
    AutoGenerateColumns="True" ShowStatusBar="True" CellSpacing="0" EnableViewState="false" AutoGenerateEditColumn="true" OnUpdateCommand="rgrate_UpdateCommand">
    <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
    <MasterTableView ShowFooter="false" DataKeyNames="ID" EditMode="InPlace" Width="100%" CommandItemDisplay="Bottom"  HorizontalAlign="NotSet" AutoGenerateColumns="false">
    <CommandItemTemplate>
        <div style="padding: 5px 5px; text-align:left;">
            <asp:LinkButton ID="reload" Text="Reset to Defaults" CommandName="Reload" runat="server" OnCommand="lbReload_Command"></asp:LinkButton>
        </div>
    </CommandItemTemplate>
    <Columns>
        <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID" SortExpression="ID" Display="false"></telerik:GridBoundColumn>
        <telerik:GridTemplateColumn DataField="Description" UniqueName="Description" HeaderText=" Description" SortExpression="Description" HeaderStyle-Width="128px" ItemStyle-Width="128px" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Left">
            <ItemTemplate>
                <%#DataBinder.Eval(Container.DataItem, "Description")%>
            </ItemTemplate>
            <EditItemTemplate>
                <telerik:RadTextBox Width="128px" runat="server" ID="rgrate_Description" MaxLength="20" Text='<%#Eval("Description") %>'></telerik:RadTextBox>
            </EditItemTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridTemplateColumn DataField="rate" UniqueName="rate" HeaderText="Rate" SortExpression="rate"  ItemStyle-Width="56px"  HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Left">
            <ItemTemplate>
                <%#DataBinder.Eval(Container.DataItem, "rate")%>
            </ItemTemplate>
            <EditItemTemplate>
                <telerik:RadNumericTextBox Width="56px" ID="rgrate_Amount" Type="Percent" MinValue="0" MaxValue="100" MaxLength="2" NumberFormat-DecimalDigits="0" NumberFormat-GroupSeparator="," Culture="en-US"  runat="server" DbValue='<%#Eval("rate") %>'>
                </telerik:RadNumericTextBox>
            </EditItemTemplate>
        </telerik:GridTemplateColumn>
    </Columns>
    </MasterTableView>
    <ClientSettings></ClientSettings>
    <FilterMenu EnableTheming="True">
        <CollapseAnimation Duration="200" Type="OutQuint"></CollapseAnimation>
    </FilterMenu>
</telerik:RadGrid>


0
Princy
Top achievements
Rank 2
answered on 04 Jan 2014, 04:06 AM
Hi,

If you are trying to access the edit items in the UpdateCommand event, please try the following code snippet. If this doesn't help, please provide your full CS code and the steps to reproduce the error.

VB:
Protected Sub rgrate_UpdateCommand(sender As Object, e As GridCommandEventArgs)
    Dim edit As GridEditableItem = DirectCast(e.Item, GridEditableItem)
    Dim id As String = TryCast(edit("ID").Controls(0), TextBox).Text
    Dim descp As RadTextBox = DirectCast(edit.FindControl("rgrate_Description"), RadTextBox)
    Dim description As String = descp.Text
    Dim rate As RadNumericTextBox = DirectCast(edit.FindControl("rgrate_Amount"), RadNumericTextBox)
    Dim ratevalue As String = rate.Text
    'Code to update          
End Sub

Thanks,
Princy
Tags
Grid
Asked by
AName
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
AName
Top achievements
Rank 1
Share this question
or