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

Copy checkbox record to new insert form

4 Answers 94 Views
Grid
This is a migrated thread and some comments may be shown as answers.
C O
Top achievements
Rank 1
C O asked on 29 Apr 2010, 09:24 PM
I have checkboxes in my grid row which I would like to pass to a new insert form.  However, the following code seems to be creating the form before inserting the data and therefore causing an invalid DBnull where it is expecting a boolean value.  How can this be corrected?

If e.CommandName = "CopyToInsertForm" Then 
                Dim item As GridDataItem = DirectCast(e.Item, GridDataItem) 
                'Put the RadGrid in the insert mode  
                RadGrid1.MasterTableView.IsItemInserted = True 
                RadGrid1.Rebind() 
                'Get the inserformItem using the GetInsertItem Method  
                Dim insertedItem As GridEditFormInsertItem = DirectCast(RadGrid1.MasterTableView.GetInsertItem(), GridEditFormInsertItem) 
                'Finding the textbox in the insert form and setting it's value  
                TryCast(insertedItem("NCR").Controls(0), RadTextBox).Text = item("NCR").Text 
                TryCast(insertedItem("PartNumber").Controls(0), RadTextBox).Text = item("PartNumber").Text 
                TryCast(insertedItem("Title").Controls(0), RadTextBox).Text = item("Title").Text 
                TryCast(insertedItem("Disposition").Controls(0), RadTextBox).Text = item("Disposition").Text 
                TryCast(insertedItem("Status").Controls(0), RadTextBox).Text = item("Status").Text 
                TryCast(insertedItem("maj").Controls(0), CheckBox).Checked = item("maj").Text 
                TryCast(insertedItem("min").Controls(0), CheckBox).Checked = item("min").Text 
                TryCast(insertedItem("corractionreqyes").Controls(0), CheckBox).Checked = item("corractionreqyes").Text 
                TryCast(insertedItem("corractionreqno").Controls(0), CheckBox).Checked = item("corractionreqno").Text 
 
            End If 


I work around the issue in normal insert with the following code:

            If e.CommandName = RadGrid.InitInsertCommandName Then 
                e.Canceled = True 
                'Prepare an IDictionary with the predefined values 
                Dim newValues As System.Collections.Specialized.ListDictionary = New System.Collections.Specialized.ListDictionary() 
 
                'set initial checked state for the checkbox on init insert 
                newValues("maj") = True 
                newValues("min") = False 
                newValues("corractionreqyes") = False 
                newValues("corractionreqno") = True 
                newValues("Liason") = User.Identity.Name + Space(5) + DateTime.Today 
 
                'Insert the item and rebind 
                e.Item.OwnerTableView.InsertItem(newValues) 
            End If 

4 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 05 May 2010, 08:09 AM
Hello C O,

You can use the ItemDataBound event handler, to get a reference to the insert form, and preset the values there:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
 {
     if (e.Item is GridEditFormInsertItem && RadGrid1.MasterTableView.IsItemInserted)
     {
     }
 }
I hope this helps.

Best wishes,
Yavor
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
C O
Top achievements
Rank 1
answered on 05 May 2010, 02:56 PM
Thanks for the help, but no, not really working.    Even after I convert it to VB it does not work. 

        Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs)  
            If TypeOf e.Item Is GridEditFormInsertItem AndAlso RadGrid1.MasterTableView.IsItemInserted Then  
 
            End If  
        End Sub 

 I no longer get the error, however, also I do not get the insert form.  Any other suggestions?  Just a note: my code works if I remove the checkboxes from the form, but then I can no longer pass that data for inserting, which isn't what I want.

