
C#如何隐藏当前显示的dataGridview表格注:不是释放资源
2016-06-29
展开全部
DataGridView动态添加新行:DataGridView控件在实际应用中非常实用,特别需要表格显示数据时。可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行。假如需要动态为DataGridView控件添加新行,方法有很多种,下面简单介绍如何为DataGridView控件动态添加新行的两种方法:方法一:intindex=this.dataGridView1.Rows.Add();this.dataGridView1.Rows[index].Cells[0].Value="1";this.dataGridView1.Rows[index].Cells[1].Value="2";this.dataGridView1.Rows[index].Cells[2].Value="监听";利用dataGridView1.Rows.Add()事件为DataGridView控件增加新的行,该函数返回添加新行的索引号,即新行的行号,然后可以通过该索引号操作该行的各个单元格,如dataGridView1.Rows[index].Cells[0].Value="1"。这是很常用也是很简单的方法。方法二:DataGridViewRowrow=newDataGridViewRow();DataGridViewTextBoxCelltextboxcell=newDataGridViewTextBoxCell();textboxcell.Value="aaa";row.Cells.Add(textboxcell);DataGridViewComboBoxCellcomboxcell=newDataGridViewComboBoxCell();row.Cells.Add(comboxcell);dataGridView1.Rows.Add(row);2.DataGridView取得或者修改当前单元格的内容:当前单元格指的是DataGridView焦点所在的单元格,它可以通过DataGridView对象的CurrentCell属性取得。如果当前单元格不存在的时候,返回Nothing(C#是null)//取得当前单元格内容Console.WriteLine(DataGridView1.CurrentCell.Value);//取得当前单元格的列IndexConsole.WriteLine(DataGridView1.CurrentCell.ColumnIndex);//取得当前单元格的行IndexConsole.WriteLine(DataGridView1.CurrentCell.RowIndex);另外,使用DataGridView.CurrentCellAddress属性(而不是直接访问单元格)来确定单元格所在的行:DataGridView.CurrentCellAddress.Y列:DataGridView.CurrentCellAddress.X。这对于避免取消共享行的共享非常有用。当前的单元格可以通过设定DataGridView对象的CurrentCell来改变。可以通过CurrentCell来设定DataGridView的激活单元格。将CurrentCell设为Nothing(null)可以取消激活的单元格。//设定(0,0)为当前单元格DataGridView1.CurrentCell=DataGridView1[0,0];在整行选中模式开启时,你也可以通过CurrentCell来设定选定行。///向下遍历privatevoidbutton4_Click(objectsender,EventArgse){introw=this.dataGridView1.CurrentRow.Index+1;if(row>this.dataGridView1.RowCount-1)row=0;this.dataGridView1.CurrentCell=this.dataGridView1[0,row];}///向上遍历privatevoidbutton5_Click(objectsender,EventArgse){introw=this.dataGridView1.CurrentRow.Index-1;if(rowRow>Column>DataGridView⇒CellContextMenuStripNeeded、RowContextMenuStripNeeded 事件利用 CellContextMenuStripNeeded 事件可以设定单元格的右键菜单,尤其但需要右键菜单根据单元格值的变化而变化的时候。比起使用循环遍历,使用该事件来设定右键菜单的效率更高。但是,在DataGridView使用了DataSource绑定而且是VirtualMode的时候,该事件将不被引发。// CellContextMenuStripNeeded事件处理方法privatevoidDataGridView1_CellContextMenuStripNeeded(objectsender,DataGridViewCellContextMenuStripNeededEventArgse){DataGridViewdgv=(DataGridView)sender;if(e.RowIndex<0){// 列头的ContextMenuStrip设定e.ContextMenuStrip=this.ContextMenuStrip1;}elseif(e.ColumnIndex<0){// 行头的ContextMenuStrip设定e.ContextMenuStrip=this.ContextMenuStrip2;}elseif(dgv[e.ColumnIndex,e.RowIndex].Valueisint){// 如果单元格值是整数时e.ContextMenuStrip=this.ContextMenuStrip3;}}同样,可以通过 RowContextMenuStripNeeded 事件来设定行的右键菜单。// RowContextMenuStripNeeded事件处理方法privatevoidDataGridView1_RowContextMenuStripNeeded(objectsender,DataGridViewRowContextMenuStripNeededEventArgse){DataGridViewdgv=(DataGridView)sender;// 当"Column1"列是Bool型且为True时、设定其的ContextMenuStripobjectboolVal=dgv["Column1",e.RowIndex].Value;Console.WriteLine(boolVal);if(boolValisbool&&(bool)boolVal){e.ContextMenuStrip=this.ContextMenuStrip1;}}CellContextMenuStripNeeded 事件处理方法的参数中、「e.ColumnIndex=-1」表示行头、「e.RowIndex=-1」表示列头。RowContextMenuStripNeeded则不存在「e.RowIndex=-1」的情况。7.DataGridView单元格表示值的自定义:通过CellFormatting事件,可以自定义单元格的表示值。(比如:值为Error的时候,单元格被设定为红色)下面的示例:将“Colmn1”列的值改为大写。//CellFormatting事件处理方法privatevoidDataGridView1_CellFormatting(objectsender,DataGridViewCellFormattingEventArgse){DataGridViewdgv=(DataGridView)sender;//如果单元格是“Column1”列的单元格if(dgv.Columns[e.ColumnIndex].Name=="Column1"&&e.Valueisstring){//将单元格值改为大写stringstr=e.Value.ToString();e.Value=str.ToUpper();//应用该Format,Format完毕。e.FormattingApplied=true;}}CellFormatting事件的DataGridViewCellFormattingEventArgs对象的Value属性一开始保存着未被格式化的值。当Value属性被设定表示用的文本之后,把FormattingApplied属性做为True,告知DataGridView文本已经格式化完毕。如果不这样做的话,DataGridView会根据已经设定的Format,NullValue,DataSourceNullValue,FormatProvider属性会将Value属性会被重新格式化一遍。8.DataGridView用户输入时,单元格输入值的设定:通过DataGridView.CellParsing事件可以设定用户输入的值。下面的示例:当输入英文文本内容的时候,立即被改变为大写。//CellParsing事件处理方法privatevoidDataGridView1_CellParsing(objectsender,DataGridViewCellParsingEventArgse){DataGridViewdgv=(DataGridView)sender;//单元格列为“Column1”时if(dgv.Columns[e.ColumnIndex].Name=="Column1"&&e.DesiredType==typeof(string)){//将单元格值设为大写e.Value=e.Value.ToString().ToUpper();//解析完毕e.ParsingApplied=true;}}9.DataGridView新加行的默认值的设定:需要指定新加行的默认值的时候,可以在DataGridView.DefaultValuesNeeded事件里处理。在该事件中处理除了可以设定默认值以外,还可以指定某些特定的单元格的ReadOnly属性等。//DefaultValuesNeeded事件处理方法privatevoidDataGridView1_DefaultValuesNeeded(objectsender,DataGridViewRowEventArgse){//设定单元格的默认值e.Row.Cells["Column1"].Value=0;e.Row.Cells["Column2"].Value="-";}10.DataGridView设定单元格只读:1)使用ReadOnly属性如果希望,DataGridView内所有单元格都不可编辑,那么只要://设置DataGridView1为只读DataGridView1.ReadOnly=true;此时,用户的新增行操作和删除行操作也被屏蔽了。如果希望,DataGridView内某个单元格不可编辑,那么只要://设置DataGridView1的第2列整列单元格为只读DataGridView1.Columns[1].ReadOnly=true;//设置DataGridView1的第3行整行单元格为只读DataGridView1.Rows[2].ReadOnly=true;//设置DataGridView1的[0,0]单元格为只读DataGridView1[0,0].ReadOnly=true;2)使用EditMode属性DataGridView.EditMode属性被设置为DataGridViewEditMode.EditProgrammatically时,用户就不能手动编辑单元格的内容了。但是可以通过程序,调用DataGridView.BeginEdit方法,使单元格进入编辑模式进行编辑。DataGridView1.EditMode=DataGridViewEditMode.EditProgrammatically;3)根据条件设定单元格的不可编辑状态当一个一个的通过单元格坐标设定单元格ReadOnly属性的方法太麻烦的时候,你可以通过CellBeginEdit事件来取消单元格的编辑。//CellBeginEdit事件处理方法privatevoidDataGridView1_CellBeginEdit(objectsender,DataGridViewCellCancelEventArgse){DataGridViewdgv=(DataGridView)sender;//是否可以进行编辑的条件检查if(dgv.Columns[e.ColumnIndex].Name=="Column1"&&!(bool)dgv["Column2",e.RowIndex].Value){//取消编辑e.Cancel=true;}}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询