Reputation: 183
I have a datagridview that is loading by the datatable..
//dgvApprovazione is my DataGridView
//dt is my DataTable
dgvApprovazione.DataSource = dt;
DataTable represent part of table in db.
I try with this code
foreach (DataGridViewRow row in dgvApprovazione.Rows)
{
if (row.Cells[3].Value != null)
{
//CheckBox ckb = row.Cells[3] is check
if ((Convert.ToBoolean(row.Cells[3].Value)) == true)
{
row.Cells[3].ReadOnly = true;
}
}
}
But i think that problem is in setting DataTable dt
...
How i delete that checkbox ?!?
And is a second question .. how i delete a last row in datatable AND in datagridview .
Upvotes: 2
Views: 2433
Reputation: 125197
To disable adding new row (remove the last new row):
DataGridView.AllowUserToAddRows
to false
.To keep adding new row enabled but hide and disable CheckBox
CellPainting
and don't render the check boxCellContentClick
and check if the cell is in the last row don't do anythingReadOnly
property to false
.If you want to don't render the check box, you should handle CellPainting
event and prevent drawing the check box:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
// I suppose your check box is at column index 0
if (e.ColumnIndex == 0 && e.RowIndex == dataGridView1.NewRowIndex)
{
e.PaintBackground(e.ClipBounds, true);
e.Handled = true;
}
}
This doesn't prevent a click on the cell, if you want to prevent running the logic that you have on click for last row, you should handle CellContentClick
event and check if the clicked cell is not the new row:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// I suppose your check box is at column index 0
// To exclude header cell: e.RowIndex >= 0
// To exclude new row: celle.RowIndex!=dataGridView1.newRowIndex
if (e.ColumnIndex == 0 && e.RowIndex >= 0 && e.RowIndex!=dataGridView1.newRowIndex)
{
//Put the logic here
}
}
Upvotes: 3
Reputation: 122
To delete last row DataGridView.Rows.RemoveAt(DataGridView.Rows.Count - 1)
Same thing for: DataTable.Rows.RemoveAt(DataTable.Rows.Count - 1)
To not allow new empty rows at all set DataGridView.AllowUserToAddRows
Also there is not much of a point in deleting that checkbox from logic perspective as it belongs to the checkbox column and is supposed to be there.
Upvotes: 1