如何在numericupdowncolumn()中使用numericupdown属性? [英] How can I use numericupdown properties in numericupdowncolumn() ?

查看:166
本文介绍了如何在numericupdowncolumn()中使用numericupdown属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

对不起我是C#的新手,我使用过这个课程: DataGridView中自定义NumericUpDown控件的问题 [ ^ 在我的C#VS2013 Win Form应用程序中,我可以使用如下简单代码在我的DataGridView中显示和使用NumericalUpDown:



Hi
Sorry I am new in C#, I used this class: Problem with custom NumericUpDown control in DataGridView[^] in my C# VS2013 Win Form application and I can show and use NumericalUpDown in my DataGridView by using a simple code like this:

private void Test_Load(object sender, EventArgs e)
{
    NumericUpDownColumn num = new NumericUpDownColumn();
    num.Name = "num";
    num.HeaderText = "Number";
    dgvTest.Columns.Add(num);
}





现在,我的问题是:

我如何设置或更改一些NumericUpDown属性!?

如下示例





Now, my question is:
How can I set or change some of NumericUpDown Properties!?
like below Sample:

num.Maximum = 500;
num.TextAlign = HorizontalAlignment.Right;





我知道我可以从上面这一行中设置/更改一些属性:





I know I can Set/Change some properties from this line of above class:

public NumericUpDownEditingControl()
{
    initializing = true;

    this.Minimum = (decimal)0;
    this.DecimalPlaces = 0;
    this.Maximum = 120;

    initializing = false;
}



并将其更改为满足我的需求:


and change it to this to meet my needs:

public NumericUpDownEditingControl()
{
    initializing = true;

    this.Minimum = (decimal)0;
    this.DecimalPlaces = 0;
    this.Maximum = 500;
    this.TextAlign = HorizontalAlignment.Right;
    this.Tag = "notzero";
    initializing = false;
}



但它会被硬编码!我希望能够以编程方式设置新的或更改任何属性,就像我在上面写的样本一样。



非常感谢和抱歉如果我在错误的地方提出这么基本的问题!



最好的问候

MRK



我的尝试:



我提到我尝试使用它:

num.Maximum = 500;

num.TextAlign = Horizo​​ntalAlignment.Right;

但是没有NumericUpDown属性可以通过NumericUpDownColumn类型访问!


But it will be hard coded! and I want to be able to set new or change any properties programmatically, just like sample I wrote in above.

Thanks so much and sorry If I ask in wrong place or so basic question!

Best Regards
MRK

What I have tried:

as I mention I try to use this:
num.Maximum = 500;
num.TextAlign = HorizontalAlignment.Right;
But none of NumericUpDown properties are accessible via NumericUpDownColumn type!

推荐答案

嗨谢尔盖



感谢您的快速回答和完整信息。当我第一次读到你的答案时,我害怕我无法理解我该怎么做!哈哈,因为我说我是C#的新人!



但是多亏了你的链接我想出怎么做!我这样做了,看起来效果很好:



Hi Sergey

Thanks for your Quick answer and full info. When I read your answer for first time I was afraid that I cannot understand what should I do! haha as i said I am so new in C#!

But thanks to your Links I figure out how to do it! I did this and It's seems working great:

private void Test_Load(object sender, EventArgs e)
{

    NumericUpDownColumn num = new NumericUpDownColumn();
    num.Name = "num";
    num.HeaderText = "Number";
    dgvTest.Columns.Add(num);
    dgvTest.EditingControlShowing += dgvTest_SetMyProperties;

}

private void dgvTest_SetMyProperties(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    (e.Control as NumericUpDown).Maximum = 600;
    (e.Control as NumericUpDown).TextAlign = HorizontalAlignment.Right;
    dgvTest.EditingControlShowing -= dgvTest_SetMyProperties;
}





谢尔盖,我是否以正确的方式使用它?



我按照你的第二个建议同时我在最后一行调用它删除 EditingControlShowing 事件。 (为了确保它正常工作,我也在该事件中使用了Line Break)



这个控件有些奇怪,当我插入一个数字然后按下输入键,它将转到下一个单元格,但插入的数字将被删除!!(有时它没有!!)如果我按Tab键,它将转到下一个单元格,但插入的数字将保留! !



你也可以帮帮我!?



感谢你的时间



Sergey, Do I use It in right way?

I followed your second suggestion Also I remove the EditingControlShowing event at the last line of call it. (To make sure it work fine, I use Line Break inside that event too)

There are something strange about this control, when I insert a number and press Enter key, It will go to next cell, but inserted number will be deleted!!(and sometimes it did not!!) If I press Tab, It will go to next cell but inserted number will be remain!!

Can you help me with that too!?

Thanks for your Time


通过自定义单元格/列类型自定义单元格的编辑器控件的引用的一种方法是使用属性 DataGridView.EditingControl 。获得控件后,只需将其类型转换为示例代码中显示的具体运行时控件类,只是因为您知道此类型并且可以在代码中完全访问它。但问题是:你必须在适当的编辑阶段只处理一个单元格,所以你可能还需要使用属性 DataGridView.EditMode 或处理 DataGridView.EditModeChanged DataGridView.CellBeginEdit / CellEndEdit 。请查看 DataGridView 的所有与版本相关的成员:

DataGridView类(System.Windows.Forms)



另一种方法是处理事件 DataGridView.EditingControlShowing ,它还允许您访问控件,您可能希望将其输入到 NumericUpDown 控制。并且只有在控件准备好供您使用时才会调用此事件。请参阅:

DataGridView.EditingControlShowing事件(System.Windows.Forms)



向所有人发出的一个重要警告:对其进行编辑控制由<重复使用code>的DataGridView 。您必须确保仅将事件处理程序添加到控件实例一次。您必须检查事件句柄是否已添加到控件的同一实例的事件实例的调用列表中,然后再不添加处理程序。未能检查将导致内存泄漏。这是托管内存中相对罕见的内存泄漏可能性之一。



-SA
One way to get the reference to the editor control of a cell, customized through a custom cell/column type or not, is using the property DataGridView.EditingControl. When you got the control, you can simply type-cast it to the concrete runtime control class shown in your sample code, just because you know this type and it is fully accessible in your code. But the problem is: you have to deal with it only for a cell in a proper editing phase, so you may also need to use the properties DataGridView.EditMode or handle DataGridView.EditModeChanged or DataGridView.CellBeginEdit/CellEndEdit. Please review all the edition-related members of DataGridView:
DataGridView Class (System.Windows.Forms).

Another approach would be handling the event DataGridView.EditingControlShowing, which also gives you the access to the control, which you may want to type-cast to your NumericUpDown control. And this event is always invoked only when the control is ready for your use. Please see:
DataGridView.EditingControlShowing Event (System.Windows.Forms).

One big warning to everyone: the editing control of the same is reused by DataGridView. You have to make sure that you add event handler to the control instance only once. You have to check up that the events handles are already added to the invocation list of the event instances of the same instance of the control and then not add the handlers again. Failure to check it up will result in a memory leak. This is one of the relatively rare cases of the memory leak possibilities in managed memory.

—SA


这篇关于如何在numericupdowncolumn()中使用numericupdown属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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