none
DefaultCellStyle Format problem RRS feed

  • Question

  • I have a dataGridView control on my form with a job no column. I would like to format the job no column in the format "99-999". 

    I have used the format property of the dataGridView control but the dash does not appear when entering data into each row without clicking the bindingNavigator add (+) button.

    How to make the dash appear each time when entering data?

    Thanks,

    Isom Wilson, Jr.

    Friday, September 29, 2017 8:59 PM

Answers

  • Hi isomcsa,

    For your question, you could try the following code.

    private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Level");
                dt.Columns.Add("PartNumber");
                dt.Columns.Add("job no");
                //// dt.Rows.Add(0, "Item A", "Number");
                //dt.Rows.Add(1, "List 1", "11111");
                //dt.Rows.Add(1, "List 3", "12345");
                //dt.Rows.Add(1, "List 6", "67890");
                //dt.Rows.Add(1, "List 7", "22222");
                //dt.Rows.Add(1, "List 8", "");           
                dataGridView1.DataSource = dt;
               
            }
    
    
            private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == 2)
                {
                    for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
                    {
                        string st = dataGridView1.Rows[i].Cells[2].Value.ToString();
                        dataGridView1.Rows[i].Cells[2].Value = st.Insert(2, "-");
                    }
    
                }
    
            }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by isomcsa Tuesday, October 10, 2017 8:33 PM
    • Unmarked as answer by isomcsa Tuesday, October 10, 2017 8:34 PM
    • Marked as answer by isomcsa Tuesday, October 10, 2017 8:34 PM
    Friday, October 6, 2017 1:59 AM
    Moderator

All replies

  • Hi isomcsa,

    For your question, you could try the following code.

    private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Level");
                dt.Columns.Add("PartNumber");
                dt.Columns.Add("job no");
                //// dt.Rows.Add(0, "Item A", "Number");
                //dt.Rows.Add(1, "List 1", "11111");
                //dt.Rows.Add(1, "List 3", "12345");
                //dt.Rows.Add(1, "List 6", "67890");
                //dt.Rows.Add(1, "List 7", "22222");
                //dt.Rows.Add(1, "List 8", "");           
                dataGridView1.DataSource = dt;
               
            }
    
    
            private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == 2)
                {
                    for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
                    {
                        string st = dataGridView1.Rows[i].Cells[2].Value.ToString();
                        dataGridView1.Rows[i].Cells[2].Value = st.Insert(2, "-");
                    }
    
                }
    
            }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by isomcsa Tuesday, October 10, 2017 8:33 PM
    • Unmarked as answer by isomcsa Tuesday, October 10, 2017 8:34 PM
    • Marked as answer by isomcsa Tuesday, October 10, 2017 8:34 PM
    Friday, October 6, 2017 1:59 AM
    Moderator
  • This allowed the user to input only the numerals in the job number column without having to enter the hyphen and the hyphen was entered by the code.

    The only difference is that I used it for the present row by using the code below

                if (e.ColumnIndex == 2)
                {
                    string strJobNo2 = dataGridViewFuel.Rows[m_RowPosition].Cells[2].Value.ToString();
                    if (strJobNo2.IndexOf("-") == -1)
                    {
                        dataGridViewFuel.Rows[m_RowPosition].Cells[2].Value = strJobNo2.Insert(2, "-");
                    }
                }

    Tuesday, October 10, 2017 9:56 PM
  • Hi isomcsa,

    Thanks for your sharing. If you have something else about this issue, please feel free to contact us.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 11, 2017 8:39 AM
    Moderator