Here is the test code I used to validate it does work.  However, this code does not have custom edit forms, which is where I am having the trouble arise.

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default2.aspx.vb" Inherits="Default2" %> 
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server">  
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server">  
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">  
        <Scripts> 
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" /> 
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" /> 
        </Scripts> 
    </telerik:RadScriptManager> 
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">  
        <AjaxSettings> 
            <telerik:AjaxSetting AjaxControlID="RadGrid1">  
                <UpdatedControls> 
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" /> 
                </UpdatedControls> 
            </telerik:AjaxSetting> 
        </AjaxSettings> 
    </telerik:RadAjaxManager> 
    <div> 
        <strong><span style="font-size: 24pt; color: #0099cc">Copying the row details into an  
            Insert form</span></strong><br /> 
        <br /> 
      
     <telerik:RadGrid ID="RadGrid1" runat="server" Skin="Black" GridLines="None"   
            AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"    
            Width="97%" OnNeedDataSource="RadGrid1_NeedDataSource"   
            OnDeleteCommand="RadGrid1_DeleteCommand"   
            OnInsertCommand="RadGrid1_InsertCommand"   
            OnUpdateCommand="RadGrid1_UpdateCommand" EnableAJAX="True"   
            OnItemCommand="RadGrid1_ItemCommand" >    
            <ExportSettings>    
                <Pdf PageBottomMargin="" PageFooterMargin="" PageHeaderMargin="" PageHeight="11in"    
                    PageLeftMargin="" PageRightMargin="" PageTopMargin="" PageWidth="8.5in" />    
            </ExportSettings>    
             <MasterTableView DataKeyNames="NCR_ID" GridLines="None" Width="100%" CommandItemDisplay ="Top" >    
                 
                <Columns>    
                    <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="Delete" />    
                    <telerik:GridBoundColumn DataField="NCR_ID" HeaderText="NCR_ID" UniqueName="NCR_ID" ReadOnly="True" />    
                    <telerik:GridBoundColumn DataField="NCR" HeaderText="NCR" UniqueName="NCR" />   
                    <telerik:GridBoundColumn DataField="PartNumber" HeaderText="PartNumber" UniqueName="PartNumber" /> 
                    <telerik:GridBoundColumn DataField="Title" HeaderText="Title" UniqueName="Title" /> 
                    <telerik:GridBoundColumn DataField="Disposition" HeaderText="Disposition" UniqueName="Disposition" />   
                    <telerik:GridBoundColumn DataField="Status" HeaderText="Status" UniqueName="Status" />    
                    <telerik:GridEditCommandColumn>    
                    </telerik:GridEditCommandColumn>    
                    <telerik:GridButtonColumn ButtonType="PushButton" CommandName="CopyToInsertForm" Text="Copy To InsertForm" UniqueName="column" />    
                </Columns>    
                <EditFormSettings ColumnNumber="2" CaptionFormatString="Edit details for data with ID {0}"    
                    CaptionDataField="NCR_ID">     
                    <FormTableItemStyle Wrap="False"></FormTableItemStyle>     
                    <FormMainTableStyle CellSpacing="0" CellPadding="3" Width="100%" />    
                    <FormTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="2"   
                        Height="110px" Width="100%" />    
                    <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>    
                    <FormStyle Width="100%" BackColor="gray"></FormStyle>    
                    <EditColumn UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit">     
                    </EditColumn>    
                    <FormTableButtonRowStyle HorizontalAlign="Right"></FormTableButtonRowStyle>    
                </EditFormSettings>    
                <ExpandCollapseColumn Visible="False">     
                    <HeaderStyle Width="19px"></HeaderStyle>    
                </ExpandCollapseColumn>    
                <RowIndicatorColumn Visible="False">     
                    <HeaderStyle Width="20px" />    
                </RowIndicatorColumn>    
            </MasterTableView>    
        </telerik:RadGrid>    
    </div> 
    </form> 
</body> 
</html> 
 
