$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
DataGridView for C#.NET WinForms
Create custom data grid cells to GirdView in C# for your .NET Windows Forms applications
Home > WinForms UI Controls > User Manual > Formatting Grid Cells in C#

Formatting Cells to C# DataGridView

DataGridView C#.NET component allows users of the control easily create formatting grid data cells to GridView in Windows Forms application. This is achieved by using the FormatCell event, which is able to add formatting to grid data cells as well as new row cells. The Kettic DataGridView contains the UI virtualization, which restrict the created cell elements to the currently visible cells and will reuse the created formatting grid data cells while scrolling, filtering, and grouping, etc. To prevent the formatting being applied to other column cell elements, it is necessary to reset the left cell elements.

Format Grid Data Cells Font Color in C#

The following are the C# code that shows how to formatting cells font color to GridView data cells.

void ketticGridView_CellFormatting1(object sender, Kettic.WinForms.UI.FormatCellEventArgs e)
{
if (e.CellElement.ColumnInfo.Name == "KBytes")
{
e.CellElement.ForeColor = Color.Blue;
}
else
{
e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
}
}

Format Grid Data Cells Background in C#

The following is an example that demonstrates how to highlight certain cells in yellow color by using the FormatCell event. This background color formatting will be based on the values of the grid data cells in the same row but different columns. The example below will highlight the values in the first column when the value in the check box column returns true. C# code fo formatting cells background as following.

void ketticGridView_ FormatCell 2(object sender, Kettic.WinForms.UI.FormatCellEventArgs e)
{
if (e.CellElement.ColumnInfo.HeaderText == "Id")
{
if (e.CellElement.RowInfo.Cells["JPG"].Value != null)
{
if ((bool)e.CellElement.RowInfo.Cells["JPG"].Value == true)
{
e.CellElement.DrawFill = true;
e.CellElement.ForeColor = Color.Yellow;
e.CellElement.NumberOfColors = 1;
e.CellElement.BackColor = Color.Aqua;
}
else
{
e.CellElement.DrawFill = true;
e.CellElement.ForeColor = Color.Yellow;
e.CellElement.NumberOfColors = 1;
e.CellElement.BackColor = Color.Green;
}
}
}
else
{
e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
}
}

Format Non-data Grid Cells in C#

When we are going to format a grid data cell which contains no data, we can use the ViewFormatCell event. The FormatCell event is only for cells containing data, however, the ViewFormatCell event is able to format all cells, including, grouping row, header cells, and none data cells.

Create Formatting Group and Header Cells in C#

The following are the example that goes through the process of changing the font of the header cells and the group cells to GridView in C#.NET projects.

Font newFont = new Font("Arial", 12f, FontStyle.Bold);
void ketticGridView1_ViewFormatCell(object sender, FormatCellEventArgs e)
{
if (e.CellElement is GridHeaderCellElement || e.CellElement is GridGroupContentCellElement)
{
e.CellElement.Font = newFont;
e.CellElement.ForeColor = Color.Blue;
}
else
{
e.CellElement.ResetValue(LightVisualElement.FontProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
}

GridFilterCellElement filterCell = e.CellElement as GridFilterCellElement;
if (filterCell != null)
{
filterCell.FilterOperatorText.Visibility = Kettic.WinForms.ElementVisibility.Collapsed;
}

}

Format TextAlignment and Group Rows BackColor

To align the text in the grid data cells and change the back color in the group rows to none data cells in GridView C#.NET component, we can use the C# code snippet as following.

Style Button Appearance in GridViewCommandColumn

When the buttons are the children of the grid data cells, we can access the buttons through the Children collection of the visual cells to change the appearance in the GridViewCommandColumn. In the example below, we are going through how to style the button appearance.

Format Grid Data Cells as Specified

When we need to create formatting grid data cells a specific requirement, we can achieve this following the example as below. In the following example, we are going to format the button click. There is a search box above the GridView and KetticButton. When we click the button, the KetticGridView will highlight the grid data cell which matches the text entered in the KetticTextBox.

Style Property for Grid Cells Formatting

The Kettic DataGridView control provides the GridViewCellInformation Style property for users to access to the grid cell visual properties directly, which makes it possible to change styles to grid data cells at runtime instead of using the FormatCell event or the ConditionFormatObject. Furthermore, this approach will help users to customize visual properties defined by the theme and all changes will have a greater priority than the theme.
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls