In the bound mode, grid fetches the data from data source and display it as records and bound columns. To add a column to display data that does not exist in the data source, you need to add unbound columns at design time or through code.
FlexGrid provides Add, Insert and InsertRange method of the ColumnCollection class to add an unbound column to the grid. While Add method adds a column at the end, the Insert method lets you specify the position where you want to add a new column. Similarly, you can add multiple columns at a specified position by using the InsertRange method.
Use the code below to add unbound column in a bound WinForms FlexGrid.
To define values in the unbound column of bound grid, you need to use the SetUnboundValue and GetUnboundValue events of the C1FlexGrid class. First, create a hash table to store the entered values then, use the SetUnboundValue event to set unbound value in the hash table using a unique key to identify the record and use GetUnboundValue event, to get the value stored in the hash table using its unique key and set unbound value to be displayed in the cell.
Following code shows how to set values in an unbound column added to a bound WinForms FlexGrid.
To set values in the fixed columns, you need to set DrawMode property of the C1FlexGrid class to OwnerDraw in the form load event and then, create the OwnerDrawCell event to set values in the fixed column cells. In this example, we are setting row numbers in the fixed column of a bound WinForms FlexGrid.
FlexGrid provides AutoResize property of the C1FlexGrid class to adjust the width of columns automatically according to the text length. You need to set the property to true before binding it to the data source to load the grid with appropriate column width. You can also call AutoSizeCol method to adjust the width of the specified column.
Use the code below auto-adjust the column width according to the text in WinForms FlexGrid.
In most of the scenarios, grid directly fetches the images from the data source. However, if the grid is bound to a database which stores images as OLE objects such as Microsoft Access, a little bit of extra processing is required to fetch bitmap images. In such case, you need to convert the image data stored as byte array to a memory stream and then, use the OwnerDrawCell event to load the image. In the form load event, you need to set the DrawMode property to OwnerDraw. Also, adjust the height of row to display the image properly.
Following code demonstrates how to display bitmap image in fields of WinForms FlexGrid.
Note that the above sample code uses a custom method, LoadImage method to convert the images from byte array to a memory stream.