一个dataGridView中,我想某些单元格不能选择,比如就像控件的Enabled属性设为false一样,单击它没有响应!
这个怎么实现?
这个怎么实现?
解决方案 »
- C# 自定义网络协议的问题,怎样写协议才可以使发送byte[]数组到服务端后服务端可以轻松解析成键值对形式,或者说怎样写既方便传输又方便解析
- c# Image Fromfile 资源释放
- c# MouseHover的悬停时间如何改长一点?
- 问一个存储过程的简单问题
- 寻:lovefootball(蟑螂) 帮我解决问题!!
- 如何在picturebox控件里的图片中指定一个正方形的区域,点击那个区域则触发指定的函数
- 第一次写原创文章,关于VC#.NET的组件入门篇,大家支持一下.顺便给点意见.小弟在这先谢了.
- 页面刷新密码怎么保留阿?
- 又送100分,老问题还没有解决,新问题又来了
- C#父类获取子类 引用
- 新手求高手给一个测试方法效率代码
- 俄罗斯方块的声音问题!!!只能播放一次 然后停止 再播放就不可以了
DataGridView dgv = new DataGridView();
dgv[0, 0].ReadOnly = true; //单元格只读第1列第1行只读
dgv.Columns[0].ReadOnly = true; //第1列只读
设置每列的ReadOnly属性为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;
}
}(0, 0)的单元格只读//DataGridView1的(0, 0)的单元格只读
DataGridView1[0, 0].ReadOnly = true;
DataGridView dgv = new DataGridView();
dgv[i, i].ReadOnly = true; //单元格只读第(i+1)列第(i+1)行只读
dgv.Columns[i].ReadOnly = true; //第(i+1)列只读
在rowdatabind里面实现就可以了 Label l = e.Row.Cells[5].Controls[1] as Label;
l.Enabled = false;
{
e.Cancel = true;
}
或 ReadOnly = true;
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;
}
}