Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Format text in data grid view
 

Format text in data grid view

HI

I have the folloing code in my datagrid view EditingControlShowing event..

private void dataEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridView D = sender as DataGridView;
if (D.CurrentCell.ColumnIndex == 1)
{
TextBox txt = (TextBox)(e.Control);
if (txt != null)
{
txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(Program.txtOnlyAllowNumericAndMinus);
txt.Leave += new System.EventHandler(this.FormatText);
}
}
else
{
TextBox txt = (TextBox)(e.Control);
if (txt != null)
{
txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CellKeyPress);
txt.Leave += new System.EventHandler(this.DoNothing);
}
}
}

when the Leave event is executed it does evereything as I wanted it to(formats the tect of one cell in the datagridview) but for some strange reason the text gets changed back right away... I stepped through the code and there was nothing that would have changed it back...

Can anyone show me how I can format the text of the second column in a two column datagridview?

thanks

szsz
szsz  Monday, October 22, 2007 9:41 AM

Once the datagridview cell is done editting the datagridview draws the cells value in the datagridview. The cells textbox no longer exists. You should use the CellFormatting event to format the data for display. Also in the editting control showing event you should remove the handlers to the textbox's Keypress and leave events before you add the new handlers. This will prevent the event handlers from running multiple times.

Ken Tucker  Monday, October 22, 2007 11:14 AM

Once the datagridview cell is done editting the datagridview draws the cells value in the datagridview. The cells textbox no longer exists. You should use the CellFormatting event to format the data for display. Also in the editting control showing event you should remove the handlers to the textbox's Keypress and leave events before you add the new handlers. This will prevent the event handlers from running multiple times.

Ken Tucker  Monday, October 22, 2007 11:14 AM
Thanks

I got the formating part
Exatcly how do I delete the event handlers after I use them? (C#)

szsz

szsz  Monday, October 22, 2007 11:33 AM
Just delete the old one before you add a new event handler

Code Block

txt.KeyPress -= new System.Windows.Forms.KeyPressEventHandler(Program.txtOnlyAllowNumericAndMinus); txt.Leave += new System.EventHandler(this.FormatText);


txt.Leave -= new System.EventHandler(this.FormatText);
txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(Program.txtOnlyAllowNumericAndMinus);

Ken Tucker  Monday, October 22, 2007 3:34 PM
thanks alot
szsz  Monday, October 22, 2007 4:01 PM

You can use google to search for other answers

Custom Search

More Threads

• SQL DataGridView Binding
• Selecting multiples cells in a grid
• Hittest in treeview control
• How to display two columns in datagridviewcombobox
• Which column changed with DataGridView?
• Data for provinces and states
• DataGridView, Access Database, Related Tables, Automatic Suggestions and AutoComplete
• data source configuration wizard
• Ups ... again Timeout from the SQL-server !!!
• data bound ComboBox selecting first item in list unexpectedly (VS2008/.Net 3.5)