C# DataGridView Formatting

The DataGridView control stands out for its exceptional configurability and extensibility, offering developers a rich set of properties, methods, and events to seamlessly customize its appearance and behavior. This high level of flexibility empowers developers to tailor the DataGridView to meet their specific requirements and achieve the desired user experience.

One notable feature of the DataGridView control is its ability to effortlessly define the visual aspects of cells and the formatting of their displayed values. Each individual cell within the control can possess its own unique style, including characteristics such as text format, background color, foreground color, and font. However, it is also common for multiple cells to share certain style attributes, promoting consistency and cohesion across the table.

datagrid-formating

Appearance and formatting styles

To define appearance and formatting styles, developers have various options at their disposal. They can specify styles for individual cells, customize the styles of cells within specific columns and rows, or even apply a consistent style to all cells within the control. These customizations can be achieved by setting the properties of DataGridViewCellStyle objects, which are accessed through different properties of the DataGridView control.

The following C# program shows how to implement different ways of formatting in a DataGridView control.

Full Source C#
using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; using System.Drawing; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"; string sql = "SELECT * FROM Authors"; SqlConnection connection = new SqlConnection(connectionString); SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection); DataSet ds = new DataSet(); connection.Open(); dataadapter.Fill(ds, "Author_table"); connection.Close(); dataGridView1.DataSource = ds; dataGridView1.DataMember = "Author_table"; dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque; dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige; dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None; dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red; dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow; dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True; dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.AllowUserToResizeColumns = false; } } }

Conclusion

Exploring the diverse styling capabilities provided by the DataGridView control, developers can create visually appealing and user-friendly interfaces. They can effortlessly modify the appearance and formatting of cells, aligning them with branding guidelines or optimizing readability for users. This level of customization adds a touch of professionalism and ensures a consistent visual presentation throughout the DataGridView control.