Net-informations.com
SiteMap  | About    

C# DataGridView Template

datagridview-template

The DataGridView control makes it easy to define the basic appearance and the display formatting of rows and columns. There are situations that you want greater control over the appearance of DataGridView rows than what is provided by the various DataGridView cell style properties.

The row template gives you greater control over the appearance and behavior of rows than the RowsDefaultCellStyle property provides. With the row template, you can set any DataGridViewRow properties, including DefaultCellStyle. The following C# example illustrates how to use the row template to specify an initial row height and a minimum row height and BackColor.




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)
        {
            DataGridViewRow row = this.dataGridView1.RowTemplate;
            row.DefaultCellStyle.BackColor = Color.Bisque;
            row.Height = 35;
            row.MinimumHeight = 20;
            
            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";

        }
    }
}

 C# DataGridView - Related Contents