BPSoftware.com
Home   Utilities   Purchase   FAQ   Support   Contact        
Shareware Utilities
 APrintDirect
 AIconExtract
 AFile Attribute Manager
Freeware Utilities
 AddrMon
 AFileSync
 ASysIcon
 B&P Table Utilities
 BPACLer
 BPSNMPMon
 BPSNMPUtil
 CharCount
 Delphi® Components
 MacAddr
Miscellaneous
 BPSoftware Blog
 Purchase Shareware
 Support

 


Saturday, March 03, 2007
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.
1namespace DrawInDataGrid1
2{
3 public partial class Form1 : Form
4 {
5 private DataTable myTable;
6 private DataGridViewCheckBoxColumn checkcolumn;
7 private DataGridViewButtonColumn buttoncolumn;
8 private DataColumn colItem1, colItem2, colItem3;
9 private DataRow NewRow;
10 private DataView myView;
11
12 public Form1()
13 {
14 InitializeComponent();
15 }

16
17 private void Form1_Load(object sender, EventArgs e)
18 {
19 // DataTable to hold data that is displayed in DataGrid
20 myTable = new DataTable("myTable");
21
22 // the three columns in the table
23 colItem1 = new DataColumn("CheckBox", Type.GetType("System.Boolean"));
24 colItem2 = new DataColumn("Button", Type.GetType("System.String"));
25 colItem3 = new DataColumn("String", Type.GetType("System.String"));
26
27 // add the columns to the table
28 myTable.Columns.Add(colItem1);
29 myTable.Columns.Add(colItem2);
30 myTable.Columns.Add(colItem3);
31
32 checkcolumn = new DataGridViewCheckBoxColumn(false);
33 checkcolumn.HeaderText = "CheckBox";
34 checkcolumn.DataPropertyName = "CheckBox";
35
36 buttoncolumn = new DataGridViewButtonColumn();
37 buttoncolumn.HeaderText = "Button";
38 buttoncolumn.DataPropertyName = "Button";
39
40 dataGridView1.Columns.Add(checkcolumn);
41 dataGridView1.Columns.Add(buttoncolumn);
42
43
44 // Fill in some data
45 NewRow = myTable.NewRow();
46 NewRow[0] = true;
47 NewRow[1] = "0";
48 NewRow[2] = "Test";
49 myTable.Rows.Add(NewRow);
50
51 NewRow = myTable.NewRow();
52 NewRow[0] = false;
53 NewRow[1] = "1";
54 NewRow[2] = "Next One";
55 myTable.Rows.Add(NewRow);
56
57 // DataView for the DataGridView
58 myView = new DataView(myTable);
59 myView.AllowDelete = false;
60 myView.AllowEdit = false;
61 myView.AllowNew = false;
62
63 // add an event to check for button click
64 this.dataGridView1.CellContentClick +=
65 new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
66
67 // Assign DataView to DataGrid
68 dataGridView1.DataSource = myView;
69 }

70
71
72 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
73 {
74 if (sender is DataGridView)
75 {
76 if ((((DataGridView)sender).Columns[e.ColumnIndex] is DataGridViewButtonColumn) &&
77 (e.RowIndex >= 0))
78 {
79 MessageBox.Show(e.RowIndex.ToString());
80 }

81 }

82 }

83 }

84}



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

Labels: , ,

posted by Brad Prendergast at 1:52:00 PM
Comments:
thnks on help wiring the CellContentClick event.
posted by Anonymous Anonymous Thursday, April 12, 2007 12:10:00 PM  
Links to this post:

Create a Link

Recent Posts
 Those Commercials
 System Information and the PerformanceCounter Clas...
 Cryptography hash and a class - to go
 C# LocalAdapterInformation
 PInvoke WIN32 and .NET
 ASP.NET upload a file HtmlInputFile Control
 PPCTL.DLL is damaged and could not be repaired.
 Google Mobile SMS
 Running a .NET Framework application from a networ...
 I am Green Lantern!


Labels



Archives
 October 2005
 November 2005
 December 2005
 January 2006
 February 2006
 March 2006
 April 2006
 May 2006
 June 2006
 July 2006
 August 2006
 September 2006
 December 2006
 January 2007
 February 2007
 March 2007
 September 2007
 October 2007
 November 2007
 July 2008
 November 2008
Powered by Blogger