Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to remove white space in a textbox 1

Status
Not open for further replies.

b98mr1

Programmer
Feb 23, 2005
20
GB
Hi all,

I am working in VS 2005 and have a datagridview that correctly displays data through an ODBC connection. each column in the datagridview is then displayed in a textbox which the user can edit.

However my problem is that the fields in the datagridview are 40 characters long and say the data is 5 characters long, the remaining 35 characters are padded out with white space.

How can this be removed so that when the user clicks to edit a textbox field, the cursor will appear at 5th character and not the 40th?

I have tried trimming the data with the 'trim' function but it doesn't appear to work.

Here is my code:

try
{
<connection>.Open();
<dataset>.Clear();
<dataadapter>.Fill(<dataset>, "tablename");
<datagrid>.DataSource = <dataset>.Tables["tablename"];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
<connection>.Close();
<connection>.Dispose();
}

this.<textbox1>.DataBindings.Add("text", <Dataset>.Tables["tablename"], "column1".Trim());
this.<textbox2>.DataBindings.Add("text", <Dataset>.Tables["tablename"], "column2".Trim());
this.<textbox3>.DataBindings.Add("text", <Dataset>Tables["tablename"], "column3".Trim());
}

Any help would be greatly appreciated..

Thanks
Ross
 
Sounds like the source column type is char (which is fixed length). Use the DataGridView.CellFormatting event to strip unwanted white space (in the example we are trimming all column 1 values):
Code:
dataGridView1.CellFormatting += delegate(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.Value != null && e.ColumnIndex == 1) e.Value = e.Value.ToString().Trim();
};
 
Thanks for your quick reply.

The source column is a char(fixed in length). I tried this code but it didn't remove the white space in the datagrid or the textbox.

I should have mentioned in my original post that I have tried using 'trim' in the select command but this didn't work either. Also the data is being read from an Informix database using the ODBC connection. Not sure if that helps but I have a very limited knowledge of Informix

I'm stumped....
 
I have no experience of Informix either, but the CellFormatting code worked on a SQL Server char field. It's possible that the 'padding' that Informix uses does not qualify for trimming ?

Can you use CONVERT(VARCHAR(40), <columnName>) in your SELECT statement ? Converting to a VARCHAR may allowing the trimming to work correctly.
 
I'd thought of that too but CONVERT is not recognised by Informix.
 
Good suggestion, but unfortunately in seems that Informix is not able to handle VARCHAR at all, unless the length of the data is the same i.e all data is 5 characters long - which in this case is not true.
 
My apologies SHelton.

Your initial post using the DataGridViewCellFormatting Event was in fact correct and did remove the white space in each cell. Not sure why it didn't work for me first time round.

Now all I need to do is figure out how to repeat this method for a textbox field.

Many thanks again

Ross
 
If you are data binding the text box, you can trim the white space in the Format event of the binding.
 
Thanks SHelton. I have got it to trim the white space in the textboxes now. Never created a delegate before but now I understand how useful they are.

One thing I have noticed however is that when the form loads and the datagrid is filled, I have to click on the datagrid before the white space is trimmed in the textbox. This only occurs with the first record in the datagrid. If I click on the second record, the white space is removed in the textbox.

This is the code I produced and is placed in the load event of the form.

I am thinking it is a coding error on my part though...

Code:
//Format 3 columns in Datagrid

<datagrid>.CellFormatting += delegate(object senderSP, DataGridViewCellFormattingEventArgs ea)
                    {
                        if (ea.Value != null && ea.ColumnIndex == 0)
                        {
                            ea.Value = ea.Value.ToString().Trim();
                        }
                        if (ea.Value != null && ea.ColumnIndex == 1) ea.Value = ea.Value.ToString().Trim();
  
                        if (ea.Value != null && ea.ColumnIndex == 2)
                        {
                            ea.Value = ea.Value.ToString().Trim();
                        }
                    };

//Remove space in textbox 1

            Binding Value_txt = this.<textbox>.DataBindings["text"];
            Value_txt.Format += delegate(object vbinding, ConvertEventArgs args)
            {
                if (args.Value != null)
                {
                    args.Value = args.Value.ToString().Trim();
                }
            };

//Remove space in textbox 2

            Binding Comment_txt = this.<textbox>t.DataBindings["text"];
            Comment_txt.Format += delegate(object cbinding, ConvertEventArgs args)
            {
                if (args.Value != null)
                {
                    args.Value = args.Value.ToString().Trim();
                }
            };

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top