Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to create customised datagridview control with customised control like customised check box. I want to set customised check box as check box column in customised datagridview control and also set customised check box as header of that column. So, how it is possible? I visit lot of web sites but I can't find solution for this so please help me. and I want to do this in c# windows forms application.


What I have tried:

I tried lot of web sites but can't find proper solution
Posted
Updated 22-Feb-17 2:42am

Here is an example on CodeProject, it is a bit verbose, but it shows how to use checkboxes in a datagridview: DataGridView Helper Class[^]

Here is a simpler example, you can skip the lines using the .png images:
using System;
using System.Windows.Forms;
using System.Drawing;

namespace TestForm1
{
    /// <summary>
    /// DataGridView with buttons and checkboxes.
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            this.InitializeComponent();
        }

        private void buttonFill_Click(object sender, EventArgs e)
        {
            myDataGridView.Columns.Clear();
            myDataGridView.RowHeadersVisible = false;
            //myDataGridView.SortedColumn
            myDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.WhiteSmoke;
            myDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            DataGridViewImageColumn dgvButton = new DataGridViewImageColumn();
            dgvButton.Name = "";
            var image1 = Image.FromFile("expand.png");
            dgvButton.Image = image1;
            myDataGridView.Columns.Add(dgvButton);

            DataGridViewTextBoxColumn dgvText = new DataGridViewTextBoxColumn();
            dgvText.Name = "User ID";
            myDataGridView.Columns.Add(dgvText);

            DataGridViewTextBoxColumn dgvText2 = new DataGridViewTextBoxColumn();
            dgvText2.Name = "Password";
            myDataGridView.Columns.Add(dgvText2);

            DataGridViewComboBoxColumn dgvCombo = new DataGridViewComboBoxColumn();
            dgvCombo.Name = "Priority";
            dgvCombo.Width = 300;
            dgvCombo.DataSource = new string[] { "One", "Two", "Three" };
            myDataGridView.Columns.Add(dgvCombo);

            DataGridViewCheckBoxColumn dgvCheck = new DataGridViewCheckBoxColumn(true);
            myDataGridView.Columns.Add(dgvCheck);

            var row = new object[] { image1, "abc", "xyz", "One", false };
            myDataGridView.Rows.Add(row);
            row = new object[] { image1, "pqr", "stu", "Two", true };
            myDataGridView.Rows.Add(row);
            row = new object[] { image1, "gaga", "aha", "Three", false };
            myDataGridView.Rows.Add(row);
            row = new object[] { image1, "zuzu", "lala", "One", false };
            myDataGridView.Rows.Add(row);
        }

        private void myDataGridView_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                var imageName = myDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag ?? "expand.png";

                if (imageName.ToString() == "toggle.png")
                {
                    imageName = "expand.png";
                }
                else
                {
                    imageName = "toggle.png";
                }

                myDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Image.FromFile(imageName.ToString());
                myDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag = imageName;

                string str = myDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString();
                MessageBox.Show("You Have Selected " + str);
            }
        }

        private void myDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                if (myDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == "abc")
                {
                    //e.CellStyle.BackColor = Color.Yellow;
                    e.CellStyle.ForeColor = Color.Gray;
                }
            }

            if (e.ColumnIndex == 4)
            {
                var check = myDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

                if (check != null && (bool)check == false)
                {
                    e.CellStyle.BackColor = Color.Gray;
                    e.CellStyle.ForeColor = Color.Red;
                }
            }
        }
    }
}
 
Share this answer
 
v2
Comments
Member 12578086 22-Feb-17 8:39am    
Thanks RickZeeland
Karthik_Mahalingam 23-Feb-17 12:25pm    
5
This could also be an Approach which helps you ...

www.codemag.com/article/0707061

It shows how to create a custom ColumnType for the DataGridView.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900