dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Datagridview - customizing combobox

  1. #1
    Join Date
    Oct 2009
    Location
    Harrisburg, PA
    Posts
    23

    Datagridview - customizing combobox

    I need a combobox in my datagridview which will meet the these two requirements:

    1) The data source of the combobox will depend on the underlying business object that is bound to the row of the combobox. So this means that the combobox of every row will have a different datasource. Everytime the combobox is entered the datasource would need to be recalculated.

    2) I would like the autocomplete to behave as a normal combobox in "suggest" mode, where a list of choices appears as the user types in text.

    My first thought was to extend the DataGridViewTextboxColumn and related class to use a combobox as the template cell. I Like this way but I'm not quite sure how I could get the combobox to fetch a datasource from the underlying business object

    I was also thinking that there are properties of the combobox column and/or ways to hack the comboboxcolumn to behave the way I want it to. I like this way because in my research so far I have seen alot of people that have created their own custom columns that always seem to only work about 95% of the way!

    Please point me in the right direction - thanks

  2. #2
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Datagridview - customizing combobox

    Have you considered using Reflector to look at the way DataGridViewComboBox works?

    Note, if you do this and do a good one, let me know because it's contantly a pain in my arse getting cascading combos in datagridviews; all combos refer to one BindingSource that changes its filter based on the parental cascade on the current row, which then screws all the other combos because they cannot find their data any more..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  3. #3
    Join Date
    Oct 2009
    Location
    Harrisburg, PA
    Posts
    23

    Re: Datagridview - customizing combobox

    I never ended up creating my own combobox - for time constraint reasons.

    However
    I did come up with a solution for giving each row's combobox a custom "datasource"

    Pretty simple just do something like this for the datagridview's editingcontrolshowingevent:

    private void dgViewFinished_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
    if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
    {
    DataGridViewComboBoxEditingControl ctr = (DataGridViewComboBoxEditingControl) e.Control;
    ctr.Items.Clear();
    //add own list of items right here
    // this will not effect the other rows at all
    }

    }

  4. #4
    Join Date
    Oct 2009
    Location
    Harrisburg, PA
    Posts
    23

    Re: Datagridview - customizing combobox

    I guess I'll point out that in my example the custom list of items I am using is simply a subset of the datasource of the combobox column. If you add items to the combobox that are not part of the datasource I think you will get a data error.

    In my application the combobox allows the user to select a partnumber. I wanted to restrict the list of available part numbers to those that weren't used already. So when the editingcontrolshowing event fires I set the list of parts to a filtered subset of the original datasource of the column.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)