all groups > dotnet windows forms databinding > september 2006 >
You're in the

dotnet windows forms databinding

group:

Forcing DataGridView to repaint a row/disabling a row


Forcing DataGridView to repaint a row/disabling a row Sam Carleton
9/18/2006 7:37:04 AM
dotnet windows forms databinding:
I have a datagridview that is bound to a datasource. The under lying
data is not from a database, but biz objects. The first colume (InUse)
is a check box to determine if the rest of the row is enabled. When
the row is not in use, it is grayed out in the CellFormatting event.
After the user checks the InUse colume, the rest of the row does not
get repainted.

Is there a better way to control editablity of individual rows? If
not, how do I go about making the whole row get updated correctly? As
I click on the different cells and leave them, they change to the
correct color.

Sam
Re: Forcing DataGridView to repaint a row/disabling a row Bart Mermuys
9/18/2006 9:51:34 PM
Hi,

[quoted text, click to view]

Try CellPainting and get the *editing (or stored)* value then set forecolor
and enable/disable the other cells:

private void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if ((e.RowIndex != -1) && (e.ColumnIndex > 0))
{
DataGridViewRow currentRow = dataGridView1.Rows[e.RowIndex];

// returns editing or stored value (from checkboxcell)
bool inUse = (bool)currentRow.Cells[0].EditedFormattedValue;

e.CellStyle.ForeColor = inUse ? Color.Black : Color.Gray;
for (int i = 1; i < dataGridView1.Columns.Count; ++i)
currentRow.Cells[i].ReadOnly = !inUse ;
}
}

Dedect whether the CheckBox is changed during edit and trigger a row
invalidate:

private void dataGridView1_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
dataGridView1.InvalidateRow(e.RowIndex);
}

private void dataGridView1_CellContentDoubleClick(object sender,
DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
dataGridView1.InvalidateRow(e.RowIndex);
}

private void dataGridView1_CurrentCellDirtyStateChanged(object sender,
EventArgs e)
{
// when user cancel's checkbox edit, dirty state reverts to false
if ( dataGridView1.CurrentCell.ColumnIndex == 0 &&
!dataGridView1.IsCurrentCellDirty )
dataGridView1.InvalidateRow(dataGridView1.CurrentCell.RowIndex);
}



HTH,
Greetings


[quoted text, click to view]

AddThis Social Bookmark Button