下面的示例:将“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;
}
}
二、行/列的操作
DataGridView 不显示最下面的新行:
通常 DataGridView 的最下面一行是用户新追加的行(行头显示 * )。如果不想让用户新追加行即不想显示该新行,可以将 DataGridView 对象的 AllowUserToAddRows 属性设置为 False。
// 设置用户不能手动给 DataGridView1 添加新行
DataGridView1.AllowUserToAddRows = false;
但是,可以通过程序: DataGridViewRowCollection.Add 为 DataGridView 追加新行。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-50070-7.html
美国人的行动就是来挑战这种所谓的十二海里领海权
打死一些男人更靠谱