C# datagridview 实现单元格内容进行回车换行而不是换另一行数据

自带的换行是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;


        }
    }
}

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现DataGridView中合并单元格一行的功能,可以使用DataGridView的CellPainting事件来自定义单元格的绘制。 以下是一个示例,演示如何将DataGridView中相邻的相同单元格合并为一行: ```csharp private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { // 只对非表头单元格进行处理 if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.RowIndex < dataGridView1.RowCount - 1) { DataGridViewCell curCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; DataGridViewCell nextCell = dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex]; // 判断当前单元格和下一个单元格的值是否相同 if (curCell.Value != null && nextCell.Value != null && curCell.Value.Equals(nextCell.Value)) { // 下一个单元格被合并到当前单元格 e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None; nextCell.Visible = false; } else { // 重新绘制当前单元格的下边框 e.AdvancedBorderStyle.Bottom = dataGridView1.AdvancedCellBorderStyle.Bottom; } } } ``` 在这个示例中,我们通过比较相邻单元格的值来判断是否需要进行合并。如果需要合并,我们将下一个单元格的可见性设置为false,并将当前单元格的下边框样式设置为None,以达到合并单元格一行的效果。如果当前单元格和下一个单元格的值不同,我们就重新绘制当前单元格的下边框。 需要注意的是,这种方法只能合并相邻的相同单元格一行,如果需要合并其他单元格,需要根据实际情况进行适当的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值