Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridViewCheckBoxColumn with Text Information
 

DataGridViewCheckBoxColumn with Text Information

[C# Windows Form]
I would like to have one of myDataGridView columns as CheckBox column with a text displayed next to the checkbox . using DataGridViewCheckBoxColumn I am able to display the checkbox but the text info. Is there any solution for this?

I tried inherting DataGridViewColum and added a checkbox to it, but CheckBox is  getting displayed in the cell which has the focusSad.

 

DotNetGroup  Friday, November 18, 2005 9:55 PM
One way to do it is to inherit from the DataGridViewCheckBox and re-use the DataGridViewCheckBox's painting implementation as much as possible. Note that with this implementation the text will not be editable...

Key to re-using the base painting implementation is getting the check box bounds. The check box bounds are also the content bounds for the data grid view check box cell. Using the content bounds for the data grid view check box cell we can determine the location where to draw the string.

Here is a possible implementation.

Hope this helps.

public class MyDGVCheckBoxColumn : DataGridViewCheckBoxColumn

{

public override DataGridViewCell CellTemplate

{

get

{

return new MyDGVCheckBoxCell();

}

}

}

public class MyDGVCheckBoxCell : DataGridViewCheckBoxCell

{

protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)

{

// the base Paint implementation paints the check box

base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

// now let's paint the text

// Get the check box bounds: they are the content bounds

Rectangle contentBounds = this.GetContentBounds(rowIndex);

// Compute the location where we want to paint the string.

Point stringLocation = new Point();

// Compute the Y.

// NOTE: the current logic does not take into account padding.

stringLocation.Y = cellBounds.Y + 2;

// Compute the X.

// Content bounds are computed relative to the cell bounds

// - not relative to the DataGridView control.

stringLocation.X = cellBounds.X + contentBounds.Right + 2;

// Paint the string.

graphics.DrawString("foo", Control.DefaultFont, System.Drawing.Brushes.Red, stringLocation);

}

}

Daniel Herling - Student  Friday, November 18, 2005 11:08 PM

Here is some code that paints the text in the check box column according to whether the cell is selected or not:

// Paint the string.

// Checking the Data Grid View Cell's Selected property un-shares the row.

// So use the elementState to determine if the cell is selected or not.

// This works w/ selected rows and selected columns as well.

if ((elementState & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected)

{

graphics.DrawString("Selected", Control.DefaultFont, System.Drawing.Brushes.Red, stringLocation);

}

else

{

graphics.DrawString("NOTSelected", Control.DefaultFont, System.Drawing.Brushes.Black, stringLocation);

}


Hope this helps.
Daniel.

Daniel Herling - Student  Tuesday, November 22, 2005 12:31 AM
One way to do it is to inherit from the DataGridViewCheckBox and re-use the DataGridViewCheckBox's painting implementation as much as possible. Note that with this implementation the text will not be editable...

Key to re-using the base painting implementation is getting the check box bounds. The check box bounds are also the content bounds for the data grid view check box cell. Using the content bounds for the data grid view check box cell we can determine the location where to draw the string.

Here is a possible implementation.

Hope this helps.

public class MyDGVCheckBoxColumn : DataGridViewCheckBoxColumn

{

public override DataGridViewCell CellTemplate

{

get

{

return new MyDGVCheckBoxCell();

}

}

}

public class MyDGVCheckBoxCell : DataGridViewCheckBoxCell

{

protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)

{

// the base Paint implementation paints the check box

base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

// now let's paint the text

// Get the check box bounds: they are the content bounds

Rectangle contentBounds = this.GetContentBounds(rowIndex);

// Compute the location where we want to paint the string.

Point stringLocation = new Point();

// Compute the Y.

// NOTE: the current logic does not take into account padding.

stringLocation.Y = cellBounds.Y + 2;

// Compute the X.

// Content bounds are computed relative to the cell bounds

// - not relative to the DataGridView control.

stringLocation.X = cellBounds.X + contentBounds.Right + 2;

// Paint the string.

graphics.DrawString("foo", Control.DefaultFont, System.Drawing.Brushes.Red, stringLocation);

}

}

Daniel Herling - Student  Friday, November 18, 2005 11:08 PM

Great!
This is working fine!!!.Smile



DotNetGroup  Monday, November 21, 2005 5:06 PM
When the focus goes to a particular row, i am chaning forecolor for the entire row content and the color is getting changed except this CheckBox text. Is there way to fix this?
DotNetGroup  Monday, November 21, 2005 11:03 PM
The color that paints the CheckBox text is hard coded to RED: see the code where it paints the text. I thought this way it will be more obvious how the text will look next to the CheckBoxes.

However, you can use any color to paint the text, it does not have to be RED. For example, you can use the ForeColor from the cellStyle that is passed into the Paint method.

Hope this helps.
Daniel Herling - Student  Monday, November 21, 2005 11:46 PM
But the paint method changes the color of the entire column. My requirement is to change the forecolor for all the cells of a selected ROW. In my case, this checkBox column(with text) is one of the cell of the seleted row. I would like to change fore color of selected Row's checkBox column's text and rest of the rows's CheckBoxColumn's text should be in black color.

 (In the paint method i have given the color as balck so the entire column's foreColor is Black. )
DotNetGroup  Monday, November 21, 2005 11:55 PM

Here is some code that paints the text in the check box column according to whether the cell is selected or not:

// Paint the string.

// Checking the Data Grid View Cell's Selected property un-shares the row.

// So use the elementState to determine if the cell is selected or not.

// This works w/ selected rows and selected columns as well.

if ((elementState & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected)

{

graphics.DrawString("Selected", Control.DefaultFont, System.Drawing.Brushes.Red, stringLocation);

}

else

{

graphics.DrawString("NOTSelected", Control.DefaultFont, System.Drawing.Brushes.Black, stringLocation);

}


Hope this helps.
Daniel.

Daniel Herling - Student  Tuesday, November 22, 2005 12:31 AM
Great!. It works.
Thanks.
:)
DotNetGroup  Tuesday, November 22, 2005 12:38 AM
Hello Daniel,

Nice solution, very useful. Thanks.
But I am having one problem here in my solution.
I am setting text associated with checkbox from the cell's tag.

graphics.DrawString(this.Tag.ToString(), Control.DefaultFont, System.Drawing.Brushes.Black, stringLocation);

So that I can have different text for different cells.

However, the long text is getting clipped or trimmed by the width of the cell. String sizes can vary and not known in advance. So I can't the hardcode width of the column.
I tried setting AutoSizeMode for the column as AllCells, but it didn't work.

Please help me out...
Wayne Disney  Tuesday, September 15, 2009 9:23 AM

You can use google to search for other answers

Custom Search

More Threads

• Displaying and Storing Data bound image in Windows Form form SQL Server
• How can I remove a row from the datagridview control in an interactive manner
• textfile in a gridview
• accessing columns in a Tableadapter not dragged on to the form
• Combobox binding and adding new column
• Datagrid Operations in Vb.net
• Enter event not working on DataGrid?
• filtering a datagridview
• To increase datagrid ComboBox Drop Down Length
• "Traffic lights" chart in Datagridviewcell?