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

Dropdrownlist in edit mode based on cell value

10 Answers 84 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Norway77
Top achievements
Rank 1
Norway77 asked on 01 Mar 2015, 04:09 PM
Hi
i am new to telerik  and asp.net but have been using the examples on the site to make rad grids and am able to edit and insert items.


I have radgrid and there is a column named status.  I want to add a different dropdown based on the value during inline edit mode

so if value in the rad grid cell is "drop". The list is  
drop
add
remove

if value is "taken" then the list is
taken
remove 
keep

so I need a different drop down for the same column depending on the value of the cell. 

Also so if the cell is a different value "waiting" then I don't want to allow editing at all 

thanks

10 Answers, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 04 Mar 2015, 04:58 PM
Hello,

You can replace the contents of the dropdown in the ItemDataBound event of the grid where you can databind and populate the combo with the preferred values. Additional information is also available in the following help topic.
The editing can be cancelled in the ItemCommand event of the control where you can check the value of the Status cell and prevent the control from going in edit mode.

I am also attaching a sample page showing the above approaches.

Regards,
Marin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Norway77
Top achievements
Rank 1
answered on 04 Mar 2015, 06:18 PM
Thanks Marin
I know you set it up with an array for the data(took be a bit to figure it out) but it doesn't compile.
I keep getting 
the name 'RadGrid1' does not exist in the current context  line 13 asp.cs

Do i have the wrong version of something.   I have the current telerik

I also tried to get it working with my database for the dropdowns but could not.  So I wanted to get back to your original file and at least see it work

thanks
0
Norway77
Top achievements
Rank 1
answered on 05 Mar 2015, 01:13 AM
Ok, so far I have this part working.
protected void RadGrid1_OnItemDataBound(object sender, GridItemEventArgs e)
       {
 
 
 
           if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode)
           {
               
              
                 //  GridEditableItem edititem = (GridEditableItem)e.Item;
                 //  DropDownList ddl = (DropDownList)edititem["Dropstatus"].FindControl("DropStatusddl");
               GridEditableItem item = e.Item as GridEditableItem;
               RadComboBox cmbCountry = item.FindControl("DropStatusddl") as RadComboBox;
             //  RadComboBox cmbState = item.FindControl("cmbState") as RadComboBox;
               dynamic data = new[] {
                       new { CountryID = 1, CountryName ="INDIA"},
                       new { CountryID = 2, CountryName = "US"},
                       new { CountryID = 3, CountryName = "UK"}
                   };
 
               cmbCountry.DataSource = data;
               cmbCountry.DataTextField = "CountryName";
               cmbCountry.DataValueField = "CountryID";
               cmbCountry.DataBind();
                
           }
here is the grid column in rad grid
<telerik:GridBoundColumn DataField="swapped" DataType="System.Int32" FilterControlAltText="Filter swapped column" HeaderText="swapped" SortExpression="swapped" UniqueName="swapped" >
    <ColumnValidationSettings>
        <ModelErrorMessage Text="" />
    </ColumnValidationSettings>
</telerik:GridBoundColumn>

 
                  
<
telerik:GridTemplateColumn HeaderText="dropstatus" UniqueName="dropstatus" ItemStyle-Width="140px">
                        
                      <ItemTemplate>
                          <%#DataBinder.Eval(Container.DataItem, "Dropstatus")%>
                      </ItemTemplate>
                      <EditItemTemplate>
                           
                                    
                          <telerik:RadComboBox ID="DropStatusddl" runat="server"></telerik:RadComboBox>
                              </EditItemTemplate>
                   
                  </telerik:GridTemplateColumn>

here is the update 
<asp:SqlDataSource ID="ShuffleResultsSql" runat="server" ConnectionString="<%$ ConnectionStrings:theDrawConnectionString %>"
          
            UpdateCommand="UPDATE dbSetUp  SET dropStatus = @CountryName , swapped = @swapped WHERE UID = @UID">
         <UpdateParameters>
               <asp:Parameter Name="UID" Type="Int64" />
               <asp:Parameter Name="dropstatus" Type="String"  />
                 <asp:Parameter Name="swapped" Type="Int32" />
           </UpdateParameters>  
 
       </asp:SqlDataSource>
 but i can't get the value to update the database.  
