Spread Windows Forms 8.0 Product Documentation > Developer's Guide > Managing Data Binding > Binding to Data > Customizing Column and Field Binding |
When a sheet is bound to a data set, columns are assigned to data set fields sequentially. That is, the first data field is assigned to column A, the second to column B, and so on. You can change the assignments to assign any field to any column.
For bound spreadsheets, by default the spreadsheet inherits the width of the columns from the database. To determine your own custom widths, you would either need to set your width after binding the Spread or set DataAutoSizeColumns to false and set your width.
If you have more than one Spread bound to a single data set, you may want to set the AutoGenerateColumns property of the sheet in each Spread to false, so Spread does not bind all the columns. Then you can set the DataField property of the columns in each of the Spreads to the field name from the data set. Then, only that column of the data set is bound to the Spread.
This example code binds the Spread component to a data set then sets the fields to use in the first four columns.
C# |
Copy Code
|
---|---|
// Turn off automatic column and field mapping. fpSpread1.Sheets[0].AutoGenerateColumns = false; // Bind the component to the data set. fpSpread1.DataSource = dataSet1; // Set the fields for the columns. fpSpread1.Sheets[0].Columns[0].DataField = "Description"; fpSpread1.Sheets[0].Columns[1].DataField = "ID"; fpSpread1.Sheets[0].Columns[2].DataField = "LeadTime"; fpSpread1.Sheets[0].Columns[3].DataField = "Price"; |
VB |
Copy Code
|
---|---|
' Turn off automatic column and field mapping. FpSpread1.Sheets(0).AutoGenerateColumns = False ' Bind the component to the data set. FpSpread1.DataSource = DataSet1 ' Set the fields for the columns. FpSpread1.Sheets(0).Columns(0).DataField = "Description" FpSpread1.Sheets(0).Columns(1).DataField = "ID" FpSpread1.Sheets(0).Columns(2).DataField = "LeadTime" FpSpread1.Sheets(0).Columns(3).DataField = "Price" |
This example code creates a bound SheetView object and maps four fields to four columns, then assigns it to a sheet in a Spread component.
C# |
Copy Code
|
---|---|
// Create a new SheetView object. FarPoint.Win.Spread.SheetView newsheet = new FarPoint.Win.Spread.SheetView(); // Turn off automatic column and field mapping. newsheet.AutoGenerateColumns = false; // Bind the SheetView object to the data set. newsheet.DataSource = dataSet1; // Set the fields for the columns. newsheet.Columns[0].DataField = "Description"; newsheet.Columns[1].DataField = "ID"; newsheet.Columns[2].DataField = "LeadTime"; newsheet.Columns[3].DataField = "Price"; // Assign the SheetView object to the first sheet. fpSpread1.Sheets[0] = newsheet; |
VB |
Copy Code
|
---|---|
' Create a new SheetView object. Dim newsheet As New FarPoint.Win.Spread.SheetView() ' Turn off automatic column and field mapping. newsheet.AutoGenerateColumns = False ' Bind the SheetView object to the data set. newsheet.DataSource = DataSet1 ' Set the fields for the columns. newsheet.Columns(0).DataField = "Description" newsheet.Columns(1).DataField = "ID" newsheet.Columns(2).DataField = "LeadTime" newsheet.Columns(3).DataField = "Price" ' Assign the SheetView object to the first sheet. FpSpread1.Sheets(0) = newsheet |