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

Adding items to a combobox column

5 Answers 729 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jhon s
Top achievements
Rank 1
Jhon s asked on 07 Sep 2008, 11:52 AM
Hi,
I have a GridView control with 3 columns. The first is a text column, the second is a combobox column and the third is a command column.
Basically, all the gridview values are readonly so I don't need to change them.
I have been trying to add items to the gridview through code but I can't find a the way to insert the values into the combobox cell. I'm using the Add method of the Rows collection but I don't know what value to pass to it. (I have been trying different ways). In the documentation there's only an example of strings or numbers that are pass to the Add method.
ANother question in the same subject, how can I initialize my grid from an xml file - I look at this link:
http://www.telerik.com/support/kb/article/b454K-ehc-b454T-bhb-b454c-bhb.aspx
but some of the objects are not recognized - like 'GridBoundColumn'. Is there a

certain format that the xml file should be in, so the values will be set to the right cells


5 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 09 Sep 2008, 10:50 AM
Hello Ilan Hofshi,

Thank you for contacting us.

RadGridView supports XML data sources through the DataSet.ReadXML functionality. So, RadGridView can present any data that can be loaded in a DataSet. You can find more about loading XML data to a DataSet by following this link.

I would suggest creating the columns manually and adding the values to the combo column through code. To do this, you need to set a DataSource containing the desired values to the combo box column. Consider the code snippet below:

this.radGridView1.MasterGridViewTemplate.AutoGenerateColumns = false
 
GridViewComboBoxColumn comboColumn = new GridViewComboBoxColumn("Combo"); 
comboColumn.DataSource = new string [] { "A""B""C" }; 
 
this.radGridView1.Columns.Add(new GridViewTextBoxColumn("Name")); 
this.radGridView1.Columns.Add(comboColumn); 
this.radGridView1.Columns.Add(new GridViewCommandColumn("Command")); 
 
this.radGridView1.DataSource = xmlDataSet; 


I hope this helps. Please do not hesitate to write me if you need further assistance.

Best wishes,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Jhon s
Top achievements
Rank 1
answered on 15 Sep 2008, 03:00 PM
Thank you for your response.

I have some questions regarding the GridView.
1. I didn't mentioned that in every row, the combobox values are different. I tried to use the code that sent but couldn't accomplish it.
2. When I perform a certain change in the GridView and than move to a different node in the TreeView I get a RowNotInTable Exception. In that subject, how can I remove the first row that appears in the gridview which says: "Click here to add a new row" and make sure it will not appear when I clear all the rows from the grid?
3. I want to disable/enable the buttons in the CommandColumn according to certain ruls, however when I try to initialize all the GridView rows and set the Enable property of the buttons in the CommandColumn to True or False I get a NullReferenceException:
GridViewSectionData.Columns(3)c Rows.Item(i - 1).Cells.Item(3).CellElement.ColumnInfo.ReadOnly = True

My last question is about a problem that I had with your UI. At some point in all the GUI disappeared. I restored the GUI but every time I close the application ( the X button), I get a ArgumentException (Parameter is missing) and I just can't find the reasone for it.

Regards,

Avi
0
Accepted
Nikolay
Telerik team
answered on 18 Sep 2008, 02:47 PM
Hello Ilan Hofshi,

Thank you for getting back to us.

