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

radGrid - Add mode vs edit mode - codebehind

1 Answer 183 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Randy Bauer
Top achievements
Rank 1
Randy Bauer asked on 29 Dec 2009, 05:13 PM

I have a radGrid with Add a Record turned on and the edit is turned on for each row.  How do I validate in the code behind if the user selected Add a Record or Edit an Existing?

I have a drop down list in the Insert and Edit modes.  If the user selects add a new record, I want to populate the drop dwon list with all the values and the first one should be selected.  If the user selects edit a record, I want the current value to display.  I am getting the latter one to work.

Protected Sub RadGrid1_EditCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.EditCommand  
        editSelected = True 
 
        Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)  
        Dim statusLabel As Label  
        statusLabel = item.FindControl("lblStatus")  
        Session("updatedStatusValue") = statusLabel.Text.ToString()  
 
        Dim priorityLabel As Label  
        priorityLabel = item.FindControl("lblPriorityDesc")  
        Session("updatedPriorityValue") = priorityLabel.Text.ToString()  
 
    End Sub  
 
    Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound  
 
        If "matter".Equals(e.Item.OwnerTableView.Name) Then  
            If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then  
                Dim item As GridEditableItem = e.Item  
 
                Dim ddlStatusList As DropDownList = item.FindControl("ddlStatus")  
                ddlStatusList.DataSource = GetStatusList()  
                ddlStatusList.DataBind()  
 
                If (Not Session("updatedStatusValue") Is Nothing) Or editSelected Then  
                    For Each ddlItems As ListItem In ddlStatusList.Items  
                        If ddlItems.Text = Session("updatedStatusValue") And editSelected Then  
                            ddlStatusList.SelectedValue = ddlItems.Value  
                        End If  
                    Next  
                Else  
                    ddlStatusList.SelectedValue = 1 
                End If  
 
                Dim ddlPriorityList As DropDownList = item.FindControl("ddlPriorityDesc")  
                ddlPriorityList.DataSource = GetPriorityList()  
                ddlPriorityList.DataBind()  
 
                If (Not Session("updatedPriorityValue") Is Nothing) Or editSelected Then  
                    For Each ddlItems As ListItem In ddlPriorityList.Items  
                        If ddlItems.Text = Session("updatedPriorityValue") And editSelected Then  
                            ddlPriorityList.SelectedValue = ddlItems.Value  
                        End If  
                    Next  
                Else  
                    ddlPriorityList.SelectedValue = 1 
                End If  
 
            End If  
        End If  
    End Sub 

Thanks,

Randy

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 30 Dec 2009, 04:42 AM
Hello Randy,

You can use the following code to access the dropdownlist when the grid is in Insert mode and customize it:
aspx:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted) 
        { 
            GridEditFormInsertItem editItem = (GridEditFormInsertItem)e.Item; 
            DropDownList ddlStatusList = (DropDownList)editItem.FindControl("ddlStatus"); 
            // populate the control and set the selected item             
        } 
    } 

Hope this helps..
Princy.
Tags
Grid
Asked by
Randy Bauer
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or