TelerikDeveloper.com

TelerikDeveloper.com

sgray128 7/27/2012 12:51:58 PM

Telerik - ASP.NET RadGrid inside of a UserControl example

This is a complete example of a ASP.NET RadGrid that uses InPlace editing

We show access to a bound column, a template column, and the DataKeyValue

 



<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<!DOCTYPE html>
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"
        EditMode="InPlace" CommandItemDisplay="Top"
        CssClass="grid"
        AllowPaging="True" PageSize="20"  Width="600px"
        >
        <MasterTableView DataKeyNames="RowID" CommandItemDisplay="Top" EditMode="InPlace">
            <Columns>
                <telerik:GridButtonColumn ConfirmText="Delete this user?" ConfirmDialogType="RadWindow"
                    ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" ConfirmDialogHeight="100px"
                    ConfirmDialogWidth="220px"  />
                <telerik:GridEditCommandColumn   ButtonType="ImageButton"></telerik:GridEditCommandColumn>
  
                <telerik:GridBoundColumn HeaderText="OrderNumber" DataField="OrderNumber"  ></telerik:GridBoundColumn>
                <telerik:GridTemplateColumn HeaderText="Template Column">
                    <ItemTemplate>
                        <asp:label ID="lblDocAmount" runat="server" Text='<%# Bind("DocAmount")%>' ></asp:label>
                    </ItemTemplate>
                    <EditItemTemplate>
                           <telerik:RadNumericTextBox  ID="txtDocAmount" runat="server" Value='<%#  TD(Eval("DocAmount")) %>'
                               Width="100px" ShowSpinButtons="false" NumberFormat-DecimalDigits="2" />
                       </EditItemTemplate>
                </telerik:GridTemplateColumn>
 
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
 
    </div>
    </form>
</body>
</html>
 

Imports Telerik.Web.UI
 
Public Class WebForm1
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
    End Sub
 
    Private Sub RadGrid1_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        Me.RadGrid1.DataSource = GetData()
    End Sub
 
    Private Sub RadGrid1_DeleteCommand(sender As Object, e As GridCommandEventArgs) Handles RadGrid1.DeleteCommand
 
        'get one of the data key values
        Dim intRowID As Int32 = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("RowID")
 
        'DATA ACCESS CODE HERE
        'to save the record
 
        Me.RadGrid1.Rebind()
 
    End Sub
 
    Private Sub RadGrid1_InsertCommand(sender As Object, e As GridCommandEventArgs) Handles RadGrid1.InsertCommand
        Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
 
        'get bound columns
        Dim strOrderNumber As String = (TryCast(editedItem("OrderNumber").Controls(0), TextBox)).Text
 
        'get items in template colums
        Dim txtDocAmount As RadNumericTextBox = editedItem.FindControl("txtDocAmount")
        Dim decOrderAmount As Decimal = txtDocAmount.Value
 
        'DATA ACCESS CODE HERE
        'to save the record
 
        Me.RadGrid1.Rebind()
 
    End Sub
 
    Function TD(o As Object) As Decimal
        'the Templated RadNumericTextBox balked at Null values
        'this function will strip them
        Try
            Dim decOutput As Decimal = Convert.ToDecimal(o)
            Return 0
        Catch ex As Exception
            Return 0
        End Try
    End Function
 
    Private Sub RadGrid1_UpdateCommand(sender As Object, e As GridCommandEventArgs) Handles RadGrid1.UpdateCommand
        Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
 
        'get one of the data key values
        Dim intRowID As Int32 = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("RowID")
 
        'get bound columns
        Dim strOrderNumber As String = (TryCast(editedItem("OrderNumber").Controls(0), TextBox)).Text
 
        'get items in template colums
        Dim txtDocAmount As RadNumericTextBox = editedItem.FindControl("txtDocAmount")
        Dim decOrderAmount As Decimal = txtDocAmount.Value
 
        'DATA ACCESS CODE HERE
        'to save the record
 
        Me.RadGrid1.Rebind()
 
    End Sub
 
    Function GetData() As DataTable
        'returns a sample datatable for our example
        Dim oDT As New DataTable
 
        'add the columns
        oDT.Columns.Add(New DataColumn("RowID", System.Type.GetType("System.Int32")))
        oDT.Columns.Add(New DataColumn("OrderNumber", System.Type.GetType("System.String")))
        oDT.Columns.Add(New DataColumn("DocAmount", Type.GetType("System.Double")))
        oDT.Columns("DocAmount").AllowDBNull = False
 
        'add rows
        oDT.Rows.Add(1, "ORD123", 1.21)
        oDT.Rows.Add(2, "ORD124", 1.66)
        oDT.Rows.Add(3, "ORD125", 1.33)
 
        Return oDT
 
    End Function
 
End Class