And after I save the data, how do I close the input window?
Here's what I've tried:
Protected Sub RadGrid_InsertCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
'Get the GridEditFormInsertItem of the RadGrid
Dim insertedItem As GridEditFormInsertItem = TryCast(e.Item, GridEditFormInsertItem)
' Get an error on the next line
Dim ProductName As String = (TryCast(insertedItem("ProductName").Controls(0), TextBox)).Text
Dim UnitPrice As String = (TryCast(insertedItem("UnitPrice").Controls(0), TextBox)).Text
Dim UnitsOnOrder As String = (TryCast(insertedItem("UnitsOnOrder").Controls(0), TextBox)).Text
Dim insertQuery As String
insertQuery = "insert into Products set "
insertQuery = insertQuery & " ProductName='" & ProductName & "'"
insertQuery = insertQuery & " ,UnitPrice=" & UnitPrice
insertQuery = insertQuery & " ,UnitsOnOrder=" & UnitsOnOrder
' Execute the query
GridDataBind() ' Now that the data has been changed
End Sub
9 Answers, 1 is accepted
Have you set the EditMode to InPlace? If so try accessing the InsertFormItem as shown below.
CS:
Protected Sub RadGrid1_InsertCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) |
Dim insertedItem As GridDataInsertItem = TryCast(e.Item, GridDataInsertItem) |
' Get an error on the next line |
Dim ProductName As String = TryCast(insertedItem("ProductName").Controls(0), TextBox).Text |
'......... |
|
End Sub |
Thanks
Shinu
The suggested code returns a blank value when data has been entered.
I am not sure why it is returning a blank value. Here is the code which I tried on my end which is working.
VB:
Protected Sub RadGrid1_InsertCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) |
If (TypeOf e.Item Is GridDataInsertItem) AndAlso (e.Item.OwnerTableView.IsItemInserted) Then |
Dim insertItem As GridDataInsertItem = DirectCast(e.Item, GridDataInsertItem) |
Dim txtbx As TextBox = DirectCast(insertItem("ProductName").Controls(0), TextBox) |
Dim strProductName As String = txtbx.Text |
End If |
End Sub |
Shinu
<%@ Page EnableEventValidation="true" Language="VB" AutoEventWireup="false" CodeFile="TestAdd.aspx.vb" Inherits="TestAdd" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Sample Grid</title>
</head>
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<telerik:RadGrid ID="radGrid" runat="server"
GridLines="None"
PageSize="10"
AutoGenerateColumns = "False"
Skin="Office2007"
OnItemCommand= "radGrid_ItemCommand"
OnItemDataBound="radGrid_ItemDataBound"
OnDeleteCommand="RadGrid_DeleteCommand"
OnUpdateCommand="RadGrid_UpdateCommand"
OnInsertCommand="RadGrid_InsertCommand"
OnItemCreated= "RadGrid_ItemCreated"
PagerStyle-Mode = "NextPrevNumericAndAdvanced"
>
<MasterTableView
CommandItemDisplay="Top"
DataKeyNames="ProductID"
AllowAutomaticInserts = "True"
EditMode="InPlace"
>
<CommandItemTemplate >
<div style="padding:10px 0px;">
<asp:LinkButton ID="lnkcmdAdd" runat="server" CommandName="InitInsert"><img height="25" width="25" style="border:0px;vertical-align:middle;" alt="" src="Images/AddRecord.gif" /> Add Record</asp:LinkButton>
</div>
</CommandItemTemplate>
<Columns>
<telerik:GridEditCommandColumn UniqueName="cmdEditColumn" />
<telerik:GridBoundColumn ReadOnly="true" Visible="false" DataField="ProductID" UniqueName="ProductID" />
<telerik:GridBoundColumn DataField="ProductName" DataType="System.String" HeaderText="Product Name"
SortExpression="ProductName" UniqueName="ProductName">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</form>
</body>
</html>
Imports CDSFunctions
Imports Telerik.Web.UI
Partial Public Class TestAdd
Inherits System.Web.UI.Page
Protected CDSSQLFunctions As New CDSFunctions.SQLFunctions()
Protected SelectCommand As String = ""
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
'-----------------------------------------------------
lblMessage.Text = ""
If Not IsPostBack Then
GridDataBind()
Else
GridDataBind()
End If
End Sub
Public Sub GridDataBind()
SelectCommand = "SELECT [ProductID],[ProductName],[CompanyName],[CategoryName],[QuantityPerUnit],[UnitPrice],[UnitsInStock] ,[UnitsOnOrder],[ReorderLevel],[Discontinued],[DateOfLastInventory],[DateLastUpdated] From vwProducts_Info order by ProductName"
radGrid.DataSource = CDSSQLFunctions.GetDataSet(SelectCommand)
radGrid.DataBind()
End Sub
Public Sub radGrid_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs)
End Sub
Protected Sub radGrid_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs)
End Sub
Protected Sub RadGrid_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs)
End Sub
Protected Sub RadGrid_DeleteCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
End Sub
Protected Sub RadGrid_UpdateCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
End Sub
Protected Sub RadGrid_InsertCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
If (TypeOf e.Item Is GridDataInsertItem) AndAlso (e.Item.OwnerTableView.IsItemInserted) Then
Dim insertItem As GridDataInsertItem = DirectCast(e.Item, GridDataInsertItem)
Dim txtbx As TextBox = DirectCast(insertItem("ProductName").Controls(0), TextBox)
Dim strProductName As String = txtbx.Text
End If
End Sub
End Class
Try accessing the textbox value from the InsertForm in the ItemCommand event as shown below.
VB:
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) |
If e.CommandName = "PerformInsert" Then |
If (TypeOf e.Item Is GridDataInsertItem) AndAlso (e.Item.OwnerTableView.IsItemInserted) Then |
Dim insertItem As GridDataInsertItem = DirectCast(e.Item, GridDataInsertItem) |
Dim txtbx As TextBox = DirectCast(insertItem("ProductName").Controls(0), TextBox) |
Dim strProductName As String = txtbx.Text |
End If |
End If |
End Sub |
Also try to populate the Grid using Advanced Data Binding techniques.
Thanks
Princy
Do you have an example that you could send me?
Thanks.
Give a try with the following code library that I have submitted.
Manual Insert/Update/Delete operations using Auto-generated editform with sql statements from the code-behind
Also refer the following help article.
Insert/Update/Delete at database level with queries
In the above given aspx code you have set the AllowAutomaticInserts property to true. Try setting it to false and see whether it helps.
Thanks
Shinu.