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

Set Combo box value to Label value in gridview during edit

2 Answers 174 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
AkAlan
Top achievements
Rank 2
AkAlan asked on 27 Dec 2010, 08:18 PM
Hi, I have a gridview and am using templates to display and edit data. In the Item templates I use a Label to display the records RefrigerantType. When I go to edit mode I have a dropdown which is bound to a sqlDatasource and it works fine except that I need it to default to the value of the label in the item template. I have searched quite a bit but can't seem to find a way to do this. One thing that is different here is that there is only one Column in the table for the Refrigerant Type so both the DataTextField and DataValueField are the same. How do I get the value of the Label and set the dropdown default value to it? Thanks for any help.

2 Answers, 1 is accepted

Sort by
0
AkAlan
Top achievements
Rank 2
answered on 28 Dec 2010, 12:05 AM
I have a partial solution. One of the issues I was having was actually a data issue and I will explain here for the benefit of others. As I had said, I had only one row of Refrigerant types, not the typical Id column with an autonumber and a text column. Before I began development of this gridview I gave the person who needed it the ability to add items directly to the table and in the process, he pasted the Refrigerant type directly in from an Excel spreadsheet and he was adding spaces to the data so when I was looking for values in the code behind I was not returning anything because of the added spaces comparing to the lookup table which has no spaces.
OK now for the issue I am still having. I can populate the combo box with the value from the label using the following code in the RadGrid1_ItemDataBound event

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
         
 If e.Item.IsInEditMode Then
   Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
   Dim combo As RadComboBox = DirectCast(item.FindControl("rcbRefrigerantType"), RadComboBox)
  
            ' Cast the e.Item.DataItem object to its real type so that can ...
            Dim dataItem As DataRowView = TryCast(e.Item.DataItem, DataRowView)
  
            ' ...access the value in the Column where the RadComboBox resides.
            Dim selectedValue As String = dataItem("RefrigerantTypeID").ToString()
            selectedValue = Replace(selectedValue, " ", "")
            ' Bind the ComboBox to make sure its Items are loaded.
            combo.DataBind()
  
            ' Look for the Item with the same value as the one in the current Column.
            Dim itemToSelect As RadComboBoxItem = combo.FindItemByValue(selectedValue)
            ' If such an Item exists...
            If itemToSelect IsNot Nothing Then
                ' ... select it.
                itemToSelect.Selected = True
            End If
        End If
    End Sub
Here is the markup
<telerik:GridTemplateColumn  UniqueName="RefrigerantTypeID" 
            HeaderText="Refr Type" ItemStyle-Width="50px" HeaderStyle-Width="75px" 
            DataField="RefrigerantTypeID">
              
             
           <ItemTemplate>
                <asp:Label ID="lblType" runat="server" Text='<%# Eval("RefrigerantTypeID") %>'></asp:Label>
           </ItemTemplate>
           <EditItemTemplate>
                 
               <telerik:RadComboBox ID="rcbRefrigerantType" AppendDataBoundItems="true"  runat="server" 
                   DataSourceID="sdsRefrigerantTypes" DataTextField="RefrigerantTypeID" 
                   DataValueField="RefrigerantTypeID">
     
              </telerik:RadComboBox>
                    </EditItemTemplate>
            </telerik:GridTemplateColumn>
<asp:SqlDataSource ID="sdsRefrigerantTypes" runat="server" 
       ConnectionString="<%$ ConnectionStrings:AAIOMSConnectionString %>" 
       SelectCommand="spSelectFAC_RefrigerantTypes" 
       SelectCommandType="StoredProcedure"></asp:SqlDataSource>

One thing I don't understand here is if I leave in combo.DataBind() when I select the dropdown I get back all values from all rows in the gridview, not values from the combo sqlDataSource. If I remark out the comboDataBind() I then get the correct data populated in the combobox.

The only thing left to do is to dynamically update the combobox value when the Update command is given. I believe I can handle that in the sqldatasource Updating event and will post that code when I get it to work. In the mean time if anyone has a better solution I am all ears. Thanks.
0
AkAlan
Top achievements
Rank 2
answered on 28 Dec 2010, 02:22 AM
OK, got it all working now. Got part of my solution from this Telerik post Load On Demand 
The only thing I had to change was the line updating the sqlDatasource from type integer to type String since as I have said both my DataTextField and DataValueField are both the same and are of type varchar(1) in the database.


Protected Sub RadGrid1_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) _ Handles  RadGrid1.UpdateCommand
  
Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
Dim comboBox As RadComboBox = DirectCast(editedItem.FindControl("rcbRefrigerantType"), RadComboBox)
sdsRefrigerantCylinders.UpdateParameters.Add(New Parameter("RefrigerantTypeID", DbType.String, comboBox.Text))
sdsRefrigerantCylinders.Update()
  
  
    End Sub
Tags
ComboBox
Asked by
AkAlan
Top achievements
Rank 2
Answers by
AkAlan
Top achievements
Rank 2
Share this question
or