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

Inserting new row from added boundcolumn

1 Answer 66 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bill
Top achievements
Rank 1
Bill asked on 18 Nov 2010, 03:06 AM
Hi,

I have a dropdown combobox which have 3 table listed. Each different selection will cause a selectedindexchanged event handler which reload the grid. There's 2 column declared on the front end: GridEditCommandColumn(edit) and GridButtonColumn(delete). Each time the grid is loaded or reloaded, a random number of boundcolumn are added to the grid based on the number of column from the selected table(from the combobox). All works fine except the edit and add row function. When I click add a new row, a textbox is added(in line) on the grid, for each column(except the edit and delete). After I enter value into the textbox, I can't seem to grab this value in the code behind.

Here's my code:
Private Sub LoadRadGrid(ByVal selectedTable As String, ByVal loadType As String)
        Dim ds As DataSet = New DataSet
  
        Dim ConnString As String = ConfigurationManager.ConnectionStrings("SqlConn").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(ConnString)
        Dim adapter As SqlDataAdapter = New SqlDataAdapter
  
        Dim sqlString As String = "SELECT ID, Iron, Name, Type FROM JOE_Coffee"
  
        If selectedTable = "JOE_Coffee" Then
            sqlString = "SELECT ID, Iron, Name, Type FROM JOE_Coffee"
        ElseIf selectedTable = "JOE_Cake" Then
            sqlString = "SELECT ID, Name FROM JOE_Cake"
        ElseIf selectedTable = "JOE_Tip" Then
            sqlString = "SELECT ID, Name FROM JOE_Tip"
        End If
  
        adapter.SelectCommand = New SqlCommand(sqlString, conn)
  
        Try
            adapter.Fill(ds, "Table1")
  
            For Each column As GridColumn In RadGrid1.MasterTableView.RenderColumns
                'If Not column.UniqueName = "EditCommandColumn" Then
  
                'End If
                If column.ColumnType = "GridBoundColumn" Then
                    Dim columnIndex As Integer = column.OrderIndex
                    RadGrid1.MasterTableView.Columns.Remove(column)
                End If
  
            Next
  
            For i As Integer = 0 To ds.Tables(0).Columns.Count - 1
                Dim boundColumn(i) As GridBoundColumn
                boundColumn(i) = New GridBoundColumn()
                boundColumn(i).HeaderText = ds.Tables(0).Columns(i).ColumnName
                boundColumn(i).DataField = ds.Tables(0).Columns(i).ColumnName
                boundColumn(i).UniqueName = ds.Tables(0).Columns(i).ColumnName
                boundColumn(i).IsBoundToFieldName(ds.Tables(0).Columns(i).ColumnName)
  
                RadGrid1.MasterTableView.Columns.Add(boundColumn(i))
                  
            Next
  
            If loadType = "PageLoad" Or loadType = "LoadAfterDelete" Or loadType = "SelectedIndexChangeLoad" Then
                RadGrid1.DataSource = ds
                RadGrid1.DataBind()
            ElseIf loadType = "LoadAfterInsert" Or loadType = "LoadAfterUpdate" Then
                'RadGrid1.MasterTableView.Columns.Clear()
                RadGrid1.DataSource = ds
                RadGrid1.Rebind()
            ElseIf loadType = "NeedDataSourceLoad" Then
                RadGrid1.DataSource = ds
                'Don't need to bind for "NeedDataSourceLoad"
            End If
  
            For Each column As GridColumn In RadGrid1.Columns
                If column.HeaderText = "ID" Then
                    column.Visible = False
                End If
            Next
        Catch ex As Exception
        End Try
End Sub
  
Protected Sub RadGrid1_CreateColumnEditor(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCreateColumnEditorEventArgs) Handles RadGrid1.CreateColumnEditor
        If (TypeOf e.Column Is GridBoundColumn) Then
            e.ColumnEditor = New GridTextBoxColumnEditor
        End If
End Sub
  
Protected Sub RadGrid1_InsertCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles RadGrid1.InsertCommand
        selectedTable = rcbTableName.SelectedValue.ToString()
  
        Dim iron As String = ""
        Dim name As String = ""
        Dim type As String = ""
  
        Dim ConnString As String = ConfigurationManager.ConnectionStrings("NineFiveSqlConn").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(ConnString)
  
        Dim cmd As New SqlCommand()
        cmd.Connection = conn
  
        Dim sqlString As String = "INSERT INTO JMOS_Satellite(Iron, Name, Type) VALUES(@Iron, @Name, @Type)"
  
        If selectedTable = "JMOS_Satellite" Then
            sqlString = "INSERT INTO JMOS_Satellite(Iron, Name, Type) VALUES(@Iron, @Name, @Type)"
        ElseIf selectedTable = "JMOS_UplinkBeam" Then
            sqlString = "INSERT INTO JMOS_UplinkBeam(Name) VALUES(@Name)"
        ElseIf selectedTable = "JMOS_UplinkChannel" Then
            sqlString = "INSERT INTO JMOS_UplinkChannel(Name) VALUES(@Name)"
        End If
  
        cmd.CommandType = CommandType.Text
        cmd.CommandText = sqlString
  
        '<<<Need value to set parameter for insert>>>                
                          
        cmd.Parameters.Add("@Iron", SqlDbType.VarChar)
        cmd.Parameters("@Iron").Value = iron
        cmd.Parameters.Add("@Name", SqlDbType.VarChar)
        cmd.Parameters("@Name").Value = name
        cmd.Parameters.Add("@Type", SqlDbType.VarChar)
        cmd.Parameters("@Type").Value = type
  
        Try
            If Not conn.State = ConnectionState.Open Then
                conn.Open()
                cmd.ExecuteNonQuery()
            End If
  
        Catch ex As Exception
            e.Canceled = True
        Finally
            If Not conn.State = ConnectionState.Closed Then
                conn.Close()
            End If
        End Try
  
        loadType = "LoadAfterInsert"
        LoadRadGrid(selectedTable, loadType)
