Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Custom DataGridView cell
 

Custom DataGridView cell

I have created a custom DataGridView cell class for both display and editing.

The cell paints its own background. The edit control, which is based on the TextBox clasl sits in the middle of the cell height so there is space above and below it.

For display all is ok.

However when I go into edit mode in my cell, something somewhere is repainting the cell background to the default cell style background colour and I can't see where or how to override/stop this.

So say the default cell style is white. I paint my custom cell background to yellow. When displaying, all is ok and the background is yellow.

As soon as I enter edit mode, my edit control becomes active. I can set the background of my edit control to yellow ok to match the background of the rest of my cell but the rest of the cell not covered by the edit control , i.e the space above and below it, changes to white, the default cell style backgoround as soon as the first key is pressed and edit mode becomes active. Debuggiing shows my cell painting code is being executed but something in the datagridview elsewhere is resetting the background colour of the none editing parts of my cell when edit mode is entered.

I want my custom background painting forthe whole cell to be there when displaying and editing.

Steve98796  Thursday, November 01, 2007 10:58 AM

I need to paint the background for other reasons. The problem was the colour was chaging when I went into editing mode.

I worked out the problem was with the EditingPanel that is created by the grid and the following code resolved it:

Code Block

public override Rectangle PositionEditingPanel(Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
{
this.DataGridView.EditingPanel.BackColor = Color.Yellow;

return base.PositionEditingPanel(cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow);
}

Steve98796  Tuesday, November 06, 2007 10:34 AM

You don't need to do the painting stuff in the custom cell, just setStyle.BackColor property for the specify cell as:

this.dataGridView1[0, 1].Style.BackColor = Color.Red;

this approach would fix your problem.

Zhi-Xin Ye  Tuesday, November 06, 2007 10:08 AM

I need to paint the background for other reasons. The problem was the colour was chaging when I went into editing mode.

I worked out the problem was with the EditingPanel that is created by the grid and the following code resolved it:

Code Block

public override Rectangle PositionEditingPanel(Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
{
this.DataGridView.EditingPanel.BackColor = Color.Yellow;

return base.PositionEditingPanel(cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow);
}

Steve98796  Tuesday, November 06, 2007 10:34 AM

You can use google to search for other answers

Custom Search

More Threads

• Why DataGrid cell focus chg force DataTable.ColumnChanging event?
• Help with RadioButton properties
• Why DataGridView columns are re arranged after data binding?
• Double click ListBox
• TableAdapter update problem in parent/child relationship
• Foreign Key columns not always filled up when adding new rows in a datagridview bind to a BindingSource
• datagridview sorting
• datagridview and dataset.table rowindex don't match
• New WinForms FAQ repository created
• DataGridView with toolBar (toolStrip) binding bug?