DataGridView is used to display data in tabular structure in windows forms, DataGridView control is customizable and contains lot more events than any other control. The basic elements of  DataGridView is Rows,Columns,Cell, the default DataGridView cell type is   textbox. The following code explains how to add Button,Checkbox,Combobox,Image,Link column to DataGridView.

Create a New Windows Form Application
File -> New -> Project -> Windows Forms Application

Setup your Project with SQL Connection
Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Dgvalloperations
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection("Data Source=PROJECT-SERVER;Initial Catalog=Dbname;Persist Security Info=True;User ID=sa;Password=admin@123");
SqlCommand cmd = new SqlCommand();

public Form1()
{
InitializeComponent();
}
}
}


Add Button Column to DataGridView

private void btAddBt_Click(object sender, EventArgs e)
{
DataGridViewButtonColumn button = new DataGridViewButtonColumn();
dataGridView1.Columns.Add(button);
button.HeaderText = "Accept";
button.Text = "Accept";
button.Name = "button";
button.UseColumnTextForButtonValue = true;
}


Add Cell Click Event to Perform DataGridView Button Action

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns.Contains("button") && e.ColumnIndex == dataGridView1.Columns["button"].Index)//Specify which column contains Button in DGV
{
MessageBox.Show("Row "+(e.RowIndex + 1)+" Of " +(e.ColumnIndex + 1) + " th Column button clicked ");
}
}

Add ComboBox Column to DataGridView

private void btCmb_Click(object sender, EventArgs e)
{
DataGridViewComboBoxColumn combobox = new DataGridViewComboBoxColumn();
combobox.HeaderText = "Select Country";
combobox.Name = "combobox";
combobox.Items.Add("India");
combobox.Items.Add("UAE");
combobox.Items.Add("USA");
combobox.Items.Add("England");
dataGridView1.Columns.Add(combobox);
}


Add CheckBox Column to DataGridView

private void btAddCb_Click(object sender, EventArgs e)
{
DataGridViewCheckBoxColumn checkbox = new DataGridViewCheckBoxColumn();
checkbox.HeaderText = "Tick";
checkbox.Name = "checkbox";
dataGridView1.Columns.Add(checkbox);
dataGridView1.Rows[3].Cells[7].Value = true; // make a specific row checked
}


Add Image Column to DataGridView

private void btImg_Click(object sender, EventArgs e)
{
DataGridViewImageColumn imagecontrol = new DataGridViewImageColumn();
imagecontrol.ImageLayout = DataGridViewImageCellLayout.Stretch;
Image image = Image.FromFile(@"C:\Users\shabeer\Downloads\Mail-icon.png"); //image path
imagecontrol.Image = image;
dataGridView1.Columns.Add(imagecontrol);
imagecontrol.HeaderText = "Image";
imagecontrol.Name = "imagecontrol";
}


Add Link Column to DataGridView

private void btLink_Click(object sender, EventArgs e)
{
DataGridViewLinkColumn link = new DataGridViewLinkColumn();
link.HeaderText = "Visit";
link.Name = "Http://www.techcybo.com";
link.Text = "Http://www.techcybo.com";
dataGridView1.Columns.Add(link);
link.UseColumnTextForLinkValue = true;
}

Download Full Project 



TechCybo

TechCybo

A Technolgy Blog about How-To Tutorials, Android, Programming, Blogging, Windows, Software Tips, Mobile Apps and Tricks for better Digital Life

Post A Comment: