Good Practice on ListBox

  • Thread starter Thread starter Hemang Shah
  • Start date Start date
H

Hemang Shah

Greetings

I want to display a listbox on the form which will include two columns from
the data table" FirstName" & "LastName"

What I've done now, is modify the DataAdapter to include two additional
colums as:

[FirstName] + " " +[LastName] As NameFN
[LastName] + " " +[FirstName] AS NameLN

I've selected NameFN as the DisplayMember of the listbox
However, I've also selected the Sort option to be true

So what I see on the listbox doesn't correspond to the record selected on
the form.

The record is selected automatically on the form because my DataSource
Property is bound to the dataset object.

Is this a good practice ?

Or its better to create a dynamic Array List on loadup and use that ?

If that is the case how can I make the list selection, make the record on
the form change ?

Thanks
 
Those were some of the best articles I've read!

Ok I got my box to sort on the fly, but now my listbox is bound to the
datatable and my form is bound to the dataset. So when I select any item in
the list box, the records on the form is not changed.

How can I utilize that feature ?

Thanks


W.G. Ryan eMVP said:
Ac tually, for this task you might want to consider an ExpressionColumn
and
Binding to it

http://www.knowdotnet.com/articles/dataviews1.html

This way you can sort as you please, it will be updated immediately if the
underlying data changes, and you aren't pulling over erroneous data.

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Hemang Shah said:
Greetings

I want to display a listbox on the form which will include two columns from
the data table" FirstName" & "LastName"

What I've done now, is modify the DataAdapter to include two additional
colums as:

[FirstName] + " " +[LastName] As NameFN
[LastName] + " " +[FirstName] AS NameLN

I've selected NameFN as the DisplayMember of the listbox
However, I've also selected the Sort option to be true

So what I see on the listbox doesn't correspond to the record selected on
the form.

The record is selected automatically on the form because my DataSource
Property is bound to the dataset object.

Is this a good practice ?

Or its better to create a dynamic Array List on loadup and use that ?

If that is the case how can I make the list selection, make the record on
the form change ?

Thanks
 
Hemang - thank you. It depends on how you have things set up. If the data
in the list box is in the same dataset as the rest fo the stuff on the
form - you can just add your datacolumn to it and bind the listbox
accordingly. If there is a field between the dataset and datatable taht's
common - just include everything in the same dataset and use a DataRelation
object to link them together. Another thing you can do is use the
SelectedIndex for instance of the listbox to set the Position property of
the BindingContext.

I think thought that whatever approach you use- sticking the datatalbe in
the dataset with the rest of the data is probalby the way to go.

Let me know if you have any problems.

Bill

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Hemang Shah said:
Those were some of the best articles I've read!

Ok I got my box to sort on the fly, but now my listbox is bound to the
datatable and my form is bound to the dataset. So when I select any item in
the list box, the records on the form is not changed.

How can I utilize that feature ?

Thanks


W.G. Ryan eMVP said:
Ac tually, for this task you might want to consider an ExpressionColumn
and
Binding to it

http://www.knowdotnet.com/articles/dataviews1.html

This way you can sort as you please, it will be updated immediately if the
underlying data changes, and you aren't pulling over erroneous data.

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Hemang Shah said:
Greetings

I want to display a listbox on the form which will include two columns from
the data table" FirstName" & "LastName"

What I've done now, is modify the DataAdapter to include two additional
colums as:

[FirstName] + " " +[LastName] As NameFN
[LastName] + " " +[FirstName] AS NameLN

I've selected NameFN as the DisplayMember of the listbox
However, I've also selected the Sort option to be true

So what I see on the listbox doesn't correspond to the record selected on
the form.

The record is selected automatically on the form because my DataSource
Property is bound to the dataset object.

Is this a good practice ?

Or its better to create a dynamic Array List on loadup and use that ?

If that is the case how can I make the list selection, make the record on
the form change ?

Thanks
 
Hello Bill

You are welcome :)

Ok, your article worked, but it didn't work for me because I don't know how
to make it work for me!

I created a DataView dvClient and assisnged it to a table in my dataset -
tblClient, I did this in GUI:

//

// dvClient

//

this.dvClient.Table = this.dsClient.tblClient;

//

Next I used your article to come up with this:

private void frmClient_Load(object sender, System.EventArgs e)

