本文介绍了粘贴时如何设置datagridview的最大字节长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview,可以在文本框列中输入 DataNames 。我使用 DataGridViewTextBoxColumn 的> MaxInputLength 属性。


在这里,我想逐步解释我的问题。 br>
1 。我在记事本上写了Double Byte Characters(例如1234567890),然后将其复制。然后转到 DataGridViewTextBox ,右键单击并选择 Paste DataGridViewTextBox 显示了123456。

2 。我写了双字节字符(例如123456 )并复制它。然后我转到此 DataGridViewTextBox ,右键单击,然后选择 Paste 。 code> DataGridViewTextBox 显示了123456.

因此, MaxInputLength 属性仅限制为输入字符长度( 不关心单字节或双字节 )。


我只想显示123(6字节)。
有属性或一种限制字节字符长度的方法,尤其是在粘贴操作中吗?

I have a datagridview where DataNames can be entered in a textbox column.I restrict the input length of this column to 6 characters by using the MaxInputLength property of the DataGridViewTextBoxColumn.

Here, I want to explain my problem step by step.
1. I wrote Double Byte Characters(eg.1234567890) on a notepad and copy it.Then I went to this DataGridViewTextBox ,Right Click and then choosed Paste.The DataGridViewTextBox showed 123456.
2.I wrote Double Byte Characters(eg.123456) on a notepad and copy it.Then I went to this DataGridViewTextBox ,Right Click and then choosed Paste.The DataGridViewTextBox showed 123456.
So,MaxInputLength property only restrict to the input character length( not caring single byte or double byte).

I want to show only 123(6 bytes).
Is there a property or a way to restrict the byte character length especially in Paste operation?

预先感谢。

推荐答案

我猜你可以在TextChangedEvent中处理它

I guess you could just handle it in the TextChangedEvent

类似的东西:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    var textBytes = Encoding.UTF8.GetBytes(textBox1.Text);
    var textByteCount = Encoding.UTF8.GetByteCount(textBox1.Text);
    var textCharCount = Encoding.UTF8.GetCharCount(textBytes);

    if (textCharCount != textByteCount && textByteCount >= 12)
    {
        textBox1.Text = Encoding.UTF32.GetString(Encoding.UTF32.GetBytes(textBox1.Text), 0, 12);
    }
    else if (textBox1.Text.Length >= 6)
    {
        textBox1.Text = textBox1.Text.Substring(0, 6);
    }
}

这篇关于粘贴时如何设置datagridview的最大字节长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-31 05:34