Code Monkey

As full as my schedule is, I often look for some mindless entertainment and find myself randomly browsing YouTube or Google Videos. I prefer to browse the ‘stupid humor’ videos (who doesn’t know about Leroy Jenkins?). You know what I am talking about, those little things that make you laugh hysterically as the sane world wonders – why? Similar to why I think the shirts on ThinkGeek.com are hilarious. There are a number of videos available for a creative song by Jonathan Coulton. I think he did a great job on the song, and if you get a chance take a look at some of the videos that were put together for the song. There is even a live version.



   

A DataGridView, DataGridViewCheckBoxColumn, DataGridViewButtonColumn and DataColumn.

As I continue to work on moving (upgrading?) applications and code to dotNET 2.0 I continue to find that in some cases certain implemenations have made changes that simplify things. One case in particular, is the displaying of CheckBoxes and Buttons in a DataGrid. One old way was to override the painting of a cell. Another might have been to overaly a control in the clipped rectangle of the DataGrid.

As with any “upgrade” new “things” are introduced. In this case I will refer to the DataGridView, DataGridViewCheckBoxColumn and the DataGridViewButtonColumn classes. The allows for the displaying of “data” in a grid (Note: when working with large amounts of data don’t forget to set the VirtualMode property). This source of the data can be anything that implements a IList, IListSource, IBindingList or IBindingListView interface. The data displayed doesn’t always have to be from a database, a one dimmensional array alos works well with a datagrid (take a look at the sample method PopulateDataGridView). It may sound intimidating, but this is all fairly straght forward.

using System;
using System.Data;
using System.Windows.Forms;

namespace DrawInDataGrid1
{
    public partial class Form1 : Form
    {
        private DataTable myTable;
        private DataGridViewCheckBoxColumn checkcolumn;
        private DataGridViewButtonColumn buttoncolumn;
        private DataColumn colItem1, colItem2, colItem3;
        private DataRow NewRow;
        private DataView myView;

        public Form1()
        {
            InitializeComponent ();
        }

        private void Form1_Load( object sender, EventArgs e )
        {
            // DataTable to hold data that is displayed in DataGrid
            myTable = new DataTable ( "myTable" );

            // the three columns in the table
            colItem1 = new DataColumn ( "CheckBox", Type.GetType ( "System.Boolean" ) );
            colItem2 = new DataColumn ( "Button", Type.GetType ( "System.String" ) );
            colItem3 = new DataColumn ( "String", Type.GetType ( "System.String" ) );

            // add the columns to the table
            myTable.Columns.Add ( colItem1 );
            myTable.Columns.Add ( colItem2 );
            myTable.Columns.Add ( colItem3 );

            checkcolumn = new DataGridViewCheckBoxColumn ( false );
            checkcolumn.HeaderText = "CheckBox";
            checkcolumn.DataPropertyName = "CheckBox";

            buttoncolumn = new DataGridViewButtonColumn ();
            buttoncolumn.HeaderText = "Button";
            buttoncolumn.DataPropertyName = "Button";

            dataGridView1.Columns.Add ( checkcolumn );
            dataGridView1.Columns.Add ( buttoncolumn );


            // Fill in some data
            NewRow = myTable.NewRow ();
            NewRow[0] = true;
            NewRow[1] = "0";
            NewRow[2] = "Test";
            myTable.Rows.Add ( NewRow );

            NewRow = myTable.NewRow ();
            NewRow[0] = false;
            NewRow[1] = "1";
            NewRow[2] = "Next One";
            myTable.Rows.Add ( NewRow );

            // DataView for the DataGridView
            myView = new DataView ( myTable );
            myView.AllowDelete = false;
            myView.AllowEdit = false;
            myView.AllowNew = false;

            // add an event to check for button click
            this.dataGridView1.CellContentClick +=
                new System.Windows.Forms.DataGridViewCellEventHandler ( this.dataGridView1_CellContentClick );

            // Assign DataView to DataGrid
            dataGridView1.DataSource = myView;
        }


        private void dataGridView1_CellContentClick( object sender, DataGridViewCellEventArgs e )
        {
            if ( sender is DataGridView )
            {
                if ( ( ( (DataGridView) sender ).Columns[e.ColumnIndex] is DataGridViewButtonColumn ) &&
                    ( e.RowIndex >= 0 ) )
                {
                    MessageBox.Show ( e.RowIndex.ToString () );
                }
            }
        }
    }
}


Another “column” I am glad they added is the DataGridViewImageColumn.