i added checkbox to datagridview
like this
Dim CbxColumn As New DataGridViewCheckBoxColumn
With CbxColumn
.HeaderText = ""
.Name = "Return"
.Width = 50
End With
dgvDetail.Columns.Insert(0, CbxColumn)
when i run it show correctly but now i want to disable some rows on dataGridView dynamically not every row just some rows depend on other value in that rows i mean when column2 have value "Open" i try to do like this
For i = 0 To dgvDetail.Rows.Count - 1
If dgvDetail.Rows(i).Cells(1).Value = "Open" Then
//I want to do what i expect here//
dgvDetail.Rows(i).Cells(1).ReadOnly = True
End If
Next
but it's just can't edit value but i prefer want it to disable as grey color or inactive control like when we set buttoncontrol.enabled=false
What should i do Thanks so much
Try This :
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
if (e.RowIndex == 1)
{
DataGridViewCell cell=dataGridView1.Rows[e.RowIndex].Cells[0];
DataGridViewCheckBoxCell chkCell = cell as DataGridViewCheckBoxCell;
chkCell.Value = false;
chkCell.FlatStyle = FlatStyle.Flat;
chkCell.Style.ForeColor = Color.DarkGray;
cell.ReadOnly = true;
}
}
为什么不像刚才提到的那样只禁用单元格(只读),还要将其设置为BackColor
:
dgvDetail.Item(1, i).Style.BackColor = Color.LightGray
You can simply achieve this by disabling the DataGridViewCell
as
private void enableCell(DataGridViewCell row, bool enabled) {
//toggle read-only state
row.ReadOnly = !enabled;
if (enabled)
{
//restore cell style to the default value
row.Style.BackColor = row.OwningColumn.DefaultCellStyle.BackColor;
row.Style.ForeColor = row.OwningColumn.DefaultCellStyle.ForeColor;
}
else {
//gray out the cell
row.Style.BackColor = Color.LightGray;
row.Style.ForeColor = Color.DarkGray;
}
}
or you can extent the above code to disable the whole DataGridViewRow
by iterating through each cell.
Please check this answer: https://stackoverflow.com/a/1626948/1361234
And I found a project to do this: http://www.codeproject.com/Articles/31829/Disabled-Checkbox-Column-in-the-DataGridView
But, I used the method in @Thirisangu 's answer, it can work. Thank you, Thirisangu.
chkCell.FlatStyle = FlatStyle.Flat;
chkCell.Style.ForeColor = Color.DarkGray;
cell.ReadOnly = true;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.