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

GridViewComboBoxColumn Setting SelectedIndex/Value

4 Answers 504 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rick Petersen
Top achievements
Rank 1
Rick Petersen asked on 30 Jul 2009, 07:35 PM
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?













4 Answers, 1 is accepted

Sort by
0
Accepted
gerbrand
Top achievements
Rank 2
answered on 04 Aug 2009, 12:11 PM
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.

0
Rick Petersen
Top achievements
Rank 1
answered on 04 Aug 2009, 01:11 PM
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.
0
gerbrand
Top achievements
Rank 2
answered on 04 Aug 2009, 03:29 PM
No problem :-)

0
Martin Vasilev
Telerik team
answered on 05 Aug 2009, 07:26 AM
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.
Tags
GridView
Asked by
Rick Petersen
Top achievements
Rank 1
Answers by
gerbrand
Top achievements
Rank 2
Rick Petersen
Top achievements
Rank 1
Martin Vasilev
Telerik team
Share this question
or