C# datagridview 实现单元格内容进行回车换行而不是换另一行数据
来源:互联网 发布:mac怎么找安装包 编辑:程序博客网 时间:2024/06/05 14:10
自带的换行是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;
}
}
}
- C# datagridview 实现单元格内容进行回车换行而不是换另一行数据
- vs2005 datagridview的单元格内容自动换行
- DataGridView按回车换列-同行下一单元格
- c# 实现datagridview 用回车向右移动,到头换行,支持编辑模式
- C# Winform 中验证DataGridView单元格内容
- C# DataGridView 获取当前单元格输入内容
- DataGridView回车换行
- [转]C#控件——DataGridView单元格文本自动换行
- c# winform datagridview 编辑单元格内容,没有保存编辑内容
- DataGridView按回车换列
- 【MARK】C# DataGridView控件单元格 未提交 内容改变事件
- C#设置DataGridView某一具体单元格的内容居中显示
- art pandoc使用回车换行而不是空格空格回车换行
- DataGridView 内容换行显示
- poi 单元格内容换行
- C#中datagridview使用tooltip控件显示单元格内容与datagridview自带的tooltip显示单元格内容的方法
- C# 中回车换行
- C# 回车与换行
- Angularjs中scope与$scope
- 归并与快排算法
- JAVA的一点内容小总结(不全)
- Thread的run()与start()的区别
- Pku1236 Network of Schools
- C# datagridview 实现单元格内容进行回车换行而不是换另一行数据
- ssh项目搭建时,Spring的bean的基本配置
- React Native 第六天
- BOM
- mysql数据库相关
- 算法导论【二】——基本说明
- 序列化
- Callable,Runnable比较及用法
- 解析天气信息