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

Combobox in insert/edit mode

4 Answers 74 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Cecilie Nordbø
Top achievements
Rank 1
Cecilie Nordbø asked on 05 Aug 2010, 07:14 PM
Hi.
I have a grid with a GridTemplateColumn containing a combobox. I want the combobox to only be available when the grid is in insert mode. I use the following code:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
 {     
    if (e.Item.OwnerTableView.IsItemInserted && e.Item is GridDataInsertItem)
    {
        var column =
(GridTemplateColumn)e.Item.OwnerTableView.GetColumnSafe("UserNumber");
         column.ReadOnly = false;
   }
             
   if (!(e.Item is GridDataInsertItem) && e.Item.IsInEditMode)
   {
         var column = (GridTemplateColumn)e.Item.OwnerTableView.GetColumnSafe("UserNumber");
         column.ReadOnly = true;
   }           
}

But the problem is that sometimes the combobox is available in edit mode and othertimes is not, also the same for insert mode. Somebody who knows what i am doing wrong? I assume the if-test maybe is wrong...

The code for the grid only the columns:
<Columns>      
   <telerik:GridTemplateColumn UniqueName="UserNumber" DataField="UserNumber" HeaderText="Bruker" SortExpression="UserNumber">                   
   <ItemTemplate><%#DataBinder.Eval(Container.DataItem,"UserNumber")%></ItemTemplate>
   <EditItemTemplate>
  <telerik:RadComboBox DataTextField="UserNumber" DataValueField="UserNumber" EnableLoadOnDemand="True" ID="RadComboBox1"   HighlightTemplatedItems="true"
                     runat="server" SelectedValue='<%#Bind("UserNumber") %>' DataSourceID="MtUserLinqDataSource">
  <ItemTemplate>                        
    <%# DataBinder.Eval(Container.DataItem, "UserNumber") %> 
    <%# DataBinder.Eval(Container.DataItem, "FirstName") %> 
    <%# DataBinder.Eval(Container.DataItem, "LastName")%>                        
  </ItemTemplate>
 </telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>   
<telerik:GridNumericColumn DataField="Account" HeaderText="Reskontro"  UniqueName="Account"></telerik:GridNumericColumn>
<telerik:GridEditCommandColumn CancelText="Avbryt" EditText="Endre" InsertText="Sett inn" UpdateText="Oppdater"></telerik:GridEditCommandColumn>     
</Columns>



Ceci

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 06 Aug 2010, 06:57 AM
Hello,

Access the RadComboBox control in editform/insertform and set the Enabled property based on condition and see whether it is working as expected.

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
      if (e.Item.OwnerTableView.IsItemInserted && e.Item is GridDataInsertItem)
       {
          RadComboBox combo = (RadComboBox)e.Item.FindControl("RadComboBox1");
          combo.Enabled = false;
       }
      if (!(e.Item is GridDataInsertItem) && e.Item.IsInEditMode)
       {
          RadComboBox combo = (RadComboBox)e.Item.FindControl("RadComboBox1");
          combo.Enabled = true;
       }          
    }

Hope this helps,
Princy.
0
Cecilie Nordbø
Top achievements
Rank 1
answered on 06 Aug 2010, 09:25 AM
It's work but now in edit mode you can see a disabled combobox and in edit mode I want this combobox to look like when it is in none mode. Is there a way to do that?
0
Accepted
Carlos Contreras
Top achievements
Rank 1
answered on 06 Aug 2010, 07:42 PM
Hi Ceci!

I think I understand perfectly your scenario. This is the way I do what you want to achieve:

protected void HideRow(bool Hidden, string FieldName) 
   (RadGrid1.MasterTableView.GetColumn(FieldName) as GridBoundColumn).ReadOnly = Hidden; 
   RadGrid1.MasterTableView.Rebind(); 
    
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
   if (e.CommandName == "InitInsert") HideRow(false, "ID_Col"); 
   if (e.CommandName == "Edit") HideRow(true, "ID_Col"); 
    
Note: FieldName should be your Column's UniqueName, so in your case instead of "ID_Col" replace it for "UserNumber"

Please, let me know if that helped you!


Good Luck!
0
Cecilie Nordbø
Top achievements
Rank 1
answered on 09 Aug 2010, 11:19 AM
Thanks a lot, it works perfectly !
Tags
Grid
Asked by
Cecilie Nordbø
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Cecilie Nordbø
Top achievements
Rank 1
Carlos Contreras
Top achievements
Rank 1
Share this question
or