Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"ThreeState" In datagrids

Status
Not open for further replies.

Thumborg

Programmer
Nov 23, 2004
1
DK
I'm having trouble with my datagrids.
I have a datagrid where one of the columns is the data type Bool. When the datagrid is shown on the form, this column consists of checkboxes. All of this is very beautiful except the fact that the checkboxes has three states:

Not checked = false
Marked grey = NULL
Marked black = true

I can set the AllowDBNull = false, but this only brings up an error saying that NULL values are not allowed.

When I put normal checkboxes on a form there are no difficulties setting the ThreeState = false which gives the desired effect.

Are there any one out there who can help me ?
 
afaik you can set values for all three states... so for grey set it to false and this should solve your problem.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
If you have a DataGrid called dgMyGrid that's based on a DataView called dgMyView you can do something like this:

Code:
// Set up a new table style and map its name to the source table's name
DataGridTableStyle tsMyStyle = new DataGridTableStyle();
tsMyStyle.MappingName = dvMyView.Table.TableName;

// Clear any existing styles and add our new one
dgMyGrid.TableStyles.Clear();
dgMyGrid.TableStyles.Add(tsMyStyle);

// Loop through each column in the style
foreach(DataGridColumnStyle csColStyle in dgMyGrid.TableStyles[0].GridColumnStyles)
{
	Type tCol = csColStyle.GetType();
	if(tCol.ToString().Equals("System.Windows.Forms.DataGridBoolColumn"))
		((System.Windows.Forms.DataGridBoolColumn)(csColStyle)).AllowNull = false;
}

It works for me. It's based on something I picked up here very recently.

Regards

Nelviticus
 
You can also do it with MouseUp Event on DataGrid

Code:
Globals.ReporterUpdate.getUsers();
this.dgUsers.TableStyles.Clear();
this.dgUsers.TableStyles.Add(createStyles());
DataTable dtUsers = Globals.DataHolder.Tables[DataHolder.TableNameUsers];
dtUsers.Columns["ReporterUpdate"].DefaultValue = 0;
dtUsers.Columns["ReporterAdmin"].DefaultValue = 0;

/// <summary>
/// Handles nullvalue in checkboxes 
/// </summary>
/// <param name="sender">dataGrid</param>
/// <param name="e">The Event MouseUp</param>
private void dgUsers_MouseUp(object sender, MouseEventArgs e) {
try {
DataGrid.HitTestInfo hti = this.dgUsers.HitTest(e.X, e.Y);
if (hti.Type == DataGrid.HitTestType.Cell) {
   if (hti.Column == 3 || hti.Column == 4) {
   this.dgUsers[hti.Row, hti.Column] = ! (bool) this.dgUsers[hti.Row, hti.Column];
}
}
} catch (Exception ex) {
  this.lblSystemMsg.Text = ex.Message.ToString();
}
}

"ReporterAdmin" and "ReporterUser" is DataGridBoolColums
with columnposition 3 and 4 on every row on the datagrid.

sbd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top