Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8806207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:04:29+00:00 2026-06-14T02:04:29+00:00

In the demo of the ObjectListView control there is this code (in the Complex

  • 0

In the demo of the ObjectListView control there is this code (in the “Complex Example” tab page) to allow for a custom editor (a ComboBox) (Adapted to my case and edited for clarity):

EventHandler CurrentEH;
private void ObjectListView_CellEditStarting(object sender,
                                             CellEditEventArgs e)
{
    if (e.Column == SomeCol)
    {
        ISomeInterface M = (e.RowObject as ObjectListView1Row).SomeObject; //(1)
        ComboBox cb = new ComboBox();
        cb.Bounds = e.CellBounds;
        cb.DropDownStyle = ComboBoxStyle.DropDownList;
        cb.DataSource = ISomeOtherObjectCollection;
        cb.DisplayMember = "propertyName";
        cb.DataBindings.Add("SelectedItem", 
                            M, "ISomeOtherObject", false,
                            DataSourceUpdateMode.Never);
        e.Control = cb;
        cb.SelectedIndexChanged += 
            CurrentEH = (object sender2, EventArgs e2) =>
                M.ISomeOtherObject = 
                    (ISomeOtherObject)((ComboBox)sender2).SelectedValue;   //(2)
    }
}

private void ObjectListView_CellEditFinishing(object sender,
                                               CellEditEventArgs e)
{
    if (e.Column == SomeCol)
    {
        // Stop listening for change events
        ((ComboBox)e.Control).SelectedIndexChanged -= CurrentEH;
        // Any updating will have been down in the SelectedIndexChanged
        // event handler.
        // Here we simply make the list redraw the involved ListViewItem
        ((ObjectListView)sender).RefreshItem(e.ListViewItem);
        // We have updated the model object, so we cancel the auto update
        e.Cancel = true;
    }
}

I have too many other columns with combo editors inside objectlistviews to use a copy& paste strategy (besides, copy&paste is a serious source of bugs), so I tried to parameterize the code to keep the code duplication to a minimum. ObjectListView_CellEditFinishing is a piece of cake:

HashSet<OLVColumn> cbColumns = new HashSet<OLVColumn> (new OLVColumn[] { SomeCol, SomeCol2, ...};

private void ObjectListView_CellEditFinishing(object sender,
                                               CellEditEventArgs e)
{
    if (cbColumns.Contains(e.Column)) ...

but ObjectListView_CellEditStarting is the problematic.

I guess in CellEditStarting I will have to discriminate each case separately:

private void ObjectListView_CellEditStarting(object sender,
                                             CellEditEventArgs e)
{
    if (e.Column == SomeCol)
        // code to create the combo, put the correct list as the datasource, etc.
    else if (e.Column == SomeOtherCol)  
        // code to create the combo, put the correct list as the datasource, etc.

And so on.

But how can I parameterize the “code to create the combo, put the correct list as the datasource, etc.”? Problem lines are

(1) Get SomeObject. the property NAME varies.

(2) Set ISomeOtherObject, the property name varies too.

The types vary too, but I can cover those cases with a generic method combined with a not so “typesafe” API (for instance, the cb.DataBindings.Add and cb.DataSource both use an object)

Reflection? more lambdas? Any ideas? Any other way to do the same?

PS: I want to be able to do something like this:

private void ObjectListView_CellEditStarting(object sender,
                                             CellEditEventArgs e)
{
    if (e.Column == SomeCol)
        SetUpCombo<ISomeInterface>(ISomeOtherObjectCollection,
                                   "propertyName",
                                   SomeObject,
                                   ISomeOtherObject);

    else if (e.Column == SomeOtherCol)  
        SetUpCombo<ISomeInterface2>(ISomeOtherObject2Collection,
                                   "propertyName2",
                                   SomeObject2
                                   ISomeOtherObject2);

and so on. Or something like that.

I know, parameters SomeObject and ISomeOtherObject are not real parameters per see, but you get the idea of what I want. I want not to repeat the same code skeleton again and again and again.

One solution would be “preprocessor generics” like C’s DEFINE, but I don’t thing c# has something like that.

So, does anyone have some alternate ideas to solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:04:30+00:00Added an answer on June 14, 2026 at 2:04 am

    Found it. Kudos to Tejs!

    private void SetUpCombo(CellEditEventArgs e, 
                            object ComboItems, string DisplayMember,
                            object DataSource, string PropertyName,
                            EventHandler evt)
    {
        ComboBox cb = new ComboBox();
        cb.Bounds = e.CellBounds;
        cb.DropDownStyle = ComboBoxStyle.DropDownList;
        cb.DataSource = ComboItems;
        cb.DisplayMember = DisplayMember;
        cb.DataBindings.Add("SelectedItem", DataSource, PropertyName,
                                false, DataSourceUpdateMode.Never);
        e.Control = cb;
        cb.SelectedIndexChanged += CurrentEH = evt;
    }
    

    and the rewritten CellEditStarting:

    private void ObjectListView_CellEditStarting(object sender,
                                                 CellEditEventArgs e)
    {
        if (e.Column == SomeCol)
        {
            ISomeInterface M = (e.RowObject as ObjectListView1Row).SomeObject; 
            SetUpCombo(e, 
                       ISomeOtherObjectCollection,"propertyName",
                       M, "ISomeOtherObject",
                       (sender2, e2) =>
                           M.ISomeOtherObject = 
                               (ISomeOtherObject)((ComboBox)sender2).SelectedValue);
        }
        else if (e.Column == SomeOtherCol)  
        {
            ISomeInterface2 M = (e.RowObject as ObjectListView1Row).SomeObject2; 
            SetUpCombo(e, 
                       ISomeOtherObjectCollection2,"propertyName2",
                       M, "ISomeOtherObject2",
                       (sender2, e2) =>
                           M.ISomeOtherObject2 = 
                               (ISomeOtherObject)((ComboBox)sender2).SelectedValue);
        }
    

    and so on… There are some things I don’t like yet. For instance: the disconnection between M.ISomeOtherObject (outside the method call), “ISomeOtherObject” (the param) and the setting of M.ISomeOtherObject (inside the lambda).

    But, all things considered, it is much much better than copying & pasting the same code over and over again).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://demo.goseamless.co.za/cktest/ - this link contains an example of code with the bug try typing
Demo page . When run in IE 8, this code yields an exception with
This demo code loops 3 times and every time appends text. What I need
Here's the demo code: QList<Custom> L; QVariant v(QVariant::fromValue(l)); QDataStream d; d << v; The
Demo Basically, I'm trying to setup something that looks like this: However, my code
demo http://jsfiddle.net/bRqAR/2/ this code did not work on the internet explorer (check console). How
http://demo.thethemefoundry.com/traction/#post-183 Which wordpress plugin is this, to have post image on the left side
Here is the DEMO HTML CODE as <div id=me></div> <a id=ok href=>OK</a> jquery code:
I want to replace Demo image by code css3 or somthing else. I have
Demo: http://jsfiddle.net/55ucw/1/ In any browser other than Safari or Chrome, I can tab into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.