This article is the second way (version) of "how to populate data in a ComboBox" program. For doing that, I make use of '
TableMapping' and '
DataViewManager' that I'll explain later in "Code and How it works".
The application allows you to select a StudentID in a
ComboBox and displays the columns StudentID, Student Subject and Student Name from the table in the three
It shows also how to get connected to a MS Access database which you can also find in the project included (sudentDB.mdb). I chose MS Access database because not many people have SQL-Server running.
Code and How it works
Before starting, a short explanation of what
TableMapping is the process that controls how data adapters copy tables and columns of data from a physical data source to ADO.NET in-memory objects. When a data adapter reads data from a data source, it determines where to put the data in the corresponding
DataSet table (or tables) using a table mapping. If you create a mapping in a data adapter, it allows you to establish a correspondence between columns in the data source and columns in a
DataAdapter contains a collection of
DataTableMapping objects in its
TableMappings property. You can pass the
DataTableMapping name in place of the
DataTable name to the
Fill method of the
The following example creates a
DataTableMapping named "
MyStudentMappings" for the table "studentTable".
When you call the
Fill method of the
DataAdapter and do not specify a
DataTableMapping name, the
DataAdapter looks for a
DataTableMapping called "Table". If you leave out that
TableName of the
DataTable will be "Table". This is the default
SELECT command creates a result set with a default name of Table, then its contents go into a new or existing
DataTable object called "
DataTableMapping object describes a mapped relationship between a SQL-result set and a
DataTable object in a
DataViewManager represents a view onto an entire
DataSet and represents a databindable, customized view of a
DataTable for sorting, filtering, searching, editing, and navigation, whereas the
DataView class acts as a view onto a single
DataViewManager is an object that contains a collection of data views. The
DataViewManager returned by the
DefaultViewManager property allows you to create custom settings for each
DataTable in the
DataSet. When you want to bind a control to more than one table of a
DataSet, binding to a
DataViewManager is the ideal choice. When only a single
DataTable, then use
DataView. When binding a
DataSet, .NET automatically uses the corresponding
DataViewManager provided through the
Here is the code for the method
private void fnGetConnectedToDatabase()
string conStr =
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=..\\..\\studentDB.mdb";
myConn = new OleDbConnection(conStr);
MessageBox.Show("Error in connection ..."+ex.Message);
string sqlStr ="SELECT * FROM studentTable;";
dAdapter = new OleDbDataAdapter(sqlStr,myConn);
dset = new DataSet();
As soon as you select a StudentID from the
ComboBox, the columns
StudentName in the table "studentTable" are displayed in the three
There are of course many ways to get data into a
ComboBox control. I just tried to show a different way.