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

how to assign rad grid column value to rad combo on update

1 Answer 127 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mubasshar
Top achievements
Rank 1
Mubasshar asked on 07 Jul 2011, 03:29 PM
Dear All

i am populating a rad combo box for area column on rad grid add command and every thing is working fine now what i need that when do the edit the grid column value should be selected in my combo box in my case its always showing the first value. i am pasting my code .  and the other issue is that i am unable to sort the radcombo box even i use sort option.

any one can help me on this issue as i am a newbie. thanks

VB CODe

Imports System
Imports System.Data
Imports Telerik.Web.UI
Imports System.Data.SqlClient
 
 
Public Class frmHotelMaster
    Inherits System.Web.UI.Page
 
    Dim objclsHotelMaster As New clsHotelMaster
    Dim ObjclsAreaMaster As New clsAreaMaster
    Private Const ItemsPerRequest As Integer = 10
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
 
        If Not (Page.IsPostBack) Then
 
            HotelGrid.DataSource = (objclsHotelMaster.GetHotelList).Tables("HotelMaster")
            HotelGrid.DataBind()
 
        End If
    End Sub
 
    Public Sub HotelGrid_DeleteCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles HotelGrid.DeleteCommand
        Dim _HotelCode As Integer = 0
        Dim _Hotel As String = ""
        Dim _Result As Long = 0
 
        Dim _item As GridEditableItem
        _item = e.Item
 
        Try
 
            _HotelCode = Convert.ToInt32(_item.OwnerTableView.DataKeyValues(_item.ItemIndex)("HotelCode"))
            '_Hotel = DirectCast(_item("UnitDesc").Controls(0), TextBox).Text
 
 
            _Result = objclsHotelMaster.DeleteHotel(_HotelCode)
 
            If _Result = 0 Then
                DisplayMessage("Record Deleted Successfully")
            Else
                DisplayMessage("Error While Deleting Record" & " - " & _Result)
            End If
 
            HotelGrid.Rebind()
        Catch ex As Exception
 
            DisplayMessage(ex.Message.ToString)
            e.Canceled = True
        End Try
    End Sub
 
    Public Sub HotelGrid_InsertCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles HotelGrid.InsertCommand
        Dim _HotelCode As Integer = 0
        Dim _Hotel As String = ""
        Dim _AreaCode As String = ""
        Dim _AreaDesc As String = ""
        Dim _Star As Int32 = 1
        Dim _Remarks As String = ""
        Dim _InActive As Boolean = False
 
        Dim _Result As Long = 0
 
        Dim _item As GridEditableItem
        _item = e.Item
 
        Try
 
            _HotelCode = clsGeneral.GetMax("HotelMaster", "HotelCode")     'Convert.ToInt32(_item.OwnerTableView.DataKeyValues(_item.ItemIndex)("UnitCode"))
            _Hotel = DirectCast(_item.FindControl("txtHotel"), TextBox).Text
            _AreaCode = DirectCast(_item.FindControl("ddHotelArea"), RadComboBox).SelectedItem.Value
            _AreaDesc = DirectCast(_item.FindControl("ddHotelArea"), RadComboBox).SelectedItem.Text
            _Star = DirectCast(_item.FindControl("ddHotelStar"), DropDownList).SelectedItem.Text
            _Remarks = DirectCast(_item.FindControl("txtRemarks"), TextBox).Text
            _InActive = DirectCast(_item.FindControl("chkInActive"), CheckBox).Checked
 
            If IsNothing(_HotelCode) Then
                DisplayMessage("Hotel Code Can not be Empty")
                e.Canceled = True
                Exit Sub
            End If
 
            If _Hotel = "" Or Len(_Hotel) <= 0 Then
                DisplayMessage("Hotel Name Can not be Blank")
                e.Canceled = True
                Exit Sub
            End If
 
            _Result = objclsHotelMaster.InsertHotel(_HotelCode, _Hotel, _AreaCode, _AreaDesc, _Star, _Remarks, _InActive)
 
            If _Result = 0 Then
 
                DisplayMessage("Record Saved Successfully")
            Else
                DisplayMessage("Error While Saving Record" & " - " & _Result)
            End If
 
            HotelGrid.Rebind()
        Catch ex As Exception
 
            DisplayMessage(ex.Message.ToString)
            e.Canceled = True
 
        End Try
    End Sub
 ' i was trying to do some thing error but do not know the exact command to get the value.
    Private Sub HotelGrid_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles HotelGrid.ItemCommand
        'Dim _item As GridEditableItem
        'Dim _AreaDesc As String = ""
        'Dim _Star As Int32 = 1
        'If e.CommandName = RadGrid.EditCommandName Then
        '    _item = e.Item
        '    _AreaDesc = DirectCast(_item("AreaDesc").Controls(1), TextBox).Text
        '    DisplayMessage("test")
        'End If
    End Sub
 
    Public Sub HotelGrid_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles HotelGrid.ItemDataBound
        Dim _item As GridEditableItem
 
        If (e.Item.IsInEditMode) Then
            _item = e.Item
            Dim ddl As DropDownList
            Dim ddl1 As RadComboBox
 
 
            'Filling Area
 
            ddl1 = DirectCast(_item.FindControl("ddHotelArea"), RadComboBox)
            ddl1.Items.Clear()
            ddl1.DataSource = ObjclsAreaMaster.GetAreaCodeList.Tables("AreaMaster")
            ddl1.DataTextField = "AreaDesc"
            ddl1.DataValueField = "AreaCode"
            ddl1.DataBind()
            ddl1.Sort = RadComboBoxSort.Ascending
            'Filling Drop Down
 
 
            ddl = DirectCast(_item.FindControl("ddHotelStar"), DropDownList)
            ddl.Items.Add("1")
            ddl.Items.Add("2")
            ddl.Items.Add("3")
            ddl.Items.Add("4")
            ddl.Items.Add("5")
            ddl.Items.Add("6")
            ddl.Items.Add("7")
 
            'ddl.Text = "7"
 
 
 
        End If
    End Sub
 
    Private Sub HotelGrid_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles HotelGrid.NeedDataSource
        HotelGrid.DataSource = (objclsHotelMaster.GetHotelList).Tables("HotelMaster")
        ' HotelGrid.DataBind()
    End Sub
 
    Public Sub HotelGrid_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles HotelGrid.UpdateCommand
 
 
        ' Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
        'Dim MyUserControl As UserControl = CType(e.Item.FindControl(GridEditFormItem.EditFormUserControlID), UserControl)
 
 
        Dim _HotelCode As Integer = 0
        Dim _Hotel As String = ""
        Dim _AreaCode As String = ""
        Dim _AreaDesc As String = ""
        Dim _Star As Int32 = 1
        Dim _Remarks As String = ""
        Dim _InActive As Boolean = False
 
 
        Dim _Result As Long = 0
 
        Dim _item As GridEditableItem
        _item = e.Item
 
        Try
 
            _HotelCode = Convert.ToInt32(_item.OwnerTableView.DataKeyValues(_item.ItemIndex)("HotelCode"))
            _Hotel = DirectCast(_item.FindControl("txtHotel"), TextBox).Text
 
            _AreaCode = DirectCast(_item.FindControl("ddHotelArea"), RadComboBox).SelectedItem.Value
            _AreaDesc = DirectCast(_item.FindControl("ddHotelArea"), RadComboBox).SelectedItem.Text
            _Star = DirectCast(_item.FindControl("ddHotelStar"), DropDownList).SelectedItem.Text
            _Remarks = DirectCast(_item.FindControl("txtRemarks"), TextBox).Text
            _InActive = DirectCast(_item.FindControl("chkInActive"), CheckBox).Checked
 
 
            If _Hotel = "" Or Len(_Hotel) <= 0 Then
                DisplayMessage("Hotel Name Can not be Blank")
                e.Canceled = True
                Exit Sub
            End If
 
            _Result = objclsHotelMaster.UpdateHotel(_HotelCode, _Hotel, _AreaCode, _AreaDesc, _Star, _Remarks, _InActive)
 
            If _Result = 0 Then
                DisplayMessage("Record Saved Successfully")
            Else
                DisplayMessage("Error While Saving Record" & " - " & _Result)
            End If
 
            'HotelGrid.Rebind()
        Catch ex As Exception
 
            DisplayMessage(ex.Message.ToString)
            e.Canceled = True
 
        End Try
 
 
 
 
    End Sub
 
    Private Sub DisplayMessage(ByVal text As String)
        HotelGrid.Controls.Add(New LiteralControl(String.Format("<span style='color:red'>{0}</span>", text)))
    End Sub
 
 
 
 
