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

Combo in Grid

2 Answers 96 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marnix Bouwman
Top achievements
Rank 2
Marnix Bouwman asked on 02 Jul 2010, 01:50 PM
Hi, I'm trying to use a combobox in a grid like in the exaple here: http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/comboingrid/defaultcs.aspx?product=grid

The only thing is, I would like to do some formatting etc. on the combobox, so I did:
                                <telerik:gridtemplatecolumn uniquename="IconID" datafield="IconID" headertext="Icon">
                                    <itemtemplate>
                                        <asp:image id="imgIcon" runat="server" />
                                    </itemtemplate>
                                    <edititemtemplate>
                                        <telerik:RadComboBox  
                                            ID="rcbIcon"
                                            runat="server" />
                                    </edititemtemplate>
                                </telerik:gridtemplatecolumn>

And then in the codebehind:

#Region "rgCategories_ItemDataBound"
        Protected Sub rgCategories_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgCategories.ItemDataBound
            If e.Item.ItemType = GridItemType.Item Or e.Item.ItemType = GridItemType.AlternatingItem Then
                Dim objCategoryInfo As CategoryInfo = CType(e.Item.DataItem, CategoryInfo)
                If objCategoryInfo.IconID <> -1 Then
                    _Icon = objCategoryInfo.IconID
                    Dim objIcon As MarkerInfo = lstMarkerInfo.Find(AddressOf FindIcon)
                    If Not objIcon Is Nothing Then
                        Dim imgIcon As System.Web.UI.WebControls.Image = CType(e.Item.FindControl("imgIcon"), System.Web.UI.WebControls.Image)
                        imgIcon.ImageUrl = objIcon.MarkerFilename
                    End If
                End If
            End If
            If e.Item.ItemType = GridItemType.EditItem Then
                Dim comboBox As RadComboBox = DirectCast(e.Item.FindControl("RCBIcon"), RadComboBox)
                comboBox.Items.Clear()
                Dim objMarkerInfo As MarkerInfo
                Dim itemtext As String

                For Each objMarkerInfo In lstMarkerInfo
                    itemtext = objMarkerInfo.MarkerFilename.Replace(".png", "")
                    itemtext = itemtext.Remove(0, itemtext.LastIndexOf("/") + 1)
                    Dim item As New RadComboBoxItem()
                    item.Text = itemtext
                    item.Value = objMarkerInfo.MarkerID
                    item.ImageUrl = objMarkerInfo.MarkerFilename
                    comboBox.Items.Add(item)
                Next

                Dim objCategoryInfo As CategoryInfo = CType(e.Item.DataItem, CategoryInfo)
                If objCategoryInfo.IconID <> -1 Then
                    If Not comboBox.FindItemByValue(objCategoryInfo.IconID) Is Nothing Then
                        comboBox.FindItemByValue(objCategoryInfo.IconID).Selected = True
                    End If
                End If
            End If
        End Sub
#End Region

However when I try to update, the IconId will always be 0 instead of the selected icon from the dropdown


2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 05 Jul 2010, 02:01 PM
Hello Marnix,

I guess you are performing AutomaticUpdate with SqlDataSource. You can update 'IconId' with RadComboBox selected value by setting the UpdateParameters of SqlDataSource from code behind( in UpdateCommand of RadGrid). Following is a sample code that i tried for similar kind of scenario.

ASPX:
  
 <telerik:RadGrid ID="rdgUsers" DataSourceID="SqlDataSource1" runat="server" AllowAutomaticUpdates="true" OnItemDataBound="rdgUsers_ItemDataBound" OnUpdateCommand="rdgUsers_UpdateCommand"
   <MasterTableView ............. . . . ..> 
      <Columns> 
          <telerik:GridTemplateColumn UniqueName="LastName" DataField="LastName" HeaderText="LastName"
                    <ItemTemplate> 
                        <%#Eval("LastName") %> 
                    </ItemTemplate> 
                    <EditItemTemplate> 
                        <telerik:RadComboBox ID="RadComboBox1" runat="server" /> 
                    </EditItemTemplate> 
                </telerik:GridTemplateColumn> 
            </Columns> 
        </MasterTableView> 
    </telerik:RadGrid> 
 
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
        SelectCommand="SELECT  * FROM [Employees] " UpdateCommand="UPDATE Employees set LastName=@LastName,FirstName=@FirstName where EmployeeID=@EmployeeID"
        <UpdateParameters> 
            <asp:Parameter Name="EmployeeID" /> 
            <asp:Parameter Name="FirstName" /> 
        </UpdateParameters> 
    </asp:SqlDataSource> 

C#:
  
 protected void rdgUsers_UpdateCommand(object source, GridCommandEventArgs e)  
    {  
        GridEditFormItem dataitem2 = (GridEditFormItem)e.Item;  
        RadComboBox combo = (RadComboBox)dataitem2.FindControl("RadComboBox1");  
        SqlDataSource1.UpdateParameters.Add("LastName", combo.SelectedValue); //set the UpdateParameter for SelectedValue of RadComboBox  
    }  

Hope this helps,
Princy


0
Marnix Bouwman
Top achievements
Rank 2
answered on 05 Jul 2010, 03:32 PM
Well, it actually was a objectdatasource, but that did the trick, so thanks!!!
Tags
Grid
Asked by
Marnix Bouwman
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Marnix Bouwman
Top achievements
Rank 2
Share this question
or