{

DataColumn dcFullName = new DataColumn();

this.daClient.Fill(this.dsClient);

this.daChild.Fill(this.dsClient);

this.daClientAddress.Fill(this.dsClient);

dcFullName.DataType = System.Type.GetType("System.String");

dcFullName.ColumnName = "FullNameFN";

dcFullName.Expression = "GivenName + ' ' + LastName";

this.dsClient.tblClient.Columns.Add(dcFullName);

this.dvClient.Sort = "GivenName";

//lstName.DataSource = dvClient;

lstName.DisplayMember = "FullNameFN";

//lstName.ValueMember = "tblClient.ClientID";

}

Now, if I set the datasource of the listbox to the dataview "dvClient" the
sorting works just fine. But in your example you have set it to the
datatable, when I do that ("dsClient.tblClient"), the sort doesn't work.

I wanna ask you a question here, which is confusing me:

1) Is the table created in the dataset is technically a DataTable? I mean
DataColumn makes DataRow and DataRow makes DataTable and DataTable makes
DataSet. Because in your example you pointed the DataView to a DataTable:

dvEmployees = dtEmployees.DefaultView;

So although when you generate your database you have your dataset filled
with tables aren't those DataTable ?

2) The reason I wanted the datasource of the listbox to be the dataset it
because that would sync with my form as and when I change the values. (which
is the purpose of the listbox). You did suggest the usage of selecteditem,
there is a sample in 101 c# examples which demonstrates this. But if I can
just get away having the same datasource, its easy.

3) Although the method described in your article, if datasource is pointed
to the DataView (dvClient) works like a charm, there is one issue, most of
the time, when ppl want s'thing sorted on the field, they would want that
field to be displayed first. I guess I would just have to create another
DataColumn with another expression and change the DisplayMember of the
listbox correct ? I guess it wouldnt be suggested to change the expression
of the already displayed data and sort it, I don't know what is recommended.

4) Another Issue i'm having and I cannot seem to find the answer anywhere,
is that all example I see on the net for master child form navigation is
with the child data displayed in a grid. What if I want to display child
data all in text boxes, with its own navigation control (prev,next). Text
boxes doesn't have a datasource property to point to the relation object.
From my searching I guess it would take some CurrencyManager Magic, but
without an example its pretty hard to digest that. Or I could be wrong and
there is another method.

Also when I'm trying to pick up a book, most ADO.NET books are from 2002, we
are in 2005, are those books still the best way to go ? I can't find a book
which will teach ADO.NET on Visual Studio, like make a fully functional
application as you worth through the book.

I bought Mastering C# Database Programming, which is I agree very good on
the theory of ADO.NET it also has examples to go with Visual Studio.net but
they are hardly useful, he has 1 example at the end of each chapter...

Any insight into my issues would be a boon!

Thank you again.

Hemang.

W.G. Ryan eMVP said:
Hemang - thank you. It depends on how you have things set up. If the
data
in the list box is in the same dataset as the rest fo the stuff on the
form - you can just add your datacolumn to it and bind the listbox
accordingly. If there is a field between the dataset and datatable taht's
common - just include everything in the same dataset and use a
DataRelation
object to link them together. Another thing you can do is use the
SelectedIndex for instance of the listbox to set the Position property of
the BindingContext.

I think thought that whatever approach you use- sticking the datatalbe in
the dataset with the rest of the data is probalby the way to go.

Let me know if you have any problems.

Bill

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Hemang Shah said:
Those were some of the best articles I've read!

Ok I got my box to sort on the fly, but now my listbox is bound to the
datatable and my form is bound to the dataset. So when I select any item in
the list box, the records on the form is not changed.

How can I utilize that feature ?

Thanks


W.G. Ryan eMVP said:
Ac tually, for this task you might want to consider an ExpressionColumn
and
Binding to it

http://www.knowdotnet.com/articles/dataviews1.html

This way you can sort as you please, it will be updated immediately if the
underlying data changes, and you aren't pulling over erroneous data.

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Greetings

I want to display a listbox on the form which will include two columns
from
the data table" FirstName" & "LastName"

What I've done now, is modify the DataAdapter to include two
additional
colums as:

[FirstName] + " " +[LastName] As NameFN
[LastName] + " " +[FirstName] AS NameLN

I've selected NameFN as the DisplayMember of the listbox
However, I've also selected the Sort option to be true

So what I see on the listbox doesn't correspond to the record selected on
the form.

The record is selected automatically on the form because my DataSource
Property is bound to the dataset object.

Is this a good practice ?

Or its better to create a dynamic Array List on loadup and use that ?

If that is the case how can I make the list selection, make the record on
the form change ?

Thanks
 
Back
Top