DataGridView - BindingSource (Object) and ComboBox cell

P

Przemek M. Zawada

Dear Group,

I'm developing sample window form, using DataGridView control, which
is filled with data through BindingSource, which is based on type of
object, as follow:

public sampleClass
{
public int itemNumber;
public string itemName;
public string itemCategory;
public bool itemActive;

public int ItemNumber
{
get { return itemNumber; }
set { itemNumber = value; }
}
public string ItemName
{
get { return itemName; }
set { itemName = value; }
}
public string ItemCategory
{
get { return itemCategory; }
set { itemCategory = value; }
}
public bool ItemActive
{
get { return itemActive; }
set { itemActive = value; }
}
}

So, everything seems to work perfect, the 'ItemActive'
DataGridViewCell is checkbox (as required), but the problem is when
I'd like to insert a ComboBox (DropDownList type) into a sample cell.
I have no idea how to do it through the object binding system - in the
matter of fact, is it even possible? Or the only way is through
creating step by step all DataGridView columns?

If any tutorial found, which might help me a bit, don't hesitate
writing it down.

Thank you very much for any help.

All the best,
Przemek M. Zawada
 
O

Otis Mukinfus

Dear Group,

I'm developing sample window form, using DataGridView control, which
is filled with data through BindingSource, which is based on type of
object, as follow:

public sampleClass
{
public int itemNumber;
public string itemName;
public string itemCategory;
public bool itemActive;

public int ItemNumber
{
get { return itemNumber; }
set { itemNumber = value; }
}
public string ItemName
{
get { return itemName; }
set { itemName = value; }
}
public string ItemCategory
{
get { return itemCategory; }
set { itemCategory = value; }
}
public bool ItemActive
{
get { return itemActive; }
set { itemActive = value; }
}
}

So, everything seems to work perfect, the 'ItemActive'
DataGridViewCell is checkbox (as required), but the problem is when
I'd like to insert a ComboBox (DropDownList type) into a sample cell.
I have no idea how to do it through the object binding system - in the
matter of fact, is it even possible? Or the only way is through
creating step by step all DataGridView columns?

If any tutorial found, which might help me a bit, don't hesitate
writing it down.

Thank you very much for any help.

All the best,
Przemek M. Zawada

You will need to create a BindingList<T> object containing the objects that
contain at the very least an ID and description for the list you want to bind.
You will then bind the combobox to the list with manually written code. You may
be able to do the binding from the IDE by using a BindingSource object bound to
the BindingList<T> object. I've not tried that.

In addition to the above you might want to get the
book "Data Binding with Windows Forms 2.0" by Bryan Noyes. It addresses data
binding with objects extensively.


Good luck with your project,

Otis Mukinfus

http://www.otismukinfus.com
http://www.arltex.com
http://www.tomchilders.com
http://www.n5ge.com
 
P

Przemek M. Zawada

You will need to create a BindingList<T> object containing the objects that
contain at the very least an ID and description for the list you want to bind.
You will then bind the combobox to the list with manually written code. You may
be able to do the binding from the IDE by using a BindingSource object bound to
the BindingList<T> object. I've not tried that.

In addition to the above you might want to get the
book "Data Binding with Windows Forms 2.0" by Bryan Noyes. It addresses data
binding with objects extensively.

Good luck with your project,

Otis Mukinfus

http://www.otismukinfus.comhttp://www.arltex.comhttp://www.tomchilders.comhttp://www.n5ge.com

Dear Otis,

I probably understood the idea you've explained above, but if it isn't
a big problem, could you paste here simplest example?

What I want to achieve is the following:

Object is defined as MyItemClass and with public fields: ItemID,
ItemName, ItemDescription, ItemCategory. ItemCategory (in
DataGridView) should be a string, which is displayed by (SelectedItem)
in ComboBox, which ones items are loaded from database.

Sample example of initiated object MyItemClass:

ItemID equals 1238
ItemName equals Lorem ipsum
ItemDescription equals Lorem ipsum dolor (...)
ItemCategory {
Category 1
Category 2 [selected]
Category 3
Category 4
}

I presume, that I need to define the type of object which describes
single item category - i.e. (int categoryId, string categoryName), and
then create in my (main) MyItemClass field as an array of for example
ItemCategoryObj[]. What next?

Sorry if that's rookie style of thinking, but I'm trying to upgrade my
knowledge in binding objects into windows forms.

Thank you for fast answer to my problem.

All the best,
Przemek M. Zawada
 
O

Otis Mukinfus

You will need to create a BindingList<T> object containing the objects that
contain at the very least an ID and description for the list you want to bind.
You will then bind the combobox to the list with manually written code. You may
be able to do the binding from the IDE by using a BindingSource object bound to
the BindingList<T> object. I've not tried that.

In addition to the above you might want to get the
book "Data Binding with Windows Forms 2.0" by Bryan Noyes. It addresses data
binding with objects extensively.

Good luck with your project,

Otis Mukinfus

http://www.otismukinfus.comhttp://www.arltex.comhttp://www.tomchilders.comhttp://www.n5ge.com

Dear Otis,

I probably understood the idea you've explained above, but if it isn't
a big problem, could you paste here simplest example?

What I want to achieve is the following:

Object is defined as MyItemClass and with public fields: ItemID,
ItemName, ItemDescription, ItemCategory. ItemCategory (in
DataGridView) should be a string, which is displayed by (SelectedItem)
in ComboBox, which ones items are loaded from database.

Sample example of initiated object MyItemClass:

ItemID equals 1238
ItemName equals Lorem ipsum
ItemDescription equals Lorem ipsum dolor (...)
ItemCategory {
Category 1
Category 2 [selected]
Category 3
Category 4
}

I presume, that I need to define the type of object which describes
single item category - i.e. (int categoryId, string categoryName), and
then create in my (main) MyItemClass field as an array of for example
ItemCategoryObj[]. What next?

Sorry if that's rookie style of thinking, but I'm trying to upgrade my
knowledge in binding objects into windows forms.

Thank you for fast answer to my problem.

All the best,
Przemek M. Zawada

Przemek,

You can see some sample code for an object that uses the IEditableObject
interface by going to the Help Index in the VS2k IDE and typing IEditableObject
in the Look for: text box, then click on the about IEditableObject interface
selection.

Do the same searching for INotifyPropertyChanged and BindingList(Of T).

The BindingList(Of T) example contains Windows Forms example you might find
useful. Two of the examples I suggest have code in them that creates objects
targeted for data binding, but remember the examples are simple examples to
demonstrate a principle. They are not what I would call industrial strength.

After you have viewed all of these examples you will understand why I did not
send you any sample code.

Using objects as data sources, while giving you the most control over your data
access requires a lot of planning and work, so be ready to do some studying and
experimenting to understand how it works. If you cannot afford to spend the
time studying how it works and if your project is a small one, then you might
want to consider just using the drag and drop methods of creating a data
interface.

Good luck with your project,

Otis Mukinfus

http://www.otismukinfus.com
http://www.arltex.com
http://www.tomchilders.com
http://www.n5ge.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top