End Sub

Here's the front end:
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
  
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="PageOne.aspx.vb" Inherits="WebRadGridDynamic.PageOne" %>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server">
    </telerik:RadStyleSheetManager>
    <div align="center">
        <br />
        <br />
        <br />
        <br />
        <telerik:RadComboBox ID="rcbTableName" runat="server" DataTextField="TableName" 
            DataValueField="ID" AutoPostBack="true" 
            OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged" Skin="Black" Width="200px">
                <Items>
                    <telerik:RadComboBoxItem Value="JMOS_Satellite" Text="Satellite" Selected="true" />
                    <telerik:RadComboBoxItem Value="JMOS_UplinkBeam" Text="Uplink Beam" />
                    <telerik:RadComboBoxItem Value="JMOS_UplinkChannel" Text ="Uplink Channel" />
                </Items>
        </telerik:RadComboBox>
        <br />
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
        <br />
        <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
            <script type="text/javascript">
                function RowDblClick(sender, eventArgs) {
                    sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
                }
            </script>
        </telerik:RadCodeBlock>
        <br />
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <br />
        <telerik:RadGrid ID="RadGrid1" runat="server" Width="500px" GridLines="None" PageSize="13" 
        AllowSorting="True" AllowPaging="True" ShowStatusBar="True" Skin="Black" Height="225px" AutoGenerateColumns="False" >
        <AlternatingItemStyle HorizontalAlign="Left" />
        <MasterTableView DataKeyNames="ID" AllowMultiColumnSorting="True" Width="100%"
        CommandItemDisplay="Top" EditMode="InPlace" >
        <CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings>
            <Columns>
                <telerik:GridEditCommandColumn UpdateText="Update" UniqueName="EditCommandColumn"
                CancelText="Cancel" EditText="Edit">
                    <HeaderStyle />
                    <ItemStyle Width="20px" />
                </telerik:GridEditCommandColumn>
                <telerik:GridButtonColumn UniqueName="DeleteColumn" Text="Delete" 
                CommandName="Delete" ConfirmTitle="Delete" ConfirmDialogType="Classic" ConfirmDialogHeight="60" ConfirmDialogWidth ="150" ConfirmText="Are you sure you want to delete row?" >
                    <HeaderStyle />
                    <ItemStyle Width="20px" />
                </telerik:GridButtonColumn>
                  
            </Columns>
            <EditFormSettings CaptionFormatString="Edit details ID {0}" CaptionDataField="ID">
                <FormTableItemStyle Width="100%" Height="29px"></FormTableItemStyle>
                <FormTableStyle GridLines="None" CellSpacing="0" CellPadding="2"></FormTableStyle>
                <FormStyle Width="100%" BackColor="#eef2ea"></FormStyle>
            <EditColumn ButtonType="ImageButton" />
            </EditFormSettings>
        </MasterTableView>
        <ClientSettings AllowColumnHide="True">
            <ClientEvents OnRowDblClick="RowDblClick" />
            <Scrolling AllowScroll="true" UseStaticHeaders="true" />
        </ClientSettings>
        <EditItemStyle />
        <HeaderStyle HorizontalAlign="Center" />
        <SelectedItemStyle />
            <ItemStyle HorizontalAlign="Left" />
        </telerik:RadGrid>
        <br />
        <br />
    </div>
    <div >
    </div>
    </form>
</body>
</html>

1 Answer, 1 is accepted

Sort by
0
Bill
Top achievements
Rank 1
answered on 18 Nov 2010, 10:13 PM
Ah, I found the solution to my problem. Thanks to Pavlina from the Telerik team.
The problem was I didn't add the boundcolumn to the grid first then set properties after.

So my code in the LoadRadGrid should be:

Dim
boundColumn(i) As GridBoundColumn

bundColumn(i) =
New GridBoundColumn()

RadGrid1.MasterTableView.Columns.Add(boundColumn(i))
boundColumn(i).HeaderText = ds.Tables(0).Columns(i).ColumnName

boundColumn(i).DataField = ds.Tables(0).Columns(i).ColumnName

boundColumn(i).UniqueName = ds.Tables(0).Columns(i).ColumnName

boundColumn(i).IsBoundToFieldName(ds.Tables(0).Columns(i).ColumnName)

Thanks
Tags
Grid
Asked by
Bill
Top achievements
Rank 1
Answers by
Bill
Top achievements
Rank 1
Share this question
or