DataGridView C#中的最大长度文本 [英] Max Length Text in a DataGridView C#

查看:145
本文介绍了DataGridView C#中的最大长度文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿那里,



我需要你的帮助。我在一个特定的列= 3中使用MaxInputLength设计我的DataGridView'Grid'。当我执行我的项目时,将信息插入我的网格时没有问题,当我想编辑一些保存的信息时,我从数据库MySQL中恢复它:



Hey there,

I need your help. I design my DataGridView 'Grid' with MaxInputLength in a specific column = 3. When I execute my project there's no problem when I insert info into my Grid, when I want to edit some saved info, I recover it from database MySQL doing this:

// RecoverDBase() is a BindingSource type method
Grid.DataSource = RecoverDBase();







我在这里没问题。但是当我在这个专栏中插入文字时,我可以无限地进行。如果我可以这样做,我不会遇到这个问题:






I have no problem here. But when I insert text in this column, I can do it infinitely. I wouldn't have this problem if I can do something like this:

Grid.DataSource = RecoverDBase();
Grid.Columns[0].MaxInputLength=3;





但我不能。它不存在这样的东西。如何像我一样恢复数据库后如何控制输入文本长度?¿



But I can't. It doesn't exist something like that. How can I control the input text length after recovering a database like I do?¿

推荐答案

您可能需要编写代码来修剪字符串并添加一个elipse表示更多的文本,但我认为控件已经为你完成了,并且是基于相关列的宽度。
You're probably going to have to write code to trim the string and add an elipse to indicate more text, but I thought that was already done for you by the control, and was based on how wide the associated column is.


绑定数据网格视图时,该列成为DataGridViewTextBoxColumn对象。这是你应该引用的,因为它具有MaxInputLength属性。



因此,如果你的列名是描述,那么对象的名字应该自动就像descriptionDataGridViewTextBoxColumn(好吧,这在我的.NET中发生)。
When you bind a data grid view, the column becomes a DataGridViewTextBoxColumn object. It's this you should reference, as it is this that has the MaxInputLength property.

So if your column name is "Description", for instance, the object's name should automatically be something like "descriptionDataGridViewTextBoxColumn" (well, this happens for me in .NET).


你可以试试这个:



You can try this :

DataGridViewTextBoxColumn cName = (DataGridViewTextBoxColumn)dataGridView1.Columns[1];//here index of column
cName.MaxInputLength = 10;//here Max Length to column


这篇关于DataGridView C#中的最大长度文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