And the code behind:
Imports System  
Imports System.Data  
Imports System.Web  
Imports System.Web.Security  
Imports System.Web.UI  
Imports System.Web.UI.WebControls  
Imports System.Web.UI.WebControls.WebParts  
Imports System.Web.UI.HtmlControls  
Imports System.Data.SqlClient  
Imports Telerik.Web.UI  
Imports System.Configuration  
Partial Public Class Default2  
    Inherits System.Web.UI.Page  
    'Declare a global DataTable dtTable   
    Public Shared dtTable As DataTable  
    'Declare a global SqlConnection SqlConnection   
    Public Shared connectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("hldbConnectionString").ConnectionString  
    Public SqlConnection As New SqlConnection(connectionString)  
    'Declare a global SqlDataAdapter SqlDataAdapter   
    Public SqlDataAdapter As New SqlDataAdapter()  
    'Declare a global SqlCommand SqlCommand   
    Public SqlCommand As New SqlCommand()  
 
    Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs)  
        'Populate the Radgrid   
        dtTable = New DataTable()  
        'Open the SqlConnection   
        SqlConnection.Open()  
        Try  
            'Select Query to populate the RadGrid with data from table Customers.   
            Dim selectQuery As String = "SELECT * FROM ncrdata" 
            SqlDataAdapter.SelectCommand = New SqlCommand(selectQuery, SqlConnection)  
            SqlDataAdapter.Fill(dtTable)  
            RadGrid1.DataSource = dtTable 
        Finally  
            'Close the SqlConnection   
            SqlConnection.Close()  
 
        End Try  
    End Sub  
    Protected Sub RadGrid1_DeleteCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs)  
        'Get the GridDataItem of the RadGrid   
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)  
        'Get the primary key value using the DataKeyValue.   
        Dim NCR_ID As String = item.OwnerTableView.DataKeyValues(item.ItemIndex)("NCR_ID").ToString()  
        Try  
            'Open the SqlConnection   
            SqlConnection.Open()  
            Dim deleteQuery As String = "DELETE from ncrdata where NCR_ID='" & NCR_ID & "'"  
            SqlCommand.CommandText = deleteQuery 
            SqlCommand.Connection = SqlConnection 
            SqlCommand.ExecuteNonQuery()  
            'Close the SqlConnection   
 
            SqlConnection.Close()  
        Catch ex As Exception  
            RadGrid1.Controls.Add(New LiteralControl("Unable to delete data. Reason: " & ex.Message))  
            e.Canceled = True 
 
        End Try  
    End Sub  
    Protected Sub RadGrid1_UpdateCommand(ByVal source As Object, ByVal e As Telerik.WEB.UI.GridCommandEventArgs)  
        'Get the GridEditableItem of the RadGrid   
        Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)  
        'Get the primary key value using the DataKeyValue.   
        Dim NCR_ID As String = editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("NCR_ID").ToString()  
        'Access the textbox from the edit form template and store the values in string variables.   
        Dim NCR As String = TryCast(editedItem("NCR").Controls(0), TextBox).Text  
        Dim PartNumber As String = TryCast(editedItem("PartNumber").Controls(0), TextBox).Text  
        Dim Title As String = TryCast(editedItem("Title").Controls(0), TextBox).Text  
        Dim Disposition As String = TryCast(editedItem("Disposition").Controls(0), TextBox).Text  
        Dim Status As String = TryCast(editedItem("Status").Controls(0), TextBox).Text  
 
        Try  
            'Open the SqlConnection   
            SqlConnection.Open()  
            'Update Query to update the Datatable   
            Dim updateQuery As String = ((((("UPDATE ncrdata set NCR='" & NCR & "',PartNumber='") + PARTNUMBER & "',Title='") + Title & "',Disposition='") + Disposition & "',Status='") + status & "' where NCR_ID='") + NCR_ID & "'"  
            SqlCommand.CommandText = updateQuery 
            SqlCommand.Connection = SqlConnection 
            SqlCommand.ExecuteNonQuery()  
            'Close the SqlConnection   
 
 
            SqlConnection.Close()  
        Catch ex As Exception  
            RadGrid1.Controls.Add(New LiteralControl("Unable to update data. Reason: " & ex.Message))  
            e.Canceled = True 
 
        End Try  
    End Sub  
    Protected Sub RadGrid1_InsertCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs)  
        'Get the GridEditFormInsertItem of the RadGrid   
        Dim insertedItem As GridEditFormInsertItem = DirectCast(e.Item, GridEditFormInsertItem)  
 
        Dim NCR As String = TryCast(insertedItem("NCR").Controls(0), TextBox).Text  
        Dim PartNumber As String = TryCast(insertedItem("PartNumber").Controls(0), TextBox).Text  
        Dim Title As String = TryCast(insertedItem("Title").Controls(0), TextBox).Text  
        Dim Disposition As String = TryCast(insertedItem("Disposition").Controls(0), TextBox).Text  
        Dim Status As String = TryCast(insertedItem("Status").Controls(0), TextBox).Text  
 
        Try  
            'Open the SqlConnection   
            SqlConnection.Open()  
            'Update Query to insert into the database   
            Dim insertQuery As String = (((("INSERT into ncrdata(NCR,PartNumber,Title,Disposition,Status) values('" & NCR & "','") + PARTNUMBER & "','") + Title & "','") + Disposition & "','") + Status & "')"  
            SqlCommand.CommandText = insertQuery 
            SqlCommand.Connection = SqlConnection 
            SqlCommand.ExecuteNonQuery()  
            'Close the SqlConnection   
 
 
            SqlConnection.Close()  
        Catch ex As Exception  
            RadGrid1.Controls.Add(New LiteralControl("Unable to insert data. Reason: " & ex.Message))  
            e.Canceled = True 
 
        End Try  
    End Sub  
 
 
    Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)  
        If e.CommandName = "CopyToInsertForm" Then  
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)  
            'Put the RadGrid in the insert mode   
            RadGrid1.MasterTableView.IsItemInserted = True 
            RadGrid1.Rebind()  
            'Get the inserformItem using the GetInsertItem Method   
            Dim insertedItem As GridEditFormInsertItem = DirectCast(RadGrid1.MasterTableView.GetInsertItem(), GridEditFormInsertItem)  
            'Finding the textbox in the insert form and setting it's value   
            TryCast(insertedItem("NCR").Controls(0), TextBox).Text = item("NCR").Text  
            TryCast(insertedItem("PartNumber").Controls(0), TextBox).Text = item("PartNumber").Text  
            TryCast(insertedItem("Title").Controls(0), TextBox).Text = item("Title").Text  
            TryCast(insertedItem("Disposition").Controls(0), TextBox).Text = item("Disposition").Text  
            TryCast(insertedItem("Status").Controls(0), TextBox).Text = item("Status").Text  
 
        End If  
    End Sub  
