Spread Windows Forms 9.0 Product Documentation > Developer's Guide > Managing Data Binding > Binding to Data > Binding a Combo Box to a DataReader |
You can bind a combo box to a DataReader. A DataReader lets you access data in a read-only, forward-only way from a data source. Using a DataReader is often a quick way of returning results, as illustrated in the following example for populating results in a combo box.
This example adds data to a combo cell from a data source.
C# |
Copy Code
|
---|---|
/// Set up a connection to the database. The database name is an example. string dbpath = "c:\\reader.mdb"; System.Data.OleDb.OleDbConnection dbConn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + dbpath); /// Open a connection and a SELECT command. dbConn.Open(); System.Data.OleDb.OleDbCommand dbCommand = new System.Data.OleDb.OleDbCommand("SELECT * FROM Table1", dbConn); /// Open a DataReader on that connection and command. System.Data.OleDb.OleDbDataReader dr = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection); /// Loop through the rows returned by the reader. ArrayList al = new ArrayList(); while (dr.Read()) { al.Add(dr("Data")); } /// Populate combo box with data converted to strings. string[] s; s = al.ToArray(typeof(string)); FarPoint.Win.Spread.ComboBoxCellType cb = new FarPoint.Win.Spread.ComboBoxCellType(); cb.Items = s; FpSpread1.ActiveSheetView.Cells(0, 0).CellType = cb; /// Dispose connection. dbConn.Dispose(); |
VB |
Copy Code
|
---|---|
' Set up a connection to the database. The database name is an example. Dim dbpath As String = "c:\reader.mdb" Dim dbConn As New System.Data.OleDb.OleDbConnection( _ "Provider=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & dbpath) ' Open a connection and a SELECT command. dbConn.Open() Dim dbCommand As New System.Data.OleDb.OleDbCommand( _ "SELECT * FROM Table1", dbConn) ' Open a DataReader on that connection and command. Dim dr As System.Data.OleDb.OleDbDataReader = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection) ' Loop through the rows returned by the reader. Dim al As New ArrayList() While dr.Read() al.Add(dr("Data")) End While ' Populate combo box with data converted to strings. Dim s As String() s = al.ToArray(GetType(String)) Dim cb As New FarPoint.Win.Spread.ComboBoxCellType cb.Items = s FpSpread1.ActiveSheetView.Cells(0, 0).CellType = cb ' Dispose connection. dbConn.Dispose() |