We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

DataGridBoolColumn

I am trying to create the following behavior. I have a WinForm that contains a DataGrid, bound to a Dataset. One of the columns is a DataGridBoolColumn. When the grid is not selected (ie. no row currently selected) clicking on a check box only sets focus to the cell, but requires a second click to actually check (or uncheck) the control. I tried capturing the CurrentCellChanged event of the grid, and I am able to determine the row and col, but when I try to use the GetColValueAtRow and SetColValueAtRow functions, I am told that they are marked internal and I can't use them. I would hope that their would be a way to accomplish this?

3 Replies

CB Clay Burch Syncfusion Team May 21, 2002 05:44 AM UTC

The DataGridBoolColumn does not fire a change event when the boolean value changes. The FAQ referenced below derives this class and adds a change event that includes a row & column & value in its EventArgs. This may give you what you need for tracking the value of an active cell. In regard to it taking 2 clicks if the cell is not active, I don't have a tested solution for you. But I think this code in the Edit override of the sample should flip the value on any initial click. You may way to try to restrict the change to only when the mouse position is over the checkbox bitmap (instead of a click anywhere in the cell). protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) { saveValue = (bool) base.GetColumnValueAtRow(source, rowNum); if(Control.MouseButtons == MouseButtons.Left) { saveValue = !saveValue; base.SetColumnValueAtRow(source, rowNum, saveValue); //fire the event if(BoolValueChanged != null) { BoolValueChangedEventArgs e = new BoolValueChangedEventArgs(rowNum, _column, saveValue); BoolValueChanged(this, e); } } lockValue = true; beingEdited = true; saveRow = rowNum; base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible); } ================================================ The Windows Forms FAQ contains an entry entitled: How can I catch the bool values changing in a DataGridBoolColumn? http://www.syncfusion.com/faq/winforms/search/874.asp


CB Clay Burch Syncfusion Team May 21, 2002 06:05 AM UTC

Here is some code that only switches on the first click if it hits the checkbox (not just the cell)
protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
	saveValue = (bool) base.GetColumnValueAtRow(source, rowNum);
	if(Control.MouseButtons == MouseButtons.Left)
	{
		Point mousePos = this.DataGridTableStyle.DataGrid.PointToClient(Control.MousePosition);
		DataGrid dg = this.DataGridTableStyle.DataGrid;
		Rectangle rect = dg.GetCellBounds(dg.CurrentCell);
				
		//define the checkbox hit size somehow
		Size checkiconsize = new Size(20, 20);//SystemInformation.SmallIconSize; //however you want to define the hit-area
		//assume checkbox is centered
		rect = new Rectangle(rect.Left + (rect.Width- checkiconsize.Height) / 2,
				rect.Top + (rect.Height - checkiconsize.Height) / 2,
				checkiconsize.Width, checkiconsize.Height);
				
		if(rect.Contains(mousePos))
		{
			saveValue = !saveValue;
			base.SetColumnValueAtRow(source, rowNum, saveValue);
			//fire the event
			if(BoolValueChanged != null)
			{
				BoolValueChangedEventArgs e = new BoolValueChangedEventArgs(rowNum, _column, saveValue);
				BoolValueChanged(this, e);
			}
		}

		lockValue = true;
		beingEdited = true;
		saveRow = rowNum;

		base.Edit(source, rowNum,  bounds, readOnly, instantText, cellIsVisible);
	}
}


BT boB Taylor MCSD, MCDBA, MCSE May 21, 2002 05:32 PM UTC

This is what I wound up with. I think it is simpler approach. public class SingleClickDataGrid : DataGrid { public SingleClickDataGrid() { this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.GridMouseDown); } public void GridMouseDown(object sender, MouseEventArgs e) { System.Windows.Forms.DataGrid.HitTestInfo hi; // let's see where we clicked... hi = HitTest(e.X,e.Y); // If the user did not click on a cell, we don't care... // we'll just pass on to the base class if(hi.Type == System.Windows.Forms.DataGrid.HitTestType.Cell) { // Get the TableStyles collection DataGridTableStyle dgt = TableStyles[0]; // Get the ColumnStyle for the selected column DataGridColumnStyle myCol = dgt.GridColumnStyles[hi.Column]; // Is this a check box column? // if not, we'll let the base class handle it if(myCol.GetType() == typeof(DataGridBoolColumn)) { // First part of the magic... select the row, so that the edit can happen Select(hi.Row); // Call the grid table style to start and end the edit dgt.BeginEdit(myCol,hi.Row); dgt.EndEdit(myCol,hi.Row,true); } } } }

Loader.
Up arrow icon