DataValueMember of Combobox.

  • Thread starter Thread starter Patrick Delifer
  • Start date Start date
P

Patrick Delifer

How can I apply the DataValuefield of a windows form control (ex: Combobox)
when it's bound to a datasource?
I've always workd in Webforms but never in windows forms.
Ex: I have a combobox displaying data returned from DB..My combo box shows
"Object.object.etc."
How can I set that datavalue (which is so easy to do in Web )?
 
Thanks Jaison,

I was able to get it working the way you explained it.

However, I am faced with another dilemma now:
I have a DataGrid that I am binding to an arraylist
ex:
Order order = new Order();
ArrayList orders = order.getOrders();
DataGrid.DataSource = orders.

Now in web forms, I would attribute a DataValueField for each column I want
the data to be displayed.
Unfortunately, Windows forms doesn't work the same. So i followed an example
I found on the Web of setting up a DataGridTableStyle like this:

DataGridTableStyle tableOrders = new DataGridTableStyle();

tableOrders.MappingName = "orders";

DataGridTextBoxColumn column = new DataGridTextBoxColumn();

column.MappingName = "WayBill";

column.HeaderText = "Waybill";

tableOrders.GridColumnStyles.Add(column);


column = new DataGridTextBoxColumn();

column.MappingName = "TypeName";

column.HeaderText = "Type";

tableOrders.GridColumnStyles.Add(column);



The problem is that the DataGrid is still displaying everything: Not only
the ArrayList Columns that I am reading from my stored procedure but also my
object properties (Order). I am at a dead end.

What do you think Im doing wrong?



here's my arraylist code:

........

reader = SqlHelper.ExecuteReader(Resource.ConnectionString,
CommandType.StoredProcedure, SP_GET_ORDERS, null);


if (reader.HasRows)

{

orders = new ArrayList();

while (reader.Read())

{

Order order = new Order();

order.OrderID = (int)reader["OrderID"];

order.WayBill = reader["WayBill"] as string;

order.typeid = (int)reader["TypeID"];

order.ThisService.name = reader["ServiceName"] as string;

order.OrderDate = (DateTime)reader["OrderDate"];

order.Shipper.name = reader["Shipper"] as string;

order.Shipper.address = reader["ShipperAddress"] as string;

order.Shipper.suite = reader["ShipperSuite"] as string;

etc.....

orders.Add(order);

order = null;

}

}

return orders;

.......

This is how you get a winform control such as a combo box to hold a data
item, which maybe a dataset or any object that supports the IList interface.
Lets saywe have a NameValue collection holding several NameValue objects.
Each NameValue object corresponds to a Customer entry in a database (or any
other data source).
The propery Name of the NameValue object exposes the Customer's Name and
the property Value exposes the Customer Id, which wei subsequently use to
retrieve the corresponding record from the database.
'Get the data collection
Dim custNameColl as NameValueCollection=GetCustomerNames()'replace with your own function call
Now suppose we have a combobox called cbxCustNames, we go ahead and bind
our collection to it using the datasource property.
With cbxCustNames
.DataSource=custNameColl
'Now weI want the combobox to display the Customer Name for the user to select
'So we tell the combo to display values exposed by the Name property of
each NameValue object in the collection
.DisplayMember="Name"
'When the user makes a selection , we want to be able to use the
CustomerId value for the selected item
'The CustomerId value is exposed by the Value property in each NameValue object of the collection.
.ValueMember="Value"
End With

Now during runtime, the combo box will display the names of the customers.
When a user makes a selection, we can retrieve the CustomerId of the
selected customer by retrieveing the SelectedValue property of the combobox
Dim selectedCustomerId as Long=ctype(cbxCustName.SelectedValue,Long)

The NameValueCollection class represents a sorted collection of associated
String keys and String values. You can use even an ArrayList or any other
collection class that supports the IList interface, in place of the
NameValueCollection class, but remember to specify the DisplayMember &
ValueMember property of the combo box at the time of binding.
 
Hello Patrick

