dataGridView问题???

修改一下昵称 2009-05-13 10:54:25
一个dataGridView中,我想某些单元格不能选择,比如就像控件的Enabled属性设为false一样,单击它没有响应!
这个怎么实现?
...全文
210 29 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
修改一下昵称 2009-05-15
  • 打赏
  • 举报
回复
刚刚经过我的一些修改,和测试,在我的程序里完全可以达到我要的那个效果了。

非常感谢25楼!

问题解决,结贴去!!
修改一下昵称 2009-05-15
  • 打赏
  • 举报
回复
先谢谢你了,这样确实可以达到我要的那个效果了。
tanjian0503 2009-05-15
  • 打赏
  • 举报
回复
再次能交流到不少东西啊
welcomechenqi 2009-05-15
  • 打赏
  • 举报
回复
查下帮助文档里的属性
duping9626 2009-05-14
  • 打赏
  • 举报
回复
效果像是没反应,不过只是为了达到这样的效果,直接用可能不行,可能会有其它的没考虑的问题

//第二行,第二列,选中无反应效果
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;
}
}
CeleryZeng 2009-05-14
  • 打赏
  • 举报
回复
我是将字段换成模版
在rowdatabind里面实现就可以了

Label l = e.Row.Cells[5].Controls[1] as Label;
l.Enabled = false;
修改一下昵称 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 Dobzhansky 的回复:]
引用 22 楼 yhb417 的回复:
等待高手来解决问题。。


不知道你为啥要这个功能, 只读不够?
[/Quote]
能实现是最好咯。
我想了一下,看能不能根据鼠标移动到某个单元格的事件来屏蔽鼠标左键??
Kkiki 2009-05-14
  • 打赏
  • 举报
回复
编辑模板中 去掉textBox 换成 label
Dobzhansky 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 yhb417 的回复:]
等待高手来解决问题。。
[/Quote]

不知道你为啥要这个功能, 只读不够?
footprint2008 2009-05-14
  • 打赏
  • 举报
回复
这个好像没有这样的属性.但可以设置为只读.
DataGridView dgv = new DataGridView();
dgv[i, i].ReadOnly = true; //单元格只读第(i+1)列第(i+1)行只读
dgv.Columns[i].ReadOnly = true; //第(i+1)列只读
shui8iuhs 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 outou 的回复:]
代码来自http://bingning.net/VB/SOURCE/datagridview/readonly.html
根据条件单元格不能编辑

C# code
//CellBeginEdit事件处理器
private void DataGridView1_CellBeginEdit(object sender,
DataGridViewCellCancelEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
//判断是否可以编辑
if (dgv.Columns[e.ColumnIndex].Name == "Column1" &&
!(bool)dgv["Column2", e.Ro…
[/Quote]

同意,正解,这种方法可行。
修改一下昵称 2009-05-14
  • 打赏
  • 举报
回复
等待高手来解决问题。。
outou 2009-05-14
  • 打赏
  • 举报
回复
代码来自http://bingning.net/VB/SOURCE/datagridview/readonly.html
根据条件单元格不能编辑

//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;

zjl0422 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 Kkiki 的回复:]
编辑模板中 去掉textBox 换成 label
[/Quote]
这样也可以吗?
zjl0422 2009-05-14
  • 打赏
  • 举报
回复
up
修改一下昵称 2009-05-14
  • 打赏
  • 举报
回复
......
修改一下昵称 2009-05-14
  • 打赏
  • 举报
回复
...
修改一下昵称 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 wuyq11 的回复:]
if (dgv.Columns[e.ColumnIndex].Name == "Column1" && !(bool)dgv["Column2", e.RowIndex].Value)
{
e.Cancel = true;
}
或 ReadOnly = true;
[/Quote]
不是让它不能编辑,我是想让它单击没响应,就像用右键单击单元格一样!
wuyq11 2009-05-14
  • 打赏
  • 举报
回复
if (dgv.Columns[e.ColumnIndex].Name == "Column1" && !(bool)dgv["Column2", e.RowIndex].Value)
{
e.Cancel = true;
}
或 ReadOnly = true;
cjdxhc 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yhb417 的回复:]
.
[/Quote]

设置每列的ReadOnly属性为true即可!
加载更多回复(9)
vb.net操作DataGridView控件的用法的集合,包括: 1. DataGridView当前的单元格属性取得、变更 2. DataGridView编辑属性 3. DataGridView最下面一列新追加行非表示 4. DataGridView判断当前选中行是否为新追加的行 5. DataGridView删除行可否设定 6. DataGridView行列不表示和删除 DataGridView控件用法合集(二) 7. DataGridView行列宽度高度设置为不能编辑 8. DataGridView行高列幅自动调整 9. DataGridView指定行列冻结 10. DataGridView列顺序变更可否设定 11. DataGridView行复数选择 12. DataGridView选择的行、列、单元格取得 DataGridView控件用法合集(三) 13. DataGridView指定单元格是否表示 14. DataGridView表头部单元格取得 15. DataGridView表头部单元格文字列设定 16. DataGridView选择的部分拷贝至剪贴板 17.DataGridView粘贴 18. DataGridView单元格上ToolTip表示设定(鼠标移动到相应单元格上时,弹出说明信息) DataGridView控件用法合集(四) 19. DataGridView中的ContextMenuStrip属性 20. DataGridView指定滚动框位置 21. DataGridView手动追加列 22. DataGridView全体分界线样式设置 23. DataGridView根据单元格属性更改显示内容 24. DataGridView新追加行的行高样式设置る 25. DataGridView新追加行单元格默认值设置 DataGridView中输入错误数据的处理(五) 26. DataGridView单元格数据错误标签表示 27. DataGridView单元格内输入值正确性判断 28. DataGridView单元格输入错误值事件的捕获 DataGridView控件用法合集(六) 29. DataGridView行排序(点击列表头自动排序的设置) 30. DataGridView自动行排序(新追加值也会自动排序) 31. DataGridView自动行排序禁止情况下的排序 32. DataGridView指定列指定排序 DataGridView控件用法合集(七) 33. DataGridView单元格样式设置 34. DataGridView文字表示位置的设定 35. DataGridView单元格内文字列换行 36. DataGridView单元格DBNull值表示的设定 37. DataGridView单元格样式格式化 38. DataGridView指定单元格颜色设定 39. DataGridView单元格文字字体设置 40. DataGridView根据单元格值设定单元格样式 DataGridView控件用法合集(八) 41. DataGridView设置单元格背景颜色 42. DataGridView行样式描画 43. DataGridView显示行号 44. DataGridView焦点所在单元格焦点框不显示的设定 DataGridView控件用法合集(九) 45. DataGridView中显示选择框CheckBox 46. DataGridView中显示下拉框ComboBox 47. DataGridView单击打开下拉框 48. DataGridView中显示按钮 49. DataGridView中显示链接 50. DataGridView中显示图像 DataGridView控件用法合集(十) 51. DataGridView编辑中单元格控件取得 52. DataGridView输入自动完成 53. DataGridView单元格编辑时键盘KEY事件取得 54. DataGridView下拉框(ComboBox)单元格编辑时事件取得 55. DataGridView下拉框(ComboBox)单元格允许文字输入设定 DataGridView控件用法合集(十一) 56. DataGridView根据值不同在另一列中显示相应图片 57. DataGridView中显示进度条(ProgressBar) 58. DataGridView中添加MaskedTextBox DataGridView控件用法合集(十二) 59. DataGridView中Enter键按下焦点移至旁边的单元格 60. DataGridView行集合化(Group)

111,110

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