win-form中的DataGrid,能否通过程序的方式锁定任意指定的行?比如第1,4行锁定,不能输入。但2,3行可以输入。

解决方案 »

  1.   

    可以;以下这断代码就是演示,表假如是三个字段,字段1和3分别锁定,只可以操作字段2//创建风格
    DataGridTableStyle dbTs = new DataGridTableStyle();
    //你的字段一的风格
    System.Windows.Forms.DataGridTextBoxColumn column = new DataGridTextBoxColumn();
    column.NullText = "";
    column.HeaderText= "你的字段标题名1";
    column.MappingName= "你的字段名1";
    column.Width=100;
    column.ReadOnly=true; //锁定,直读不可修改
    dbTs.GridColumnStyles.Add(column);
    //你的字段二的风格
    System.Windows.Forms.DataGridTextBoxColumn column = new DataGridTextBoxColumn();
    column.NullText = "";
    column.HeaderText= "你的字段标题名2";
    column.MappingName= "你的字段名2";
    column.Width=100;
    dbTs.GridColumnStyles.Add(column);
    System.Windows.Forms.DataGridTextBoxColumn column = new DataGridTextBoxColumn();
    column.NullText = "";
    column.HeaderText= "你的字段标题名3";
    column.MappingName= "你的字段名3";
    column.Width=100;
    column.ReadOnly=true; //锁定,直读不可修改
    dbTs.GridColumnStyles.Add(column);
    //加入风格
    DataGrid.TableStyles.Clear();
    DataGrid.TableStyles.Add(dbTs);
      

  2.   

    哦,不好意思,看错了,你是说锁定行啊;也可以,不过你得扩展dataGrid给你段代码,你可以判断是几行几列,然后聚焦到另外的组件上,这样即可实现;    //锁定的行,这个存放了要锁定的行数据。
        private int[] LockRow=new int[]{3,6,9,20};    private void dataGrid_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
          System.Windows.Forms.DataGrid.HitTestInfo myHitTest;
          
          myHitTest = dataGrid.HitTest(Cursor.Position.X,Cursor.Position.Y);
          if(myHitTest.Type==System.Windows.Forms.DataGrid.HitTestType.Cell){
            int absRow = 0; //基数
            int currRow =(myHitTest.Row-1);//从0开始
            if(this.dataGrid.ColumnHeadersVisible){
              absRow ++; //因为有个标题
            }
            this.Text = "Column:" + myHitTest.Column + " Row:" + currRow + " HitTest:";//这里是看看当前所在行和列
            for(int i=0;i< LockRow.Length;i++){
              if(currRow==(LockRow[i]+absRow)){ //检查是否已经到了锁定行,到的话就反聚焦
                this.button1.Focus(); //这里是聚焦到一个可以接收焦点的组件上,达到最终目的
              }
            }
          }
        }
      

  3.   

    代码是刚写的,vs.net 中运行没什么问题,上面的意思说错了,不是必须扩展dataGrid,但为了实现更好的效果,可以考虑扩展datagrid,其实也很方便。
      

  4.   

    你可以用currencymanager的currentchange事件中写代码,在指定的行就把datagird只读设为true,其他就设为false。或者在datagrid的currentcellchange事件中写。我就是这样实现行只读的。
      

  5.   

    发现自己越来越白了,有现成的属性不用,而且还有个bug,不过这个没bug了,代码少了几倍,呵呵    //锁定的行,这个存放了要锁定的行数据。
        private int[] LockRow=new int[]{3,6,9,20};
        private void dataGrid_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
          for(int i=0;i<LockRow.Length;i++){
            this.dataGrid.ReadOnly = (this.dataGrid.CurrentRowIndex==LockRow[i]); //修改直读属性
            //this.button1.Focus(); //跳转焦点也形
          }
        }
      

  6.   

    就写在 CurrentCellChanged事件中就ok了;    //锁定的行,这个存放了要锁定的行数据。
        private int[] LockRow=new int[]{3,6,9,20};
        private void dataGrid_CurrentCellChanged(object sender, System.EventArgs e) {
          //锁定表格行的算法
          this.dataGrid.ReadOnly = false;
          for(int i=0;i<LockRow.Length;i++){
            if(this.dataGrid.CurrentRowIndex==LockRow[i]){
              this.dataGrid.ReadOnly = true;
              break;
            }
          }
        }
      

  7.   

    就在CurrentCellChanged事件里可以实现任务行,列的锁定,因为要编辑就肯定要先选中才能改