- A text box control is used to capture information in a computer program. A "DataGridView" control allows programmers to add a column of text boxes by adding a "DataGridViewTextBoxColumn." This column can be used to capture text-based values such as numbers and strings for each row. The following is an example of how to use this type of control:
DataGridViewTextBoxColumn titleColumn =
new DataGridViewTextBoxColumn();
titleColumn.HeaderText = "Title";
titleColumn.AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCellsExceptHeader; - A check box control is used to mark data as true or false in a computer program. The "DataGridViewCheckBoxColumn" is used to display a check box control in the cells of each row. This type of control is typically used to check a lot of cell values to perform bulk operations such as checking and deleting emails. The following is an example of how to set up a check box control in a DataGridView control to mark employees out of office:
private void AddOutOfOfficeColumn()
{
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
column.HeaderText = ColumnName.OutOfOffice.ToString();
column.Name = ColumnName.OutOfOffice.ToString();
column.AutoSizeMode =
DataGridViewAutoSizeColumnMode.DisplayedCells;
column.FlatStyle = FlatStyle.Standard;
column.ThreeState = true;
column.CellTemplate = new DataGridViewCheckBoxCell();
column.CellTemplate.Style.BackColor = Color.Beige;
}
DataGridView1.Columns.Insert(0, column);
} - The DataGridViewImageColumn is used to display images. Often, image columns are populated automatically from a data source such as a database. This data type will add an image control to each cell of this data type, which enables you to display images for every new row. One example of when to use this control may be in applications displaying ads. The first column will display an image, and the rest will display the description of the ad. The following shows how to create the image column:
private void CreateColumns()
{
DataGridViewImageColumn imageColumn;
int columnCount = 0;
do
{
Bitmap unMarked = blank;
imageColumn = new DataGridViewImageColumn();
//Add twice the padding for the left and
//right sides of the cell.
imageColumn.Width = x.Width + 2 * bitmapPadding + 1;
imageColumn.Image = unMarked;
dataGridView1.Columns.Add(imageColumn);
columnCount = columnCount + 1;
}
while (columnCount < 3);
} - You can also add a combo box column by using the "DataGridViewComboBoxColumn" data type. In C#, you can think of a combo box as a combination of a text box and a list box control. A list box control is a control that displays a list of items to choose from. Text can be entered or items can be chosen from a drop-down list for each row. This control is useful for data entry in fields that can only contain particular values, such as a particular type of product. The following is an example on how to add combo box column controls:
private void AddComboBoxColumns()
{
DataGridViewComboBoxColumn comboboxColumn;
comboboxColumn = CreateComboBoxColumn();
SetAlternateChoicesUsingDataSource(comboboxColumn);
comboboxColumn.HeaderText = "TitleOfCourtesy (via DataSource property)";
DataGridView1.Columns.Insert(0, comboboxColumn);
comboboxColumn = CreateComboBoxColumn();
SetAlternateChoicesUsingItems(comboboxColumn);
comboboxColumn.HeaderText = "TitleOfCourtesy (via Items property)";
// Tack this example column onto the end.
DataGridView1.Columns.Add(comboboxColumn);
}
Text Box Control
Check Box Control
Image Control
Combo Box Control
SHARE