It doesnt seem like you have added tableOrders (your custom DataGridTableStyle instance) to the TableStyle collection of your DataGrid object
It can be done this way..
DataGrid.TableStyles.Add(tableOrders
 
Proplem with OOP and DataGrid too

Patrick,

I am facing the same problem myself but in the compact framework.
In your case, did you manage to show order.Shipper.address value on the datagrid?


Patrick Delifer said:
Thanks Jaison,

I was able to get it working the way you explained it.

However, I am faced with another dilemma now:
I have a DataGrid that I am binding to an arraylist
ex:
Order order = new Order();
ArrayList orders = order.getOrders();
DataGrid.DataSource = orders.

Now in web forms, I would attribute a DataValueField for each column I want
the data to be displayed.
Unfortunately, Windows forms doesn't work the same. So i followed an example
I found on the Web of setting up a DataGridTableStyle like this:

DataGridTableStyle tableOrders = new DataGridTableStyle();

tableOrders.MappingName = "orders";

DataGridTextBoxColumn column = new DataGridTextBoxColumn();

column.MappingName = "WayBill";

column.HeaderText = "Waybill";

tableOrders.GridColumnStyles.Add(column);


column = new DataGridTextBoxColumn();

column.MappingName = "TypeName";

column.HeaderText = "Type";

tableOrders.GridColumnStyles.Add(column);



The problem is that the DataGrid is still displaying everything: Not only
the ArrayList Columns that I am reading from my stored procedure but also my
object properties (Order). I am at a dead end.

What do you think Im doing wrong?



here's my arraylist code:

........

reader = SqlHelper.ExecuteReader(Resource.ConnectionString,
CommandType.StoredProcedure, SP_GET_ORDERS, null);


if (reader.HasRows)

{

orders = new ArrayList();

while (reader.Read())

{

Order order = new Order();

order.OrderID = (int)reader["OrderID"];

order.WayBill = reader["WayBill"] as string;

order.typeid = (int)reader["TypeID"];

order.ThisService.name = reader["ServiceName"] as string;

order.OrderDate = (DateTime)reader["OrderDate"];

order.Shipper.name = reader["Shipper"] as string;

order.Shipper.address = reader["ShipperAddress"] as string;

order.Shipper.suite = reader["ShipperSuite"] as string;

etc.....

orders.Add(order);

order = null;

}

}

return orders;

.......

"Jaison" wrote in message
news:[email protected]...

> This is how you get a winform control such as a combo box to hold a data

item, which maybe a dataset or any object that supports the IList interface.
>
> Lets saywe have a NameValue collection holding several NameValue objects.

Each NameValue object corresponds to a Customer entry in a database (or any
other data source).
> The propery Name of the NameValue object exposes the Customer's Name and

the property Value exposes the Customer Id, which wei subsequently use to
retrieve the corresponding record from the database.
> 'Get the data collection
> Dim custNameColl as NameValueCollection=GetCustomerNames()'replace with

your own function call
> Now suppose we have a combobox called cbxCustNames, we go ahead and bind

our collection to it using the datasource property.
> With cbxCustNames
> .DataSource=custNameColl
> 'Now weI want the combobox to display the Customer Name for the user to

select
> 'So we tell the combo to display values exposed by the Name property of

each NameValue object in the collection
> .DisplayMember="Name"
> 'When the user makes a selection , we want to be able to use the

CustomerId value for the selected item
> 'The CustomerId value is exposed by the Value property in each NameValue

object of the collection.
> .ValueMember="Value"
> End With
>
> Now during runtime, the combo box will display the names of the customers.

When a user makes a selection, we can retrieve the CustomerId of the
selected customer by retrieveing the SelectedValue property of the combobox
>
> Dim selectedCustomerId as Long=ctype(cbxCustName.SelectedValue,Long)
>
> The NameValueCollection class represents a sorted collection of associated

String keys and String values. You can use even an ArrayList or any other
collection class that supports the IList interface, in place of the
NameValueCollection class, but remember to specify the DisplayMember &
ValueMember property of the combo box at the time of binding.
 
Back
Top