在javascript中更新radGrid控件中动态创建的行(列)

时间:2014-09-19 22:20:46

标签: vb.net telerik telerik-grid radgrid radgridview

这是我的问题陈述 -

我有几列将动态创建。最后一列是“总计”列,最后一行是“总计”行。 因此,如果编辑了任何单元格,我需要更新“总计”列和行。 我在代码中动态添加了CalculatedColumn。它通过应用表达式来加载数据,但是在编辑单元格时,没有任何修改。

以下是我的问题 - 1)代码是否写得正确? 2)它是否需要更改,我只在Telerik工作了2天,并且我正在努力完成这项工作,所以我的代码可能都错了。 3)“CalculatedColumn”可以刷新单元格编辑/更新吗? 4)如果没有,那么我可以更新javascript中的行和列吗?我该怎么做呢?我尝试获取单元格值,但无法将修改后的值应用于最后一列。

我想我已经总结了我的所有问题。在此先感谢您的帮助。

 <form id="form1" runat="server">
     <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadAjaxPanel runat="server" ID="RadAjaxPanel1">

               <div>
            <asp:PlaceHolder ID="placeHolder1" runat="server">
                <telerik:RadGrid ID="radGrid2" runat="server" AutoGenerateColumns="false"  OnNeedDataSource="radGrid2_NeedDataSource"  OnUpdateCommand="radGrid2_UpdateCommand">
                    <MasterTableView EditMode="Batch" AllowAutomaticUpdates="false">
                        <BatchEditingSettings EditType ="Cell" />
                    </MasterTableView>
                </telerik:RadGrid>
            </asp:PlaceHolder>
        </div>
        </telerik:RadAjaxPanel>
</form>

Vb.NET代码 Telerik课程 -

Public Class Telerik3
Inherits System.Web.UI.Page

'Dim radGrid2 As New RadGrid()
Dim dt As DataTable = New DataTable()

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Private Sub CreateDynamicGrid()

    'radGrid2.AutoGenerateColumns = False
    'radGrid2.MasterTableView.EditMode = GridEditMode.Batch
    'radGrid2.MasterTableView.BatchEditingSettings.EditType = GridBatchEditingType.Cell
    'radGrid2.AllowAutomaticUpdates = True
    'radGrid2.ID = "radGrid2"
    'AddHandler radGrid2.ItemDataBound, AddressOf radGrid2_OnItemDataBound

    Dim boundColumnId As New GridBoundColumn()
    boundColumnId.DataField = "Id"
    boundColumnId.HeaderText = "ID"
    radGrid2.MasterTableView.Columns.Add(boundColumnId)

    Dim templateColumnPayAmount As New GridTemplateColumn()
    templateColumnPayAmount.HeaderText = "Payment Due"
    templateColumnPayAmount.ItemTemplate = New GridBoundTemplate("PaymentDue")
    templateColumnPayAmount.EditItemTemplate = New DefaultTemplate("PaymentDue")

    radGrid2.MasterTableView.Columns.Add(templateColumnPayAmount)

    templateColumnPayAmount = New GridTemplateColumn()
    templateColumnPayAmount.HeaderText = "Total Amount"
    templateColumnPayAmount.ItemTemplate = New GridBoundTemplate("TotalAmount")
    templateColumnPayAmount.EditItemTemplate = New DefaultTemplate("TotalAmount")

    radGrid2.MasterTableView.Columns.Add(templateColumnPayAmount)

    Dim calculatedColumn As New GridCalculatedColumn()
    calculatedColumn.DataFields = New String() {"PaymentDue", "TotalAmount"}
    calculatedColumn.HeaderText = "FinalTotal"
    calculatedColumn.Expression = "{0}+{1}"
    calculatedColumn.Aggregate = GridAggregateFunction.Sum
    'calculatedColumn.UniqueName = "FinalTotal"
    radGrid2.MasterTableView.Columns.Add(calculatedColumn)

    'placeHolder1.Controls.Add(radGrid2)
    radGrid2.EnableLinqExpressions = False
    radGrid2.Rebind()

End Sub

'Private Sub radGrid2_OnItemDataBound(sender As Object, e As GridItemEventArgs)
'    If e.Item.IsInEditMode Then

'    End If
'End Sub


Protected Sub radGrid2_UpdateCommand(sender As Object, e As GridCommandEventArgs)
    For Each row As DataRow In dt.Rows
        row(3) = 9000
    Next
    'radGrid2.DataSource = dt
    radGrid2.DataBind()
End Sub

Protected Sub radGrid2_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs)

    If dt.Columns.Count = 0 Then
        dt.Columns.Add("ID", System.Type.GetType("System.Int64"))
        dt.Columns.Add("PaymentDue", System.Type.GetType("System.Decimal"))
        dt.Columns.Add("TotalAmount", System.Type.GetType("System.Decimal"))
        dt.Columns.Add("FinalTotal", System.Type.GetType("System.Decimal"))

        Dim oItem As DataRow = dt.NewRow()
        oItem(0) = "1"
        oItem(1) = "1000.00"
        oItem(2) = "1000.00"
        oItem(3) = vbNull
        dt.Rows.Add(oItem)

        oItem = dt.NewRow()
        oItem(0) = "2"
        oItem(1) = "2000.05"
        oItem(2) = "3000.08"
        oItem(3) = vbNull
        dt.Rows.Add(oItem)

        ''RadGrid1.DataSource = dt
        'RadGrid1.DataBind()

        CreateDynamicGrid()

        radGrid2.DataSource = dt
        radGrid2.DataBind()
    End If

End Sub
End Class

DefaultTemplate.cs

Public Class DefaultTemplate
Implements ITemplate
Protected lControl As LiteralControl
Protected textBox As RadNumericTextBox
Private colname As String

Public Sub New(cName As String)
    colname = cName
End Sub

Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn

    textBox = New RadNumericTextBox()
    textBox.ID = "templateColumnTextBox"
    textBox.Height = 20
    textBox.Width = 120
    'textBox.AutoPostBack = True
    'AddHandler textBox.TextChanged, AddressOf textBox_TextChanged
    'textBox.ClientEvents.OnBlur = "UpdateGrid"
    container.Controls.Add(textBox)

End Sub

Private Sub textBox_TextChanged(sender As Object, e As EventArgs)
    Dim txtRad As RadNumericTextBox = sender
End Sub
End Class

GridBoundTemplate.cs

Public Class GridBoundTemplate
Implements ITemplate
Protected lControl As LiteralControl
Private colname As String

Public Sub New(cName As String)
    colname = cName
End Sub

Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn

    lControl = New LiteralControl()
    lControl.ID = "templateColumnTextBox"
    AddHandler lControl.DataBinding, AddressOf lControl_DataBinding
    container.Controls.Add(lControl)

End Sub


Public Sub lControl_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
    Dim l As LiteralControl = DirectCast(sender, LiteralControl)
    Dim container As GridDataItem = DirectCast(l.NamingContainer, GridDataItem)
    l.Text = (DirectCast(container.DataItem, DataRowView))(colname).ToString()

End Sub

End Class

0 个答案:

没有答案