Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > format DataGridViewTextBoxColumn as number as the user is typing the value
 

format DataGridViewTextBoxColumn as number as the user is typing the value

HI all,

I am using a datagridview control on a form and one of the columns is defined as datagridviewtextboxcolumn type. This column is used to display and amount and the cell is editable allowing the user to modify the amount. I already formatted this column as "N0" so when the user moves the focus to amother element this cell displays the amount as a number (eg: 1,000,000 for 1 million). Anyway, I want to be able to format this value as number as the user types the amount... so instead of getting 10, and then 100 and then 1000... 100000 as he types, I would like to show 10.. 100 .... 1,000 .... 100,000 so the user won't have to count how many zeros he typed. I already implemented such a feature in a standalone textbox control using the TextChanged event, but I am unable to implement this in the gridview.
Is it possible to achieve this in a gridview cell? What event(s) should I implement to get this functionality working? I am opened to any suggestions or comments!

Thank you in advance!

Andrei
AndreiC  Tuesday, July 28, 2009 10:02 PM
Hi AndreiC,

That's great you have already implement that effect in normal TextBox with TextChanged event. Now you can do it with DataGridView cell very easy.

You can handle the EditingControlShowing event of DataGridView to get the TextBox from the editing cell.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dataGridView1.Columns.Add("Amount", "Amount");
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtBox = e.Control as TextBox;
if (txtBox != null)
{
txtBox.TextChanged -= new EventHandler(txtBox_TextChanged);
txtBox.TextChanged += new EventHandler(txtBox_TextChanged);

}
}

void txtBox_TextChanged(object sender, EventArgs e)
{

}
}
After you get the e.Control, convert it to normal TextBox. Then handle the TextChanged event of that normal TextBox. You can easily move on without difficulty.

I am also desire to know how you can implement that in a normal TextBox. If you don't mind, could you please post the code. I am willing to learn from you too.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
  • Marked As Answer byAndreiC Friday, July 31, 2009 10:45 AM
  • Proposed As Answer byNareshG Thursday, July 30, 2009 8:43 AM
  •  
Kira Qian  Thursday, July 30, 2009 8:10 AM
Hi AndreiC,

That's great you have already implement that effect in normal TextBox with TextChanged event. Now you can do it with DataGridView cell very easy.

You can handle the EditingControlShowing event of DataGridView to get the TextBox from the editing cell.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dataGridView1.Columns.Add("Amount", "Amount");
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtBox = e.Control as TextBox;
if (txtBox != null)
{
txtBox.TextChanged -= new EventHandler(txtBox_TextChanged);
txtBox.TextChanged += new EventHandler(txtBox_TextChanged);

}
}

void txtBox_TextChanged(object sender, EventArgs e)
{

}
}
After you get the e.Control, convert it to normal TextBox. Then handle the TextChanged event of that normal TextBox. You can easily move on without difficulty.

I am also desire to know how you can implement that in a normal TextBox. If you don't mind, could you please post the code. I am willing to learn from you too.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
  • Marked As Answer byAndreiC Friday, July 31, 2009 10:45 AM
  • Proposed As Answer byNareshG Thursday, July 30, 2009 8:43 AM
  •  
Kira Qian  Thursday, July 30, 2009 8:10 AM
Hi Kira,

First of all, thanks a lot for the answer! It worked like charm :)

Regarding the code I'm using to format the value as numeric, here is the piece of code I'm using for the TextChanged event of the TextBox control:

private void textBox_TextChanged(object sender, EventArgs e)
        {
            TextBox textBox = (TextBox)sender;
            try {
                int pos = textBox.SelectionStart;
                int lengthBefore = textBox.Text.Length;
                int n = int.Parse(_remove_comma(textBox.Text));
                if (n == 0)
                    return;
                textBox.Text = n.ToString("N0");
                int lengthAfter = textBox.Text.Length;
                textBox.SelectionStart = lengthBefore == lengthAfter ? pos : (pos + 1);               
            }
            catch (Exception ex) {
                //textBox.Text = "";
            }
        }
The _remove_comma method does exactly what it says :)

private static string _remove_comma(string s)
        {
            return Regex.Replace(s, ",", "", RegexOptions.Compiled);
        }

The code above works, but it's not perfect... I know it has some problems related to the position of the caret (depending if a new , was introduced while formatting the number) so I will try to fix it and once it's working properly I will re-post it here.

Once again, thank you for the help!

Best regards,
Andrei
AndreiC  Friday, July 31, 2009 10:45 AM

You can use google to search for other answers

Custom Search

More Threads

• Using datasource with underlying objects that I dont want to change in order to affect UI.
• multi select in grid
• Textbox validation
• Private Message Queue is locked, unknown reason
• Another Serious Problem With ComboBox
• Combo Box Questions
• Extracting Columns Values from Dataset (Visual Basic)
• Adding a row to a datatable with a identity key.
• How to persist changes to DataGridView?
• delete from dataset/ database