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

Nullable FKs with combobox columns

5 Answers 67 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Riccardo
Top achievements
Rank 1
Riccardo asked on 22 May 2008, 11:12 AM
Hi,

from the release notes of Q1 2008 SP1 there's a line:

"Added support for null value in ComboBoxColumn"

Now, how would be this accomplished? Is this a way to add an extra-item to the dropdown like EmptyListDataItem and the likes for WebGrid?
Is it actually documented in some example code somewhere?

So far we've only succeeded in getting a DBNull written to the DataRow (through a regular BindingSource) from the autogenerated GridViewDecimalColumn with an empty value, but not using the GridViewComboBoxColumn.

5 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 22 May 2008, 01:31 PM
Hi Riccardo,

Thank you for writing.

You can add an item in the GridViewComboBoxColumn with a ValueMember with a value of null and DisplayMember set to "None". This scenario is more applicable when binding the RadGridView to a custom business object with Nullable type of properties.

The RadGridView currently supports null and DBNull values for its columns and there is no problem for it to work with DataRow and DBNull values for the decimal and combobox columns.

I hope this helps. If you have additional questions, please contact me.

Kind regards,
Julian Benkov
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Riccardo
Top achievements
Rank 1
answered on 22 May 2008, 03:38 PM

Hi,

I had hoped for an embedded support for that, however not a big issue, we can derive a custom column for that (our db has dozens of nullable FKs and all of them need a combobox, so this is a key feature).
The problem is: how to actually add the item? There is no documentation nor forum post out there that we could find and our tries using:

radGridView.MasterGridViewTemplate.Columns["mycolumn"].GetDefaultEditor()

and then messing with it (adding a RadItem) were fruitless. It didn't get added.
I also have no idea how to actully add the element to the colum itself, since it doesn't seem to expose a collection of additional items.
Which event should we handle and how?

(some sample code or a link to a KB article covering this would be fine)

 

0
Accepted
Georgi
Telerik team
answered on 23 May 2008, 03:21 PM
Hi Riccardo,

Indeed, we recognize that currently RadGridView documentation is not complete. We are working on improving the documentation to cover such cases.

I'm not sure what exactly is your scenario and whether you should use GridViewLookUpColumn or GridViewComboBoxColumn. However, in both cases you could add your additional item in the event handler for the RadGridView.CellEditorInitialized event. This event is fired when the editing operation begins and after the editor is initialized and made visible. So, in the event handler you could retrieve the current instance of RadComboBoxEditor and add the item.

Please use the code below as a guideline to adapt to your specific scenario:

    private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)  
    {  
        if (e.ColumnIndex == radGridView1.Columns["column1"].Index)  
        {  
            RadComboBoxEditor editor = radGridView1.ActiveEditor as RadComboBoxEditor;  
            for (int i = 0; i < editor.Items.Count - 1; i++)  
            {                     
                if (editor.Items[i].Text == "None")  
                {  
                    return;
                }  
            }  
            editor.Items.Insert(0, new RadComboBoxItem("None"null));  
        }  
    } 

The check whether the item already exists is necessary here, because the same instance of the editor is reused internally in the editor mechanism.

Please, try this solution and tell us whether it works for you.

Note: The IEditorProvider.GetDefaultEditor() method is used internally and is not applicable to your case since it returns an instance of RadComboBoxEditor different from the one actually displayed. It is intended to be overridden in the derived classes.

Best wishes,
Georgi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Riccardo
Top achievements
Rank 1
answered on 27 May 2008, 08:03 AM
Thanks, the code provided works just fine.

If I might, I'd place a feature request for the "EmptyListItem*" mechanism from Prometheus. Hope you consider it for future releases.

Thanks again,
Riccardo
0
Georgi
Telerik team
answered on 28 May 2008, 11:46 AM
Hello Riccardo,

Thank you for your request.

In fact, we have already logged this feature in our TODO list, and most probably we'll implement it in a releases, coming in the near future.

If you have other questions, please contact us.

Best wishes,
Georgi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
GridView
Asked by
Riccardo
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Riccardo
Top achievements
Rank 1
Georgi
Telerik team
Share this question
or