Need a sample project of these codes!

7 posts, 0 answers
  1. Hamid
    Hamid avatar
    20 posts
    Member since:
    May 2009

    Posted 06 Sep 2012 Link to this post

    Hi everybody
    I confused about the following article.
    http://www.telerik.com/help/winforms/gridview-editors-howto-allow-end-users-to-add-items-to-dropdownlisteditor.html 
    Can someone give me a sample project of these codes?
  2. Nikolay
    Admin
    Nikolay avatar
    1804 posts

    Posted 06 Sep 2012 Link to this post

    Hello Hamid,

    Thank you for writing.

    Generally speaking, we do not provide projects that second documentation articles and other support resources as we strive to make these resources as much helpful on their own as possible without the need of further assistance. Therefore, we would like to know what exactly do you find confusing about this article. We will address the part that you find difficult so that you and the others interested in the topic can find the article more useful.

    Thank you for your cooperation. I am looking forward to your reply.

    Regards,
    Nikolay
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Hamid
    Hamid avatar
    20 posts
    Member since:
    May 2009

    Posted 06 Sep 2012 Link to this post

    Thanks, but I really need a project!
    For Example, in this article it's not mentioned where we must write these parts:
    GridComboBoxCellElement cellElement = this.OwnerElement as GridComboBoxCellElement;
     
    RadGridView grid = cellElement.GridControl;
    AllowEnd_usersAddItemsComboBoxEditor f = (AllowEnd_usersAddItemsComboBoxEditor)grid.FindForm();
     
    // Checking if the typed value exists in the datasource of the column.
    NwindDataSet.CategoriesDataTable dt = f.DataSet.Categories;
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        if (dt.Rows[i]["CategoryName"].ToString() == ((RadDropDownListEditorElement)this.EditorElement).Text)
        {
            return base.EndEdit();
        }
    }
  5. Nikolay
    Admin
    Nikolay avatar
    1804 posts

    Posted 10 Sep 2012 Link to this post

    Hi Hamid,

    Please take a closer look at the following sentences in the documentation article just before the quoted code snippet:
    Now it is time to create our custom editor. For the purposes of our goal, we need to create a class that derives from RadDropDownListEditor and override the EndEdit method. In this method, we first need to check whether the value typed by the user exists or not in the datasource of the GridViewComboBoxColumn. If it exists, we should terminate the execution of the EndEdit method.

    In short, you should create a RadDropDownListEditor descendant class, override the EndEdit method and put the code snippet in question there. I would suggest that you name the descendant class CustomDropDownEditor. We will think of how we can improve the article to make this part more clear to the developer.

    Let me know if you have additional questions.

    Regards,
    Nikolay
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  6. Theo
    Theo avatar
    19 posts
    Member since:
    Sep 2011

    Posted 18 Sep 2012 Link to this post

    Hi Nikolay,

    Svett referred me to this article. Please see my suggestions on how the article can be made more user friendly.
    Thank you

    http://www.telerik.com/account/support-tickets/view-ticket.aspx?threadid=605081 

  7. Nikolay
    Admin
    Nikolay avatar
    1804 posts

    Posted 18 Sep 2012 Link to this post

    Hello Theo,

    Thank you for your feedback.

    I have already taken a look at it and some of the suggestions will be addressed shortly.

    Let us know if you have additional feedback to share.

    Regards,
    Nikolay
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  8. Theo
    Theo avatar
    19 posts
    Member since:
    Sep 2011

    Posted 18 Sep 2012 Link to this post

    Hi Hamid, Please see the attached code on how I implemented a solution for your problem. Hope it helps.

    Scenario, I have a form called Tasks, it has a child grid where you can define risks regarding a task (1 task, many risks). I want my users to be able to use a drop down in the risk grid to, as they type, select an item already in the risks table, but only as look-up/auto-complete. A previous risk must not also be attached to a new task.

    Start off by defining an additional new inherited class within my frmTasks form class file, outside of the frmTasks Class, but in the same file (you can move this out at a later stage, for now it might be easier to have all in the same file while testing.)
    Public Class RiskDropDownEditor
        Inherits RadDropDownListEditor
     
        Public Overrides Function EndEdit() As Boolean
            Dim cellElement As GridComboBoxCellElement = TryCast(Me.OwnerElement, GridComboBoxCellElement)
     
            If cellElement.ColumnInfo.Name = "cboRiskDetail" Then
                ' Checking if the typed value exists in the datasource of the column.
                If frmTasks.CurrentRiskDetailList.Contains((CType(Me.EditorElement, RadDropDownListEditorElement)).Text) Then Return MyBase.EndEdit()
     
                ' We are adding new data in the underlying datasource of the combobox column
                frmTasks.CurrentRiskDetailList.Add((CType(Me.EditorElement, RadDropDownListEditorElement)).Text)
                cellElement.Tag = (CType(Me.EditorElement, RadDropDownListEditorElement)).Text
     
                Return MyBase.EndEdit()
            ElseIf cellElement.ColumnInfo.Name = "cboRiskMitigation" Then
                ' Checking if the typed value exists in the datasource of the column.
                If frmTasks.CurrentRiskMitigationList.Contains((CType(Me.EditorElement, RadDropDownListEditorElement)).Text) Then Return MyBase.EndEdit()
     
                ' We are adding new data in the underlying datasource of the combobox column
                frmTasks.CurrentRiskMitigationList.Add((CType(Me.EditorElement, RadDropDownListEditorElement)).Text)
                cellElement.Tag = (CType(Me.EditorElement, RadDropDownListEditorElement)).Text
     
                Return MyBase.EndEdit()
            End If
            Return MyBase.EndEdit()
        End Function
    End Class
    Notes: 
    CurrentRiskDetailList and CurrentRiskMitigationList are both public shared variables in frmTasks defined as List(Of String). These 2 collections are populated from the database with a list of existing risk desc and mitigation desc

    All of the following code is within the frmTasks form class
    In the class body I define my 2 lookup lists and initialize them with all the existing string desc in the database
    Public Shared CurrentRiskDetailList As List(Of String) = Risk.GetObjectSetRiskDetail
    Public Shared CurrentRiskMitigationList As List(Of String) = Risk.GetObjectSetMitigation

    I use a method to add the 2 comboboxes to the risk child grid
    Public Sub AddCombosToGrid_Risks()
            If IsNothing(dgvRisks.Columns("cboRiskDetail")) Then
                Dim cboRisk As New GridViewComboBoxColumn
                With cboRisk
                    .DataSource = CurrentRiskDetailList
                    .HeaderText = "Detail"
                    .FieldName = "RiskDetail"
                    .Name = "cboRiskDetail"
                    .DropDownStyle = RadDropDownStyle.DropDown
                    .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                End With
                dgvRisks.Columns.Add(cboRisk)
            End If
     
            If IsNothing(dgvRisks.Columns("cboRiskMitigation")) Then
                Dim cboRiskMitigation As New GridViewComboBoxColumn
                With cboRiskMitigation
                    .DataSource = CurrentRiskMitigationList
                    .HeaderText = "Mitigation"
                    .FieldName = "Mitigation"
                    .Name = "cboRiskMitigation"
                    .DropDownStyle = RadDropDownStyle.DropDown
                    .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                End With
                dgvRisks.Columns.Add(cboRiskMitigation)
            End If
    End Sub
    Please Note that the combo datasources are the List(Of String) collections

    Next I implement the EditorRequired event of the Risks grid to use the custom editor class
    Private Sub dgvRisks_EditorRequired(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.EditorRequiredEventArgs) Handles dgvRisks.EditorRequired
        If (dgvRisks.CurrentColumn.Name = "cboRiskDetail") OrElse _
           (dgvRisks.CurrentColumn.Name = "cboRiskMitigation") Then
            If e.EditorType Is GetType(RadDropDownListEditor) Then
                e.Editor = New RiskDropDownEditor()
            End If
        End If
    End Sub

    Finally I implement the risks grid CellEndEdit event. This is needed to updated the selected item (cell display) correctly if you added a new item to the list, otherwise you might end up with a new item in the list, but nothing selected, which make it seem that the new item was not added.
    Private Sub dgvRisks_CellEndEdit(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles dgvRisks.CellEndEdit
        If Not IsNothing(dgvRisks.CurrentRow) Then
            If e.Column.Name = "cboRiskDetail" Then
                If dgvRisks.CurrentCell.Tag IsNot Nothing Then
                    dgvRisks.CurrentCell.Value = dgvRisks.CurrentCell.Tag
                    dgvRisks.CurrentCell.Tag = Nothing
                End If
            ElseIf e.Column.Name = "cboRiskMitigation" Then
                If dgvRisks.CurrentCell.Tag IsNot Nothing Then
                    dgvRisks.CurrentCell.Value = dgvRisks.CurrentCell.Tag
                    dgvRisks.CurrentCell.Tag = Nothing
                End If
            End If
        End If
    End Sub

    EDIT:
    When I reload/refresh my form after saving, you need to re-populate the 2 collections so that the new items are added from the database and the collections are up to date.
    CurrentRiskDetailList = New List(Of String)
    CurrentRiskDetailList = Risk.GetObjectSetRiskDetail
    CurrentRiskMitigationList = New List(Of String)
    CurrentRiskMitigationList = Risk.GetObjectSetMitigation
    Risk.GetObjectSetRiskDetail and Risk.GetObjectSetMitigation are both functions in my BL classes that return a List(of String) using LinqToEntity

    And that should be it, I hope I did not miss anything.
    You did not mention if you are using C# or VB.NET so if you have trouble with the syntax, check out Telerik's code converter. 
    http://converter.telerik.com/ 
    Very cool tool that I use often and (no exaggeration) works 9 out of 10 times. It's only when you start using LINQ and variant datatypes that it sometimes does not correctly convert, which honestly is no biggy.

    Let me know if you got this working and if it helped.
    Happy Coding
    Theo Jacobs
Back to Top
UI for WinForms is Visual Studio 2017 Ready