C# datagridview 实现单元格内容进行回车换行而不是换另一行数据
2017-08-12 14:58
2061 查看
自带的换行是shift+enter但客户偏偏想要enter换行,无耐百度了好久才搜到这一篇文章。稍加改动就可实现自己的功能
运行效果截图(原始)
运行效果截图(按下回车键)
/*
* 文件:From1.cs
* 说明:支持回车换行的DataGridView
* 作者:Boitboy(游荡男孩)
* 博客:http://boitboy.cnblogs.com/
* 支持回车换行的列有特定的要求
* 本例中必须设定
* dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
* this.Column2.DefaultCellStyle = dataGridViewCellStyle2;
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Boitboy.DataGridViewEx
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Rows.Add("测试行1", "不支持回车换行的单元格", "");
dataGridView1.Rows[0].Height = 25;
dataGridView1.Rows.Add("测试行2", "支持回车换行的单元格", "");
dataGridView1.Rows[1].Height = 75;
//属性值
dataGridView1.Rows[1].Cells[1].Style.Tag = true;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData != Keys.Enter)
{
//继续原来base.ProcessCmdKey中的处理
return base.ProcessCmdKey(ref msg, keyData);
}
if (!this.dataGridView1.IsCurrentCellInEditMode) //如果当前单元格处于编辑模式
{
//继续原来base.ProcessCmdKey中的处理
return base.ProcessCmdKey(ref msg, keyData);
}
if (this.dataGridView1.CurrentCell.Style.Tag == null ||
!(this.dataGridView1.CurrentCell.Style.Tag is bool))
{
return base.ProcessCmdKey(ref msg, keyData);
}
TextBox textBox = this.dataGridView1.EditingControl as TextBox;
int nStart = textBox.SelectionStart;//得到当前光标的位置
string text = textBox.Text;
if (nStart < 0 || nStart > text.Length)
return false;
//光标签名的字
string text1 = "";
if (nStart > 0)
{
text1 = text.Substring(0, nStart);
}
//光标后面的字
string text2 = "";
if (nStart < text.Length)
{
text2 = text.Substring(nStart, text.Length - nStart);
}
text = text1 + "\r\n" + text2;
textBox.Text = text;
this.dataGridView1.CurrentCell.Value = text;
textBox.Select(nStart + 2, 0);
return true;
}
}
}
运行效果截图(原始)
运行效果截图(按下回车键)
/*
* 文件:From1.cs
* 说明:支持回车换行的DataGridView
* 作者:Boitboy(游荡男孩)
* 博客:http://boitboy.cnblogs.com/
* 支持回车换行的列有特定的要求
* 本例中必须设定
* dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
* this.Column2.DefaultCellStyle = dataGridViewCellStyle2;
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Boitboy.DataGridViewEx
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Rows.Add("测试行1", "不支持回车换行的单元格", "");
dataGridView1.Rows[0].Height = 25;
dataGridView1.Rows.Add("测试行2", "支持回车换行的单元格", "");
dataGridView1.Rows[1].Height = 75;
//属性值
dataGridView1.Rows[1].Cells[1].Style.Tag = true;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData != Keys.Enter)
{
//继续原来base.ProcessCmdKey中的处理
return base.ProcessCmdKey(ref msg, keyData);
}
if (!this.dataGridView1.IsCurrentCellInEditMode) //如果当前单元格处于编辑模式
{
//继续原来base.ProcessCmdKey中的处理
return base.ProcessCmdKey(ref msg, keyData);
}
if (this.dataGridView1.CurrentCell.Style.Tag == null ||
!(this.dataGridView1.CurrentCell.Style.Tag is bool))
{
return base.ProcessCmdKey(ref msg, keyData);
}
TextBox textBox = this.dataGridView1.EditingControl as TextBox;
int nStart = textBox.SelectionStart;//得到当前光标的位置
string text = textBox.Text;
if (nStart < 0 || nStart > text.Length)
return false;
//光标签名的字
string text1 = "";
if (nStart > 0)
{
text1 = text.Substring(0, nStart);
}
//光标后面的字
string text2 = "";
if (nStart < text.Length)
{
text2 = text.Substring(nStart, text.Length - nStart);
}
text = text1 + "\r\n" + text2;
textBox.Text = text;
this.dataGridView1.CurrentCell.Value = text;
textBox.Select(nStart + 2, 0);
return true;
}
}
}
相关文章推荐
- 如何在VS.NET/C# Builder开发的网页中也能实现DOS下录入数据回车换行的功能
- C# winform datagridview中如何实现鼠标右键点击一行数据出现一个带有删除的菜单,并能执行删除操作?
- c# 实现datagridview 用回车向右移动,到头换行,支持编辑模式
- C# DataGridView 代码实现选中一行数据
- c#DataGridView数据绑定示例——格式化单元格的内容(转)
- c# 实现DataGridView中的单元格只能输入数字和点
- vs2005 datagridview的单元格内容自动换行
- vs2005 datagridview的单元格内容自动换行收藏
- c# datagridview回车键后获得右侧单元格焦点
- c# 实现将dataGridView中的数据导出到Excel
- 实现Ext的grid单元格数据过长换行显示
- C#对DataGridView中的数据进行添加、修改、删除操作
- C# .NET 如何在DataGridView中最简洁的实现对单元格显示的处理
- C#实现将DataGridView中的数据导入到Excel中
- 实现Ext的grid单元格数据过长换行显示
- C#利用DataGridView实现数据的快速输入
- C#实现读写EXCEL数据—附实例导出DATAGRIDVIEW 数据到EXCEL
- 如何在C#中实现DATAGRIDVIEW 数据打印
- C#实现从EXCEL将数据导出到datagridview
- xml学习(三)---利用xml文件实现数据岛功能(网页显示xml文件中的内容免C#代码操作xml文件)