Ok, I'm working on populating a list box from a database and would like to use data binding so that the rest of the fields in the form display the associated data.
Here's the code that I've got;
private void BindPersonListBox()
DataTable playerTable = Database.GetPlayerList();//returns a DataTable with all the proper fields
lbxPlayers.DataSource = playerTable;
lbxPlayers.DisplayMember = "Last_Name";
lbxPlayers.ValueMember = "Person_ID";
What I would like to know is this; How do I make it so that the DisplayMember can be a concatenation of a couple fields in my table? I would like the listbox to show "Last_Name, First_Name Middle_Initial" rather than just the "Last_Name"
I read somewhere that if you leave off the "DisplayMember" part then it calls the ToString() function on the object, however, since I am pulling directly from my database table as the data source there is no actual class being used for the "player"
I know that I could populate a DataTable, then loop through it creating objects of my classPerson, then use those to populate the list box, but how would that work with DataBinding? Will it automatically know that it has the correct person? Also, how would I go about using the DataTable? It does not seem to work like the datasets that I have used in the past. It does not seem to want to respond to playerTable[i].Player_ID.
My event handler for selected index changed is
person = Database.GetPerson(lbxPlayers.SelectedValue.ToString());
ShowPersonData(); //calls a function which changes the txtBoxes on the screen to match person.whatever
I suppose I could do the previous idea and fill it with classPerson objects and change the SelectedIndexChanged event handler to something like;
person = Database.GetPerson(((classPerson)lbxPlayers.Items[lbxPlayers.SelectedIndex]).Person_ID.ToString());
but that seems a little awkward, and there seems like there ought to be a more simple way of doing it.
So, the points will go to someone who can show me, if possible, a more elegant way of doing what I want to do.
If it's not possible, then, tell me why, and offer a solution, or validate that the way that I came up with is the only / best way to do it, and you can have the points...