Question : placing images/icons in a grid

Hi,
Is it possible to replace for example we have a data grid, one of ht ecolumns has true or false, i would like to replace the True with a Tick image and the False with a cross image or icon.
Is it possible to do this in the vamilla data gris in vs 2008, if not are there any good 3rd party controls that can be recomened?
thanks

Answer : placing images/icons in a grid

Hi,

Please check link for solution:
http://niitdeveloper.blogspot.com/2010/07/display-image-instead-of-checkbox-in.html.

Here 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.


Random Solutions  
 
programming4us programming4us