swapped updates fine.
but i tried all sorts of ways to try and get the value into the 
 <asp:Parameter Name="CountryName" Type="String"  />
but i can't seem to make it work.

I will worry the other logic later. I need to get the edit to work first.

thanks
0
Marin
Telerik team
answered on 05 Mar 2015, 08:54 AM
Hi,

You can check out the following online demo showing automatic CRUD operations for the grid with a DataSource control:
http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/automatic-crud-operations/defaultcs.aspx
Basically you have to set AllowAutomaticUpdates to true in order to enable the grid to populate the SqlDatasource parameters automatically.
More information can also be found in the following help topic:
http://www.telerik.com/help/aspnet-ajax/grid-automatic-datasource-operations.html

Regards,
Marin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Norway77
Top achievements
Rank 1
answered on 05 Mar 2015, 02:21 PM
Marin
 I am using the auto update.  
If you look at my code the swapped field updates fine.  It's the drop down fields that will not update.  The value is always blank even though it shows the correct drop down.   How do I get that drop down value into the auto update parameter for the sql update.  
Can you make me one that's works.   I really need to get this working.  
0
Norway77
Top achievements
Rank 1
answered on 05 Mar 2015, 02:22 PM
I was also never able to get your example code to run 
0
Accepted
Marin
Telerik team
answered on 05 Mar 2015, 04:17 PM
Hello,

I see that in your case you are using GridTemplateColumn - which can contain any kind of controls inside its EditItemTemplate that's why the automatic CRUD operations of the control do work for this type of column. Another options is to use the GridDropDown column with specified DataField property which will be mapped to the DataSource parameter.
The case with performing update operations in GridTemplate column can be handled manually by handling the UpdateCommand of the control, extracting the edit values and then saving them to the database (in this case AllowAutomaticUpdates should be set to false). More information on this approach is also available in these help topics:
http://www.telerik.com/help/aspnet-ajax/grid-insert-update-delete-at-database-level.html
http://www.telerik.com/help/aspnet-ajax/grid-updating-values-usercontrol-formtemplate.html

You can easily run the example code from my previous post by creating a new page with a RadGrid in your project, referencing the Telerik assemblies if they are not already referenced and then copy and paste there the code from the page I have sent.

Regards,
Marin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Norway77
Top achievements
Rank 1
answered on 12 Mar 2015, 06:54 PM
Thanks Marin
I got it to work.
I have one more quick question along these lines.

I have column in this same grid that displays a 1 or 0 or blank
 <telerik:GridBoundColumn DataField="swapped" DataType="System.Int32" FilterControlAltText="Filter swapped column" HeaderText="swapped" SortExpression="swapped" UniqueName="swapped" ReadOnly="True" >
                        <ColumnValidationSettings>
                            <ModelErrorMessage Text="" />
                        </ColumnValidationSettings>
                    </telerik:GridBoundColumn>
In codebehind I want to check to see if it is a 1 and if it is I want to put a small gif in the grid instead of the text.
so columns will be blank if not a 1 and if a 1 they show an image.

How can i go about adding an image to this field in the row.

best
danny




0
Accepted
Marin
Telerik team
answered on 17 Mar 2015, 09:52 AM
Hi,

You can use the ItemDataBound event of the control to check the value of the swapped column for each row and add an image accordingly:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            var dataItem = e.Item as GridDataItem;
            if (dataItem["swapped"].Text == "1")
            {
                var image = new Image { ID = "....", ImageUrl="...." };
                dataItem["swapped"].Controls.Add(image);
            }
        }
    }



Regards,
Marin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Norway77
Top achievements
Rank 1
answered on 18 Mar 2015, 08:12 PM
Thanks Marin

worked great
Tags
Grid
Asked by
Norway77
Top achievements
Rank 1
Answers by
Marin
Telerik team
Norway77
Top achievements
Rank 1
Share this question
or