• Declare Data table in global;
  • Datatable hold temporary data;
DataTable dt = new DataTable();
  • create column to datatable “dt”.
dt.Columns.Add("id");
dt.Columns.Add("name");
  • Create temporary data and stored to datatable “dt”;
 for (int i = 1; i < 21; i++)
 {
    dt.Rows.Add(DateTime.Now.ToString("hhmmssffffff")+"-"+i,
                "Juan"+i+" Tamad"+i);
  }
  • Create/Add CheckBox in datagridview.
            DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
            checkColumn.Name = "Selection";
            checkColumn.HeaderText = "Selection";
            checkColumn.Width = 50;
            checkColumn.ReadOnly = false;
            checkColumn.FillWeight = 10;
            dataGridView1.Columns.Add(checkColumn);
  • Display date with button in datagridview;
dataGridView1.DataSource = dt;
  • Hide button in datagridview or hide column in “Button”
dataGridView1.Columns[0].Visible = false;
  • Show button in datagridview or hide column in “Button”
dataGridView1.Columns[0].Visible = true;
  • All Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dt = new DataTable(); 
        private void Form1_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("id");
            dt.Columns.Add("name");

            for (int i = 1; i < 21; i++)
            {
                dt.Rows.Add(DateTime.Now.ToString("hhmmssffffff") + "-" + i,
                            "Juan" + i + " Tamad" + i);
            }

            DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
            checkColumn.Name = "Selection";
            checkColumn.HeaderText = "Selection";
            checkColumn.Width = 50;
            checkColumn.ReadOnly = false;
            checkColumn.FillWeight = 10;
            dataGridView1.Columns.Add(checkColumn);

            dataGridView1.DataSource = dt;
        }

        private void Show_Click(object sender, EventArgs e)
        {
            dataGridView1.Columns[0].Visible = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.Columns[0].Visible = false;
        }
    }
}

Related Topics

Leave a Reply

Your email address will not be published. Required fields are marked *