110,502
社区成员
发帖
与我相关
我的任务
分享
//第二行,第二列,选中无反应效果
private void Form_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("a", typeof(string));
dt.Columns.Add("b", typeof(string));
dt.Rows.Add(new object[] { "fad", "fdfsdf" });
dt.Rows.Add(new object[] { "fad", "fdfsdf" });
dt.Rows.Add(new object[] { "fad", "fdfsdf" });
dt.AcceptChanges();
this.dataGridView1.DataSource = dt;
this.dataGridView1[1, 1].ReadOnly = true;
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if ((e.RowIndex == 1) && (e.ColumnIndex == 1) &&
((e.PaintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
)
{
SolidBrush bush = new SolidBrush(e.CellStyle.BackColor);
try
{
e.Graphics.FillRectangle(bush, e.CellBounds);
}
finally
{
bush.Dispose();
}
DataGridViewPaintParts paintparts = e.PaintParts & (~DataGridViewPaintParts.Background);
paintparts = paintparts & (~DataGridViewPaintParts.Focus);
paintparts = paintparts & (~DataGridViewPaintParts.SelectionBackground);
e.Paint(e.CellBounds, paintparts);
TextRenderer.DrawText(e.Graphics, e.FormattedValue.ToString() , e.CellStyle.Font, e.CellBounds, e.CellStyle.ForeColor, e.CellStyle.BackColor,TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
e.Handled = true;
}
}
//CellBeginEdit事件处理器
private void DataGridView1_CellBeginEdit(object sender,
DataGridViewCellCancelEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
//判断是否可以编辑
if (dgv.Columns[e.ColumnIndex].Name == "Column1" &&
!(bool)dgv["Column2", e.RowIndex].Value)
{
//编辑不能
e.Cancel = true;
}
}
//DataGridView1的(0, 0)的单元格只读
DataGridView1[0, 0].ReadOnly = true;