win-form中的DataGrid [ 100 ] win-form中的DataGrid,能否通过程序的方式锁定任意指定的行?比如第1,4行锁定,不能输入。但2,3行可以输入。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以;以下这断代码就是演示,表假如是三个字段,字段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); 哦,不好意思,看错了,你是说锁定行啊;也可以,不过你得扩展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(); //这里是聚焦到一个可以接收焦点的组件上,达到最终目的 } } } } 代码是刚写的,vs.net 中运行没什么问题,上面的意思说错了,不是必须扩展dataGrid,但为了实现更好的效果,可以考虑扩展datagrid,其实也很方便。 你可以用currencymanager的currentchange事件中写代码,在指定的行就把datagird只读设为true,其他就设为false。或者在datagrid的currentcellchange事件中写。我就是这样实现行只读的。 发现自己越来越白了,有现成的属性不用,而且还有个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(); //跳转焦点也形 } } 就写在 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; } } } 就在CurrentCellChanged事件里可以实现任务行,列的锁定,因为要编辑就肯定要先选中才能改 程序集引用问题 紧急求救!通过filestream把在server生成的pdf调到客户端,是什么原理,想知道这个代码是c#还是javascript 如何安装STUDIO 2005 怎么让RedioButton按自己要求分组? 在AD中,组织单位中的用户移动,如何实现? 在PPC中如何是用WebService呢?请指教 自定义函数/过程和引用问题 有谁能介绍一本关于线程方面的牛书 关于tcp长连接断开的问题 我很苦恼,请大家帮帮我 WEB控件如果没有绑定数据就不显示的问题???????顶者有分 在DataGrid如何添加一列checkbox
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);
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(); //这里是聚焦到一个可以接收焦点的组件上,达到最终目的
}
}
}
}
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(); //跳转焦点也形
}
}
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;
}
}
}