您的位置:首页 > 其它

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

2012-10-15 20:57 501 查看
一、单元格内容的操作
 

// 取得当前单元格内容
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
变化的时候也会引发该事件。但是,当DataGridView的DataSource被指定且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(单线,默认)、Fixed3D、None。 

2) 单元格的边框线样式的设定 
单元格的边框线的样式是通过 DataGridView.CellBorderStyle
属性来设定的。CellBorderStyle
属性设定值是 

DataGridViewCellBorderStyle 枚举。(详细参见MSDN) 
另外,通过 DataGridView.ColumnHeadersBorderStyle
和RowHeadersBorderStyle
属性可以修改DataGridView
的头部的单元格边框线样式。属性设定值是 DataGridViewHeaderBorderStyle
枚举。(详细参见 MSDN) 

3)单元格的边框颜色的设定 
单元格的边框线的颜色可以通过 DataGridView.GridColor
属性来设定的。默认是ControlDarkDark
。但是只有在 CellBorderStyle
被设定为Single、SingleHorizontal、SingleVertical 的条件下才能改变其边框线的颜色。同样,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会根据已经设定的Format,NullValue,DataSourceNullValue,FormatProvider属性会将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;
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