Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

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


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

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


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

C#
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:
C#
public NumericUpDownEditingControl()
{
    initializing = true;

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

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!
Posted
Updated 7-Jun-16 10:23am

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:

C#
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;
}


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
 
Share this answer
 
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900