Re: Propertygrig again and combobox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc Gravell

    Re: Propertygrig again and combobox

    Sure; just set the DataSource, DisplayMember and ValueMember of a
    DataGridViewCom boBoxColumn (below [using C# 3 for brevity]).

    Marc

    using System.Collecti ons.Generic;
    using System.Windows. Forms;
    using System.Componen tModel;
    using System;

    class Role
    {
    public string Name { get; set; }
    public string Description { get; set; }
    }
    class User
    {
    public string Name { get; set; }
    public string Role { get; set; }
    }
    static class Program
    {
    [STAThread]
    static void Main()
    {
    List<Roleroles = new List<Role>
    {
    new Role {Name="ADMIN", Description="Ad ministrator"},
    new Role {Name="GUEST", Description="Gu est"},
    new Role {Name="USER", Description="St andard User"}
    };
    BindingList<Use rusers = new BindingList<Use r>
    {
    new User {Name = "Fred", Role="ADMIN"},
    new User {Name="Jo", Role="GUEST"}
    };
    Application.Run (new Form
    {
    Text = "DataGridVi ew Demo",
    Controls =
    {
    new DataGridView {
    Dock = DockStyle.Fill,
    AutoGenerateCol umns = false,
    Columns = {
    new DataGridViewTex tBoxColumn {
    DataPropertyNam e = "Name",
    HeaderText = "Name"
    }, new DataGridViewCom boBoxColumn {
    DataPropertyNam e = "Role",
    DataSource = roles,
    DisplayMember = "Descriptio n",
    ValueMember = "Name"
    }
    },
    DataSource = users
    }
    }
    });

    }
    }
  • Dobedani

    #2
    Re: Propertygrig again and combobox

    Hi Marc and others,

    Thanks for your example. I often work with lists - mostly List<String>
    or a C# implmentation of TStringList from CodeGear ;-) If I want to
    add the items to a ComboBox or similar Control, I approach the Items
    collection thereof. My colleagues do the same. Yesterday somebody told
    me that this is the way things were done in the Stone Age. He instead
    assigned a locally defined list to the DataSource of the ComboBox to
    be populated. It was not even a list, but an array of strings:
    String[] names; and ok, it worked ...

    However, things did not work as I expected. This morning I promoted
    the list to a private field and changed the type to List<String>; i.e.
    I made the list visible within the whole class. I then added a method
    to modify this list. I was then expecting that the ComboBox would be
    updated when the list would change. Not so. I tried all sorts of
    things - incl. wrapping the strings and assigning DisplayMember and
    ValueMember of the ComboBox - but to no avail.
    Unfortunately, I was not told that I should use BindingList! Now I
    understand things better. I am adding the following example for
    whoever may have the same problems. Start a new project and add the
    code to class Form1.cs with a combobox named matrixComboBox and a
    button named btnRemove.

    One question: why does the DataSourceChang ed event not fire when I
    remove an Item?

    Kind regards,
    Dobedani

    namespace DataSrcTest {
    public partial class Form1 : Form {
    private BindingList<Str ingmatrixNames;

    public Form1() {
    InitializeCompo nent();
    PopulateList();
    }

    private void PopulateList() {
    matrixNames = new BindingList<Str ing>();
    String[] names = { "Matt", "Luke", "Marc", "John", "Paul",
    "Pete", "Jude" };
    foreach (String s in names) {
    matrixNames.Add (s);
    }
    matrixComboBox. DataSource = matrixNames;
    }

    private void btnRemove_Click (object sender, EventArgs e) {

    String selectedWS =
    matrixComboBox. SelectedItem.To String();
    if (matrixNames.Co ntains(selected WS)) {
    matrixNames.Rem ove(selectedWS) ;
    }
    if (matrixNames.Co unt == 0) {
    MessageBox.Show ("I hope you read the New Testament!");
    }
    }
    }
    }

    Comment

    • Marc Gravell

      #3
      Re: Propertygrig again and combobox

      Works fine for me...

      static class Program
      {
      [STAThread]
      static void Main()
      {
      string[] names= {"Matt", "Luke", "Marc",
      "John", "Paul", "Pete", "Jude"};
      BindingList<str inglist = new BindingList<str ing>();
      foreach (string name in names)
      {
      list.Add(name);
      }
      ComboBox cb = new ComboBox { DataSource = list, Dock =
      DockStyle.Top };
      cb.DropDownStyl e = ComboBoxStyle.D ropDownList;
      Button btn = new Button { Dock = DockStyle.Botto m, Text
      = "Remove" };
      btn.Click += delegate
      {
      list.Remove((st ring)cb.Selecte dItem);
      };
      Application.Ena bleVisualStyles ();
      Application.Run (new Form { Controls = { cb, btn } });
      }
      }

      Comment

      • Marc Gravell

        #4
        Re: Propertygrig again and combobox

        I've tried with your code, and the drop-down definitely changes to match
        the data.

        For info, DataSourceChang ed isn't firing because we aren't actually
        changing the DataSource property - rather something inside the
        data-source is changing. If you want to watch those changes, use a
        BindingSource as an intermediary:

        BindingSource bs = new BindingSource() ;
        bs.DataSource = matrixNames;
        bs.ListChanged += delegate(object sender,
        ListChangedEven tArgs args)
        {
        this.Text = string.Format(" {0}: {1}",
        args.NewIndex, args.ListChange dType);
        };
        matrixComboBox. DataSource = bs;

        Marc

        Comment

        Working...