首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

dataGridView列的只读属性不起作用,该如何处理

2012-05-28 
dataGridView列的只读属性不起作用程序中dataGridView先是用来显示一个内存表,点击一个切换按钮后再让data

dataGridView列的只读属性不起作用
程序中dataGridView先是用来显示一个内存表,点击一个切换按钮后再让dataGridView绑定到数据源上的。
  想让dataGridView中的第一列为可编辑,其他列均为只可读,第一列是编程插入的,其他列是绑定到数据表上的。理论上采用dataGridView.Columns[i].ReadOnly是可以每一列进行设置的,但实际却没起作用。代码如下:

C# code
                this.dataGridView1.ReadOnly = false;                for (int i = 1; i <= this.dataGridView1.DisplayedColumnCount(false); i++)                {                    if (i == 1)                    {                        this.dataGridView1.Columns[i].ReadOnly = false;                    }                    else                    {                        this.dataGridView1.Columns[i].ReadOnly = true;                    }                }


  单步执行时已经显示第一列的ReadOnly属性值为false,其他列确实也是赋值为true了,可是运行出来的结果,是整个控件都是可编辑的。而且,如果没有第一句“dataGridView1.ReadOnly = false”的话,后面的循环里的赋值语句都没有起到赋值的作用,原来属性值为啥就是啥。

  请问怎么解决啊~~我就是想让第一列为可编辑,其他列为只读。谢谢~~~


[解决办法]
C# code
this.dataGridView1.Columns[0].ReadOnly = false;this.dataGridView1.Columns[1].ReadOnly = true;this.dataGridView1.Columns[2].ReadOnly = true;this.dataGridView1.Columns[3].ReadOnly = true;
[解决办法]
C# code
this.dataGridView1.ReadOnly = false;for (int i = [color=#FF0000]0[/color]; i [color=#FF0000]<[/color] this.dataGridView1.DisplayedColumnCount(false); i++)            {                if (i == 1)                {                    this.dataGridView1.Columns[i].ReadOnly = false;                }                else                {                    this.dataGridView1.Columns[i].ReadOnly = true;                }            }
[解决办法]
private void dataGridView1(object sender, EventArgs e)
{

...
}
第一列是0
[解决办法]
C# code
for (int i = 0; i < this.dataGridView1.Columns.Count; i++){    if(i==0)        this.dataGridView1.Columns[i].ReadOnly = false;    else        this.dataGridView1.Columns[i].ReadOnly = true;}
[解决办法]
探讨
引用:
C# code

for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
{
if(i==0)
this.dataGridView1.Columns[i].ReadOnly = false;
else
this.dataGridView1.Columns[i].ReadOnl……
……

热点排行