473,378 Members | 1,507 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Re: Propertygrig again and combobox

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

Marc

using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel;
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="Administrator"},
new Role {Name="GUEST", Description="Guest"},
new Role {Name="USER", Description="Standard User"}
};
BindingList<Userusers = new BindingList<User>
{
new User {Name = "Fred", Role="ADMIN"},
new User {Name="Jo", Role="GUEST"}
};
Application.Run(new Form
{
Text = "DataGridView Demo",
Controls =
{
new DataGridView {
Dock = DockStyle.Fill,
AutoGenerateColumns = false,
Columns = {
new DataGridViewTextBoxColumn {
DataPropertyName = "Name",
HeaderText = "Name"
}, new DataGridViewComboBoxColumn {
DataPropertyName = "Role",
DataSource = roles,
DisplayMember = "Description",
ValueMember = "Name"
}
},
DataSource = users
}
}
});

}
}
Jun 27 '08 #1
3 1294
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 DataSourceChanged event not fire when I
remove an Item?

Kind regards,
Dobedani

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

public Form1() {
InitializeComponent();
PopulateList();
}

private void PopulateList() {
matrixNames = new BindingList<String>();
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.ToString();
if (matrixNames.Contains(selectedWS)) {
matrixNames.Remove(selectedWS);
}
if (matrixNames.Count == 0) {
MessageBox.Show("I hope you read the New Testament!");
}
}
}
}
Jun 27 '08 #2
Works fine for me...

static class Program
{
[STAThread]
static void Main()
{
string[] names= {"Matt", "Luke", "Marc",
"John", "Paul", "Pete", "Jude"};
BindingList<stringlist = new BindingList<string>();
foreach (string name in names)
{
list.Add(name);
}
ComboBox cb = new ComboBox { DataSource = list, Dock =
DockStyle.Top };
cb.DropDownStyle = ComboBoxStyle.DropDownList;
Button btn = new Button { Dock = DockStyle.Bottom, Text
= "Remove" };
btn.Click += delegate
{
list.Remove((string)cb.SelectedItem);
};
Application.EnableVisualStyles();
Application.Run(new Form { Controls = { cb, btn } });
}
}
Jun 27 '08 #3
I've tried with your code, and the drop-down definitely changes to match
the data.

For info, DataSourceChanged 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,
ListChangedEventArgs args)
{
this.Text = string.Format("{0}: {1}",
args.NewIndex, args.ListChangedType);
};
matrixComboBox.DataSource = bs;

Marc
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: jdk | last post by:
I have a main form where a students name is selected. After selected the name- the subform populates all criteria related to the students program. I have 3 combo boxes (Which choose a subject...
8
by: Zlatko Matić | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the...
3
by: Susan Bricker | last post by:
Greetings. I have three forms that are open at the same time. They are related and cascading. The first form (frmEventAdd) is the anchor. Each event can have many Trials. The second form is...
1
by: Richard Hallgren | last post by:
Hi, In Windows Forms the usual approach to add a combobox in a datagrid involves adding a single combobox to the DataGrid.Controls, and then selectively displaying it as needed when a...
3
by: TT (Tom Tempelaere) | last post by:
Hay there, I'm writing my own DataGridComboBoxColumn because .NET 1.1 does not have one (I hope .NET 2.0 supplies one). I based it on this article:...
2
by: excelleinc.com | last post by:
Hi all, This is very frustrated, writing a code and every few days wasting couple of hours on obvious bugs in framework. I'm trying to bind combobox to a msde sql table and works fine. Then...
5
by: jmDesktop | last post by:
I know this should be straight forward, but I have not gotten it yet. I have a combobox and a textbox. I want to display what is selected in the combobox in the textbox. I have: ...
14
by: Mark | last post by:
I have a table with a field that uses a combobox to populate values. The Lookup tab within table design mode is the following: Display Control Combo Box Row Source Type ...
3
by: Gerhard | last post by:
Hi I have a combobox on a form which gets it's Row Source from a table. I then use an Append Query to save the selected data in the combobox to a file. Everything works fine. The Combobox's...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.