- 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
How to fixed HTML format issue in react using Visual Studio Code
In this article, We will fix a HTML format issue in react using VS Code. I will show how to do it, and just follow […]
What is Microsoft Power Automate
In this article, I will know what is power automate, And Microsoft Power Automate, formerly known as Microsoft Flow, is a cloud-based service that allows […]
What is Microsoft Power Apps
In this article, I will know what is power apps, And Microsoft Power Apps is a low-code development platform that allows users to easily create […]