Please find the answers to your questions below:
  1. RadGridView does not support assigning different DataSources the the combos in each individual row. However, you can insert RadComboBoxes in the cells and assign different DataSources. In this case, however, the combos will always be visible, no matter whether you are editing a particular cell or not.
     
  2. In order to remove the possibility to add a new row from the UI, you should set:
    this.radGridView1.MasterGridViewTemplate.AllowAddNewRow = false
    Of course, this could be done through the property grid of Visual Studio at design-time.
     
  3. As to the buttons in the GridViewCommandColumn, you should set the Enabled property in the CellFormatting event handler. This is the only place where you can use the CellElement, because it will not be null. Please note that when setting a property in this event handler, you should set the opposite property in the else clause:
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)  
    {  
        if ((e.CellElement.ColumnInfo is GridViewCommandColumn) && (e.CellElement.RowIndex >= 0))  
        {  
            if (e.CellElement.RowIndex % 2 == 0)  
            {  
                e.CellElement.Children[0].Enabled = false;  
            }  
            else 
            {  
                e.CellElement.Children[0].Enabled = true;  
            }  
        }  
  4. I was not able to reproduce the issue described. Please check whether it persists in our latest Q2 2008 SP1 version, and if so, please open a new support ticket and send me a sample project which I will analyze further. This will allow me to give you more accurate help.

If you have additional questions, feel free to contact me.

Regards,

Nikolay
the Telerik team


Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Jhon s
Top achievements
Rank 1
answered on 21 Sep 2008, 03:12 PM
Thank you for your response.

1.Regarding the problem with the UI, I don't really know what can I send you since the project is confidential. Maybe I'll try to reconstruct the whole UI again.
2. Regarding the multi comboboxes in the GridView. I tried to use a RadComboBox in each cell in the ComboColumn by creating a different RadCombobox for each row and set it's DataSource property with the strings that I need. Than I assign the combobox as one of the parameters in the "Add" method of the "Rows" collection. However,  the values do not appear inside the comboboxes but only the name of the control i.e RadCombobox. Should I use the DataSource property in a certain place in the application?

3. There's a question in my previous question that was left unanswered.
I'll repeat it. I have a treecontrol with a gridview connected to it.
When I move from one node to another, the gridview changes accordinatly.
The problem that I have is when I edit a certain row in the gridview and then change to a different node I get a "RowNotInTableException" "This row has been removed..."
This same situation happens - in a different way - in a another place in the application when I move from a different node to a another node and build the grid again by comparing a value from the gridview that should be a "string" but get a DBNull for some reason.


Regards,

Avi

0
Nikolay
Telerik team
answered on 26 Sep 2008, 05:55 PM
Hi Ilan Hofshi,

Please excuse us for the delayed response.

Since your project is confidential, we are ready to sign a Non-Disclosure Agreement (NDA). This will ensure the privacy of your project and you will be able to send it further. This is the only option which will allow us to investigate the case in detail in order to address any potential issue.

Regarding the approach with different DataSources for the comboboxes in RadGridView, I have prepared a sample application which demonstrates how this should be done. Generally, the concept is that you have predefined int values in the cells of the column filled with comboboxes, and these comboboxes take their SelectedIndex from the values:
  1. You should create your own GridDataCellElement descendant of the respective this class.
  2. The DataSources should be set in the custom GridDataCellElement.
  3. Then, you add and use an existing type of column which will serve as a base pad on which to show the comboboxes.
  4. Next, on the CreateCell event, if the column is the base pad column, you should return your own GridDataCellElement.
  5. Subscribe to the CellFormatting event handler, where the SelectedIndex of the combo will be taken from the Value of the respective CellElement. The SelectedIndex will be taken from the Value in the custom GridDataCellElement. However, this should be done in the CellFormatting as well.

In the example there are two sample DataSources containing Lists with different numbers.

As to the issue concerning RadTreeView and RadGridView, I was not able to reproduce it. I made a sample project with RadTreeView and RadGridView bound to one and the same DataSource. Then I selected a node from the RadTreeView, which made a selection in the RadGridView. Next, I changed a value of a cell in RadGridView and then selected another node in RadTreeView. Everything went as expected and the described issue did not appear. Therefore, please send me a sample project which reproduces the incorrect behavior. This will allow me to investigate the case further.

I am looking forward to your response.

 
Greetings,
Nikolay
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
Jhon s
Top achievements
Rank 1
Answers by
Jack
Telerik team
Jhon s
Top achievements
Rank 1
Nikolay
Telerik team
Share this question
or