Hi,
Please check link for solution:
http://niitdeveloper.blogspot.com/2010/07/display-image-instead-of-checkbox-in.html.Her
e you can download the project for the same.
Here is the code snippet of the file 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;
using System.Collections;
namespace ee_DataGrid
{
public partial class Form1 : Form
{
// gridViewData is the name of the DataGridView
private Bitmap trueImg, falseImg;
public Form1()
{
InitializeComponent();
trueImg = (Bitmap)Image.FromFile(Application.StartupPath + @"\Images\true.png");
falseImg = (Bitmap)Image.FromFile(Application.StartupPath + @"\Images\false.png");
// Sorting image with this event
gridViewData.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(gridViewData_ColumnHeaderMouseClick);
}
private void btnGetData_Click(object sender, EventArgs e)
{
try
{
#region Filling DataGridView named gridViewData values
String connectionString = "Initial Catalog=TestDB;Data Source=localhost; Uid=sa; pwd=god";
SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT * FROM UID", connectionString);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
dataAdapter.Fill(table);
gridViewData.DataSource = table;
gridViewData.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
#endregion
#region Showing image instead of checkbox
// Hiding checkbox values
gridViewData.Columns["Is Indian"].Visible = false;
DataGridViewImageColumn imageCol = new DataGridViewImageColumn();
imageCol.Name = "Is Indian";
imageCol.SortMode = DataGridViewColumnSortMode.Automatic;
gridViewData.Columns.Add(imageCol);
foreach (DataGridViewRow row in gridViewData.Rows)
{
if (row.Cells["Is Indian"].Value != null)
{
if (Convert.ToBoolean(row.Cells["Is Indian"].Value))
{
// Cells[3] is the position of the cell in the row
row.Cells[3].Value = trueImg;
}
else
{
row.Cells[3].Value = falseImg;
}
}
}
#endregion
}
catch (Exception exc)
{
MessageBox.Show("Contact Developer\nTechnical Report: " + exc.ToString());
}
}
void gridViewData_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
foreach (DataGridViewRow row in gridViewData.Rows)
{
if (row.Cells["Is Indian"].Value != null)
{
if (Convert.ToBoolean(row.Cells["Is Indian"].Value))
{
// Cells[3] is the position of the cell in the row
row.Cells[3].Value = trueImg;
}
else
{
row.Cells[3].Value = falseImg;
}
}
}
}
}
}
Do write back for more explanation and help.