End Class



ASPX Code

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="frmHotelMaster.aspx.vb" Inherits="iTourism.frmHotelMaster" %>
 
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server" >
    <div>
     
        <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
        </telerik:RadScriptManager>
     
        <table style="width:100%;">
 
            <tr>
                <td>
 
                                    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" DefaultLoadingPanelID="RadAjaxLoadingPanel1">
                        <AjaxSettings>
                            <telerik:AjaxSetting AjaxControlID="HotelGrid">
                                <UpdatedControls>
                                          <telerik:AjaxUpdatedControl ControlID="HotelGrid" />
                                </UpdatedControls>
                            </telerik:AjaxSetting>
                        </AjaxSettings>
 
 
 
                    </telerik:RadAjaxManager>
 
 
                    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default">
                    </telerik:RadAjaxLoadingPanel>
                   <telerik:RadGrid ID="HotelGrid" runat="server" AutoGenerateColumns="False" GridLines="None"
                        AllowFilteringByColumn="True" AllowSorting="True"
                        AllowPaging="True" Width="80%" ViewStateMode="Enabled" CellPadding="10"
                        CellSpacing="10" OnUpdateCommand  ="HotelGrid_UpdateCommand" 
                        OnDeleteCommand="HotelGrid_DeleteCommand"
                        Skin="Office2007" OnInsertCommand  ="HotelGrid_InsertCommand"  OnItemDataBound="HotelGrid_ItemDataBound">
 
 
 
        <ClientSettings>
            <Selecting AllowRowSelect="True" />
            <Scrolling AllowScroll="True"  />
            <Selecting AllowRowSelect="True"></Selecting>
             
<Scrolling AllowScroll="True"></Scrolling>
             
        </ClientSettings>
 
 
 
<MasterTableView   EditMode="EditForms" DataKeyNames="HotelCode" InsertItemPageIndexAction="ShowItemOnCurrentPage" CommandItemDisplay="Top" ViewStateMode="Enabled">
 
<CommandItemSettings AddNewRecordText="Add Hotel"></CommandItemSettings>
 
    <Columns>
     
 
        <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn1"    />
 
        <telerik:GridBoundColumn DefaultInsertValue="" UniqueName="HotelCode"
            HeaderText="Hotel Code" DataField="HotelCode" ReadOnly="True">
        </telerik:GridBoundColumn>
         
        <telerik:GridBoundColumn DefaultInsertValue="" UniqueName="Hotel"
            HeaderText ="Hotel Description" DataField="Hotel" MaxLength="100" >
        </telerik:GridBoundColumn>
 
        <telerik:GridBoundColumn DefaultInsertValue="" UniqueName="AreaDesc" Headertext="Area" DataField="AreaDesc">
        </telerik:GridBoundColumn>
                  
        <telerik:GridBoundColumn DefaultInsertValue="" UniqueName="HotelStar" Headertext="Hotel Star" DataField="Star">
        </telerik:GridBoundColumn>
 
        <telerik:GridBoundColumn DefaultInsertValue="" UniqueName="Remarks" Headertext="Remarks" DataField="Remarks" >
        </telerik:GridBoundColumn>
             
        <telerik:GridCheckBoxColumn DataField="InActive" DataType="System.Boolean"
            DefaultInsertValue="false" UniqueName="InActive" HeaderText="In Active"
            ConvertEmptyStringToNull="False" >
        </telerik:GridCheckBoxColumn>
             
        <telerik:GridButtonColumn CommandName="Delete"  ButtonType="ImageButton"
            ConfirmText="Are You Sure To Delete" ConfirmTitle="Delete" Text="Delete"
            UniqueName="Delete">
        </telerik:GridButtonColumn>
         
 
                 
    </Columns>
 
    <EditFormSettings  EditFormType="Template"  >
 
        <FormTemplate   >
         
            <table>
                <tr>
                    <td>Hotel</td>
                    <td><asp:TextBox ID="txtHotel" MaxLength="100"  runat="server" text='<%# Bind ("Hotel") %>' ToolTip="Enter Hotel Name" ></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidatorDesc"   runat="server" ControlToValidate="txtHotel" ErrorMessage="Company Name Is Required" ForeColor="Red" ></asp:RequiredFieldValidator></td>
                </tr>
                <tr>
                    <td>Area</td>
                    <td><telerik:radComboBox  ID="ddHotelArea" Sort="Ascending"  Filter="Contains" SortCaseSensitive="false"    AllowCustomText="true"   runat="server"   ToolTip="Select Hotel Area" >   </telerik:radComboBox></td>                   
                    <td >Hotel Star</td>
                    <td><asp:DropDownList  ID="ddHotelStar"   runat="server"  ToolTip="Select Hotel Star" ></asp:DropDownList>
                    </td>
                </tr>
                 
                <tr>
                    <td>Remarks</td>
                    <td><asp:TextBox  ID="txtRemarks" Width="350px"   MaxLength="100"  runat="server" text='<%# Bind ("Remarks") %>' ToolTip="Enter Remarks" ></asp:TextBox>
                    </td>
                    <td></td>
                    <td>In Active</td>
                    <td><asp:CheckBox ID="chkInActive"  runat="server"  Checked='<%# Bind ("InActive") %>' ToolTip="Company Status" ></asp:CheckBox></td>
                </tr>
                 
 
                <tr><td>
                    <asp:ImageButton ID="btnInsert" CommandName="PerformInsert"    AlternateText="Add" ImageUrl="~/images/save.png" runat="server" Visible= '<%# HotelGrid.MasterTableView.IsItemInserted %>'/>
                    <asp:ImageButton ID="btnUpdate" CommandName="Update"    AlternateText="Update" ImageUrl="~/images/save.png" runat="server" Visible= '<%# HotelGrid.EditIndexes.Count > 0 %>'/>
                       
                    <asp:ImageButton ID="btnCancel" CommandName="Cancel" AlternateText="Cancel" ImageUrl="~/images/delete.png" runat="server" CausesValidation="false"  /> </td>
                </tr>
            </table>
         
 
         
        </FormTemplate>
      
     
    </EditFormSettings>
 
    <CommandItemSettings  AddNewRecordText="Add Hotel"  />
   
</MasterTableView>
 
 
 
    </telerik:RadGrid></td>
            </tr>
            
        </table>
     
    </div>
 
    </form>
</body>
</html>


thanks in advance

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 08 Jul 2011, 06:53 AM
Hello Mubasshar,

Try the following approach to get the selected value in ComboBox.

C#:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
        Dim value As String = item("ddHotelArea").Text
        Session("updatedValue") = value
    End If
    If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
        Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
        'populating combobox                 
        If Session("updatedValue") IsNot Nothing Then
            'checking for seesion variable.
            combo.SelectedValue = Session("updatedValue").ToString()
        End If
    End If
End Sub

Simple data-binding through the DataBind() method can be used in simple scenarios which does not require complex operations like paging, sorting etc. These operations requires advanced data binding through its NeedDataSource event or a data source control. Check the following help documentation which explains more about this.
Advanced Data-binding (using NeedDataSource event).

Thanks,
Princy.
Tags
Grid
Asked by
Mubasshar
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or