WinForm开发 DataGridView控件的各种操作总结(一、单元格内容的操作)

来源:互联网 发布:九月份非农数据 编辑:程序博客网 时间:2024/06/05 00:47

一、单元格内容的操作

 

 // 取得当前单元格内容   Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index    Console.WriteLine(DataGridView1.CurrentCell.ColumnIndex); // 取得当前单元格的行 Index    Console.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来设定选定行。

        /// <summary>         /// 向下遍历         /// </summary>         /// <param ></param>         /// <param ></param>         private void button4_Click(object sender, EventArgs e)         ...{             int row = this.dataGridView1.CurrentRow.Index + 1;             if (row > this.dataGridView1.RowCount - 1)                 row = 0;             this.dataGridView1.CurrentCell = this.dataGridView1[0, row];          }         /// <summary>         /// 向上遍历         /// </summary>         /// <param ></param>         /// <param ></param>         private void button5_Click(object sender, EventArgs e)         ...{             int row = this.dataGridView1.CurrentRow.Index - 1;             if (row < 0)                 row = this.dataGridView1.RowCount - 1;             this.dataGridView1.CurrentCell = this.dataGridView1[0, row];          } 

 

        *注意: this.dataGridView的索引器的参数是:columnIndex, rowIndex或是 columnName, rowIndex 
这与习惯不同。

 

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;
 

*******DataGridView 行头列头的单元格

// 改变DataGridView1的第一列列头内容 DataGridView1.Columns[0].HeaderCell.Value = "第一列"; // 改变DataGridView1的第一行行头内容 DataGridView1.Rows[0].HeaderCell.Value = "第一行"; // 改变DataGridView1的左上头部单元内容 DataGridView1.TopLeftHeaderCell.Value = "左上"; //另外你也可以通过 HeaderText 来改变他们的内容。// 改变DataGridView1的第一列列头内容 DataGridView1.Columns[0].HeaderText = "第一列";

*********** DataGridView单元格的ToolTip的设置


DataGridView.ShowCellToolTips = True
的情况下,单元格的 ToolTip可以表示出来。对于单元格窄小,无法完全显示的单元格, ToolTip可以显示必要的信息。 
1
设定单元格的ToolTip内容 

 

// 设定单元格的ToolTip内容 DataGridView1[0, 0].ToolTipText = "该单元格的内容不能修改"; // 设定列头的单元格的ToolTip内容 DataGridView1.Columns[0].ToolTipText = "该列只能输入数字"; // 设定行头的单元格的ToolTip内容 DataGridView1.Rows[0].HeaderCell.ToolTipText = "该行单元格内容不能修改"; 


 

2 CellToolTipTextNeeded事件 
在批量的单元格的 ToolTip设定的时候,一个一个指定那么设定的效率比较低,这时候可以利用 CellToolTipTextNeeded事件。当单元格的ToolTipText变化的时候也会引发该事件。但是,当DataGridViewDataSource被指定且VirualMode=True的时候,该事件不会被引发。

// CellToolTipTextNeeded事件处理方法 private void DataGridView1_CellToolTipTextNeeded(object sender,     DataGridViewCellToolTipTextNeededEventArgs e) {     e.ToolTipText = e.ColumnIndex.ToString() + ", " + e.RowIndex.ToString(); }


 

*******DataGridView的单元格的边框、网格线样式的设定


1) DataGridView
的边框线样式的设定 
DataGridView
的边框线的样式是通过 DataGridView.BorderStyle属性来设定的。BorderStyle属性设定值是一个
BorderStyle
枚举: FixedSingle(单线,默认)、Fixed3DNone 
2)
单元格的边框线样式的设定 
单元格的边框线的样式是通过 DataGridView.CellBorderStyle属性来设定的。CellBorderStyle属性设定值是 
DataGridViewCellBorderStyle
枚举。(详细参见MSDN 
另外,通过 DataGridView.ColumnHeadersBorderStyleRowHeadersBorderStyle属性可以修改DataGridView的头部的单元格边框线样式。属性设定值是 DataGridViewHeaderBorderStyle枚举。(详细参见 MSDN 
3
单元格的边框颜色的设定 
单元格的边框线的颜色可以通过 DataGridView.GridColor属性来设定的。默认是ControlDarkDark。但是只有在 CellBorderStyle被设定为SingleSingleHorizontalSingleVertical 的条件下才能改变其边框线的颜色。同样,ColumnHeadersBorderStyle以及RowHeadersBorderStyle只有在被设定为Single时,才能改变颜色。 
4
单元格的上下左右的边框线式样的单独设定 
CellBorderStyle
只能设定单元格全部边框线的式样。要单独改变单元格某一边边框式样的话,需要用到DataGridView.AdvancedCellBorderStyle属性。如示例: 
'
单元格的上边和左边线设为二重线 
'
单元格的下边和右边线设为单重线 

 

DataGridView1.AdvancedCellBorderStyle.Top = _     DataGridViewAdvancedCellBorderStyle.InsetDouble DataGridView1.AdvancedCellBorderStyle.Right = _     DataGridViewAdvancedCellBorderStyle.Inset DataGridView1.AdvancedCellBorderStyle.Bottom = _     DataGridViewAdvancedCellBorderStyle.Inset DataGridView1.AdvancedCellBorderStyle.Left = _     DataGridViewAdvancedCellBorderStyle.InsetDouble 


 

同样,设定行头单元格的属性是:AdvancedRowHeadersBorderStyle设定列头单元格属性是:AdvancedColumnHeadersBorderStyle

 

*******DataGridView单元格表示值的自定义 

通过CellFormatting事件,可以自定义单元格的表示值。(比如:值为Error的时候,单元格被设定为红色) 
下面的示例:将“Colmn1”列的值改为大写。

//CellFormatting 事件处理方法 private void DataGridView1_CellFormatting(object sender,     DataGridViewCellFormattingEventArgs e) {     DataGridView dgv = (DataGridView)sender;     // 如果单元格是“Column1”列的单元格     if (dgv.Columns[e.ColumnIndex].Name == "Column1" && e.Value is string)     {         // 将单元格值改为大写         string str = e.Value.ToString();         e.Value = str.ToUpper();         // 应用该Format,Format完毕。         e.FormattingApplied = true;     } } 


 

CellFormatting事件的DataGridViewCellFormattingEventArgs对象的Value属性一开始保存着未被格式化的值。当Value属性被设定表示用的文本之后,把FormattingApplied属性做为True,告知DataGridView文本已经格式化完毕。如果不这样做的话,DataGridView会根据已经设定的FormatNullValueDataSourceNullValueFormatProvider属性会将Value属性会被重新格式化一遍。

 

*******DataGridView用户输入时,单元格输入值的设定


通过DataGridView.CellParsing事件可以设定用户输入的值。下面的示例:当输入英文文本内容的时候,立即被改变为大写。

//CellParsing 事件处理方法 private void DataGridView1_CellParsing(object sender,     DataGridViewCellParsingEventArgs e) {     DataGridView dgv = (DataGridView)sender;     //单元格列为“Column1”时     if (dgv.Columns[e.ColumnIndex].Name == "Column1" &&         e.DesiredType == typeof(string))     {         //将单元格值设为大写         e.Value = e.Value.ToString().ToUpper();         //解析完毕         e.ParsingApplied = true;     } }


 

原创粉丝点击