GridViewComboBoxColumn Setting SelectedIndex/Value

5 posts, 1 answers
  1. Rick Petersen
    Rick Petersen avatar
    13 posts
    Member since:
    Feb 2009

    Posted 30 Jul 2009 Link to this post

    Scenario:

    I have a list of line items for checks to be 'approved' by the CEO.  He can take one of three actions.  He can approve it, he can explicitly deny it, or he can choose to do nothing on it at this point.

    Implementation:

    I have a hierarchical GridView that properly shows everything I need.  I have a single column there with 3 options " ", "approve", "deny", declared as below:

                GridViewComboBoxColumn colApproved = new GridViewComboBoxColumn(); 
                colApproved.FieldAlias = "Approved"
                colApproved.FieldName = "Approved"
                colApproved.HeaderText = "Approved"
                colApproved.DataSource = new string[] { " ", "Approve", "Deny" }; 

    It is then added to the MasterGridViewTemplate.

    All of this is completely fine.  I am able to capture 'ValueChanged' when it is changed and by testing the active editor I can determine that it's a combobox being changed... again, all of this is working and there is no problem.

    Problem:

    However, I have a button that I want to use to approve ALL items in the grid.  So, in the button click, I loop through the rows, and then reference the proper cell, but I cannot figure out how to set the selectedIndex.  I can just set a 'value' which doesn't really do what I want... here's one of the code attempts I've made:

            private void btnApproveAll_Click(object sender, EventArgs e) 
            { 
                foreach(GridViewDataRowInfo row in this.radGridView1.MasterGridViewTemplate.Rows) 
                { 
                    if (((Button)sender).Text == "Approve All") 
                    { 
                        row.Cells["Approved"].Value = 1
                    } 
                    else 
                    { 
                        row.Cells["Approved"].Value = 0
                    } 
                } 
               if (((Button)sender).Text == "Approve All" ) 
               { 
                   ((Button)sender).Text = "UnApprove All" ; 
               } 
               else 
               { 
                   ((Button)sender).Text = "Approve All"
               }  
            }

    That code will literally set the Value to 0 or 1.  I have tried casting the row.Cells["Approved"] as a GridViewComboBoxColum or GridComboBoxCellElement and in both cases it did not work. 

    ((ComboBox)row.Cells["Approved"]).SelectedIndex = 1

    That doesn't work because you cannot convert type 'Telerik.WinControls.UI.GridViewCellInfo' to 'System.Windows.Forms.ComboBox'.

    Surely there is an easy way to do this?













  2. Answer
    gerbrand
    gerbrand avatar
    78 posts
    Member since:
    Mar 2009

    Posted 04 Aug 2009 Link to this post

    Hi,

    Have you tried this:

    private void btnApproveAll_Click(object sender, EventArgs e) 
            { 
                foreach(GridViewDataRowInfo row in this.radGridView1.MasterGridViewTemplate.Rows) 
                { 
                    if (((Button)sender).Text == "Approve All") 
                    { 
                        row.Cells["Approved"].Value = "Approve";
                    } 
                    else 
                    { 
                        row.Cells["Approved"].Value = "Deny";
                    } 
                } 
               if (((Button)sender).Text == "Approve All" ) 
               { 
                   ((Button)sender).Text = "UnApprove All" ; 
               } 
               else 
               { 
                   ((Button)sender).Text = "Approve All"
               }  
            }

    I've got something similar in my application. Only I'm not using a button but I have a combobox in my header of the gridview. when this value is changed all rows with the combobox cell in the gridview are being set to that value and the selectedindex is also okay.

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Rick Petersen
    Rick Petersen avatar
    13 posts
    Member since:
    Feb 2009

    Posted 04 Aug 2009 Link to this post

    Wow, brainfart there.  Thanks man, yeah that did it.  I was so hung up on setting selected value/index that I then became convinced that I had to cast it as a combobox to even set the value.  Thank you for getting me past my own roadblock there.
  5. gerbrand
    gerbrand avatar
    78 posts
    Member since:
    Mar 2009

    Posted 04 Aug 2009 Link to this post

    No problem :-)

  6. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 05 Aug 2009 Link to this post

    Hello guys,

    I am glad that you found a resolution for setting the combo box cell value. I have made a brief look at the code and it seems alright. Do not hesitate to write again if you have any other questions.  

    Best wishes,
    Martin Vasilev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top
UI for WinForms is Visual Studio 2017 Ready