End Class 


Thanks.
0
C O
Top achievements
Rank 1
answered on 06 May 2010, 03:59 PM
okay I think I have localized the error to the issue of in order to get the data the grid must rebind after setting to insert mode, which is not possible with the checkbox empty.  So the question is how can I get the values prior to initiating insert.  Since it is not standard forms mode indexer is not available and the method listed in the documentation does not work.

If e.CommandName = "CopyToInsertForm" Then 
 
                Dim item As GridDataItem = DirectCast(e.Item, GridDataItem) 
                'Put the RadGrid in the insert mode  
                RadGrid1.MasterTableView.IsItemInserted = True 
                RadGrid1.Rebind() 
                'Get the inserformItem using the GetInsertItem Method  
                Dim insertedItem As GridEditFormInsertItem = DirectCast(RadGrid1.MasterTableView.GetInsertItem(), GridEditFormInsertItem) 
                'Finding the textbox in the insert form and setting it's value  
                TryCast(insertedItem("NCR").Controls(0), RadTextBox).Text = item("NCR").Text 
                TryCast(insertedItem("PartNumber").Controls(0), RadTextBox).Text = item("PartNumber").Text 
                TryCast(insertedItem("Title").Controls(0), RadTextBox).Text = item("Title").Text 
                TryCast(insertedItem("Disposition").Controls(0), RadTextBox).Text = item("Disposition").Text 
                TryCast(insertedItem("Status").Controls(0), RadTextBox).Text = item("Status").Text 
                TryCast(insertedItem("maj").Controls(0), CheckBox).Checked = item("maj").Text 
                TryCast(insertedItem("min").Controls(0), CheckBox).Checked = item("min").Text 
                TryCast(insertedItem("corractionreqyes").Controls(0), CheckBox).Checked = item("corractionreqyes").Text 
                TryCast(insertedItem("corractionreqno").Controls(0), CheckBox).Checked = item("corractionreqno").Text 
 
            End If 

0
Iana Tsolova
Telerik team
answered on 11 May 2010, 11:28 AM
Hi,

Could you please open a formal support ticket and send us a runnable sample there? We will debug it locally and turn back to you with a proper resolution.

Additionally, you can try handling the grid ItemCommand event and handle the InitInsertCommand there in or to set predefined values for the desired controls as shown here.

Kind regards,
Iana
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
C O
Top achievements
Rank 1
Answers by
Yavor
Telerik team
C O
Top achievements
Rank 1
Iana Tsolova
Telerik team
Share this question
or