简体   繁体   中英

Format DataGridView TextBox Column Containing Numeric Values

I have a DataGridView with TextBox colums. One of the columns display record counts which are formatted as ###,###,000. The column itself isn't set to format the text, but the data is formatted when the data source is populated. The data source is a List by the way. The DataGridView merely displays everything as it gets it.

Now, I need to parse the numeric text value into a variable, which is fine by doing

int.Parse(data_grid_view_row.Cells["col_Record_Count"].Value.ToString())

but, if I have numbers reaching thousands, it displays as 1 000, because of the formatting. The formatting generates a space rather than a comma as this is how the environment has been configured. When I try to parse that value, I get an exception because of the space in the numeric value.

I have tried formatting the string value

int.Parse(String.Format("{0:000000000}", data_grid_view_row.Cells["col_Record_Count"].Value.ToString()))

and

int.Parse(String.Format("{0:########0}", data_grid_view_row.Cells["col_Record_Count"].Value.ToString()))

and all sorts of variants, but it keeps returning the space in the value, which then won't parse. I have tried replacing the space, but it is persistent. Also, the space shows ASCII keycode 63 which is supposed to be a question mark, not so? Even when I try to replace using the keycode... nothing!

Any ideas as to why this is happening and how to fix it?

The complete code block I have is

foreach (DataGridViewRow data_grid_view_row in dgv_Migration_Files.Rows)
{
    if ((bool)data_grid_view_row.Cells["col_Select"].Value == true)
    {
        //-------------------------------------------------------------------------

        // this is only to test and see what value I get for the space character, will be removed later

        string test_str = data_grid_view_row.Cells["col_Record_Count"].Value.ToString().Replace(" ", string.Empty);

        test_str = test_str.Replace(" ", "");

        for (int k = 0; k < test_str.Length; k++)
        {
            string newstr = test_str.Substring(k, 1);

            var kycode = ASCIIEncoding.ASCII.GetBytes(newstr);
        }

        //-------------------------------------------------------------------------

        migrate.Add(new Migrate()
        {
            File_Folder = data_grid_view_row.Cells["col_File_Folder"].Value.ToString()
            ,
            File_Name = data_grid_view_row.Cells["col_File_Name"].Value.ToString()
            ,
            Record_Count = int.Parse(String.Format("{0:000000000}", data_grid_view_row.Cells["col_Record_Count"].Value.ToString()))
        });
    }
}

Use simple Solution:- If space is available Remove it and then simply Parse into INT.

str.Replace(" ", String.Empty)


string data = data_grid_view_row.Cells["col_Record_Count"].Value.ToString().Replace(" ", String.Empty);

int.Parse(data);

Make it easier:-

int.Parse(data_grid_view_row.Cells["col_Record_Count"].Value.ToString().replace(" ", String.Empty));

And if you want to remove space and comma both while parsing using this Regular Expression

int.Parse(data_grid_view_row.Cells["col_Record_Count"].Value.ToString().replace(/[, ]+/g, " ").trim());

Check This Small Example First you will get Clear Idea:-

 string data = "1 0000";
 string result = data.Replace(" ", string.Empty);

Since the replace method didn't remove spaces as I expected to, I resorted to the below solution. This will also remove any other numeric separators irrespective of what the numeric separator has been set up as in the OS.

//get the numeric decimal seperator key code

string dummy_numeric = (1000).ToString(Helper.application_number_display_format);

char replace_char = (char)0;

foreach (char current_char in dummy_numeric)
{
    if (!System.Char.IsDigit(current_char))
    {
        replace_char = current_char;
        break;
    }
}

//get all the files that are selected for migration

List<Migrate> migrate = new List<Migrate>();

foreach (DataGridViewRow data_grid_view_row in dgv_Migration_Files.Rows)
{
    if ((bool)data_grid_view_row.Cells["col_Select"].Value == true)
    {
        migrate.Add(new Migrate()
        {
            File_Folder = data_grid_view_row.Cells["col_File_Folder"].Value.ToString()
            ,
            File_Name = data_grid_view_row.Cells["col_File_Name"].Value.ToString()
            ,
            Record_Count = int.Parse(data_grid_view_row.Cells["col_Record_Count"].Value.ToString().Replace(replace_char.ToString(), ""))
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM