hi there,

i have a datagridview in the desktop application that i made and one of the columns in that is the telephone number.

the telephone number is valid if the user type in (630) 345-2532.

so everywhere the user hast to type in the paranthasis and then the numbers with a dash.

is there any way i can make the datagridview column to make it display as (___) ___-____

appriciate if some one can give an guidance on this

thanks

There is a better option:
Allow users to enter a full telephone number, without brackets and dashes. The number like 6303452532 - all together. If the number have to be a type of long. Then you do the checking if the value is really a long. If its not user has to repair the number. If its OK, with confirming (pressing enter or button, or whatever), value changes to your type, like you have in example.
Thats the simpliest solution,and you wont bother users with entering brackets ans stuff. Here`s the code:

private void CheckingValue()
        {     
            
            long value = 0;
            int row = this.dataGridView1.CurrentCell.RowIndex;
            int column = this.dataGridView1.CurrentCell.ColumnIndex; //or column is always the same you only specify it bellow:
            string str = this.dataGridView1[column, row].Value.ToString(); //[0, row].Value.ToString(); - 0 represents the 1st column
            bool bChecking = long.TryParse(str, out value);
            if (bChecking)
            {
                str = String.Format("{0:(###) ###-####}", value);
                //insert new value into the cell!
                this.dataGridView1[column, row].Value = str;
            }
            else
                MessageBox.Show("Tel. number is not correct. Please re-enter it.");
        }

There is a better option:
Allow users to enter a full telephone number, without brackets and dashes. The number like 6303452532 - all together. If the number have to be a type of long. Then you do the checking if the value is really a long. If its not user has to repair the number. If its OK, with confirming (pressing enter or button, or whatever), value changes to your type, like you have in example.
Thats the simpliest solution,and you wont bother users with entering brackets ans stuff. Here`s the code:

private void CheckingValue()
        {     
            
            long value = 0;
            int row = this.dataGridView1.CurrentCell.RowIndex;
            int column = this.dataGridView1.CurrentCell.ColumnIndex; //or column is always the same you only specify it bellow:
            string str = this.dataGridView1[column, row].Value.ToString(); //[0, row].Value.ToString(); - 0 represents the 1st column
            bool bChecking = long.TryParse(str, out value);
            if (bChecking)
            {
                str = String.Format("{0:(###) ###-####}", value);
                //insert new value into the cell!
                this.dataGridView1[column, row].Value = str;
            }
            else
                MessageBox.Show("Tel. number is not correct. Please re-enter it.");
        }

So I get the value that the user enter in the datagridview cell and inserts into the method you have shown me and then I am trying to display it in the same datagridview cell that the user enter the tel number,
When I try to clear the value that the user entered and display the formatted new value (456) 456-4566
I have a problem In clearing the value In the datagridview,
When I add the code the datagrid dosen’t clear

this.dgvSubContractor.Rows[row].Cells[3].Value = null;
                        this.dgvSubContractor[3, row].Value = c.CheckingValue(e.FormattedValue.ToString()).ToString();
                        dgvSubContractor.Rows[e.RowIndex].ErrorText = string.Empty;

how can I clear the datagridview value

What exactly do you mean? Do you mean to clear only a specific cell, or a shole row, or maybe a whole dgv?

if this is a cell you can do it, like:

int row = dgv.CurrentCell.RowIndex;
int column = dgv.CurrentCell.ColumnIndex;
this.dgv[column, row].Value = null; //or String.Empty, or "";

If you still have question, please go ahead andask it.

Mitja

What exactly do you mean? Do you mean to clear only a specific cell, or a shole row, or maybe a whole dgv?

if this is a cell you can do it, like:

int row = dgv.CurrentCell.RowIndex;
int column = dgv.CurrentCell.ColumnIndex;
this.dgv[column, row].Value = null; //or String.Empty, or "";

If you still have question, please go ahead andask it.

Mitja

no it is like this
in a datagridview cell say dgv.cell[3,1].value the user enters 2345678901 as the telephone number ok
then what my code does is gets the value that the user enterd which is 2345678901 and inserts it to the method that you have shown above and then copies the telephone number in the format (234) 567-8901 in to the the datagriview cell which i have mentioned above,
the problem is i can't clear the original value that the user type in and copy the formatted value to the cell,

i tried setting the value as string.Empty , null, ""
dosen't work

i have to clieck on the datagridview to make it change in the formatted value

how can i solve this

If I understand you, in a cell you have 2 values:
1. the one entered
2. and the one with new fomrat

Am I right?

If I understand you, in a cell you have 2 values:
1. the one entered
2. and the one with new fomrat

Am I right?

ohh no
not two values i am overriding the first value
that is what i am trying to do

I really dont understand what is the problem. So you have the new value (formated one) in the cell.
And now what (when you want to delate it, to enter a new one)?

I really dont understand what is the problem. So you have the new value (formated one) in the cell.
And now what (when you want to delate it, to enter a new one)?

it is simple
in one cell i have a value ok
from the code i generate a formatted value
i want copy the formatted value to the cell

Which code generated formated value? and btw, why do you set a value as formated?

Which code generated formated value? and btw, why do you set a value as formated?

value enter by the user 4561234567
formattwed value (456) 123-4567
the code that formats the value is the one you have posted earlier

Ok, but I still dont understand what exactly would you like to clear? The "formated value" as you called it, it appears in the cell. So what to erase?
Come on, please do a clear explanation of the problem now.

ok, but i still dont understand what exactly would you like to clear? The "formated value" as you called it, it appears in the cell. So what to erase?
Come on, please do a clear explanation of the problem now.

how do i clear a value in a datagridview cell

not a datagridview row

When exactly do you want to erase the value in the cell? On a mouse double click or when?

If its on a double click, you have to create a new method, and put this code into it:

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = "";
        }

If its on a double click, you have to create a new method, and put this code into it:

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = "";
        }

hey in you code i have displayed below how to make it restrict the user to enter only 10 digits??????
it you add more that 10 digits it will, take all the numbers and format it
but i need only 10 number how can i do this

please can some one help me you

private void CheckingValue()
        {     
            
            long value = 0;
            int row = this.dataGridView1.CurrentCell.RowIndex;
            int column = this.dataGridView1.CurrentCell.ColumnIndex; //or column is always the same you only specify it bellow:
            string str = this.dataGridView1[column, row].Value.ToString(); //[0, row].Value.ToString(); - 0 represents the 1st column
            bool bChecking = long.TryParse(str, out value);
            if (bChecking)
            {
                str = String.Format("{0:(###) ###-####}", value);
                //insert new value into the cell!
                this.dataGridView1[column, row].Value = str;
            }
            else
                MessageBox.Show("Tel. number is not correct. Please re-enter it.");
        }

Ok, I did an example code only for you. It shows how to start editing the selected cell, and how to end editing. And if there are errors, user has to be notified about them. I hope you will get an idea, of how this is needed to be done.

Here we go:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Dec10Exercise
{
    public partial class Form1 : Form
    {
        int count;

        public Form1()
        {
            InitializeComponent();
            PopulateDGV();
        }

        private void PopulateDGV()
        {
            DataGridViewTextBoxColumn column1 = new DataGridViewTextBoxColumn();
            {
                //column1.ReadOnly = true;
                column1.Name = "column1";
                column1.HeaderText = "State";
                column1.ValueType = typeof(string);
                column1.Resizable = DataGridViewTriState.False;
                //column1.SortMode = DataGridViewColumnSortMode.Automatic;
                column1.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                column1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                column1.Width = 200;
            }
            dataGridView1.Columns.Add(column1);
            //creating method
            dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
            dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.dataGridView1.BeginEdit(true);
        }

        bool bShowing;
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (!bShowing)
            {
                int row = dataGridView1.CurrentCell.RowIndex;
                int column = dataGridView1.CurrentCell.ColumnIndex;
                string item = dataGridView1[column, row].Value.ToString();
                if (item.Contains("("))
                    item = item.Replace("(", "");
                if (item.Contains(")"))
                    item = item.Replace(")", "");
                if (item.Contains("-"))
                    item = item.Replace("-", "");

                count = item.Length;
                if (count < 10)
                {
                    MessageBox.Show("The number is too short, it has to be 10 characters long.\nPlease repair the entry...", "Too short number");
                    bShowing = true;
                    dataGridView1.BeginEdit(true);
                }
                else if (count > 10)
                {
                    MessageBox.Show("Inserted number is too long, it can only be 10 characters long.\nPlease repair the entry...", "Too long number");
                    bShowing = true;
                    dataGridView1[column, row].Value = "";
                    dataGridView1.BeginEdit(true);
                }
                else
                {
                    dataGridView1.EndEdit();
                    CheckingValue();
                }
            }
            else
                bShowing = false;
        }

        private void CheckingValue()
        {

            long value = 0;
            int row = this.dataGridView1.CurrentCell.RowIndex;
            int column = this.dataGridView1.CurrentCell.ColumnIndex; //or column is always the same you only specify it bellow:
            string str = this.dataGridView1[column, row].Value.ToString(); //[0, row].Value.ToString(); - 0 represents the 1st column
            bool bChecking = long.TryParse(str, out value);
            if (bChecking)
            {
                str = String.Format("{0:(###) ###-####}", value);
                //insert new value into the cell!
                this.dataGridView1[column, row].Value = str;
            }
            else
                MessageBox.Show("Tel. number is not correct. Please re-enter it.");
        }       
    }
}

Hope it helps, but please take some time and study it. The code works.

Mitja

hi,
i have the coding for datagrid view in javascript.its for educational details and i used jsp coding.the options are phd,pg,ug,12th, and 10th,if i choose phd it will show 5 rows and corresponding columns.i didnt got the output for that.please give the output for me.here i attach my coding.

    <script type="text/javascript">
 function education()
            {
                var edu=document.ff.submit.value;
                if(edu ==  0)
                {
                    document.getElementById("study").innerHTML="Select Your Qualification";
                    document.ff.submit.focus();
                }
                else
                {
                    document.ff.getElementById("study").innerHTML="";
                }
            }

</script>

<body>
<form name="ff" method="post">
<table  width="843" height="238" border="1">
  <tr>
    <td colspan="4" align="center"><h4>EDUCATION DETAILS</h4></td> 
                                </tr>  
                                <tr><td width="171">Education Qualification</td>
                                    <td width="187"><select name="course"  value="show">
                                            <option value="0">--select--</option>
                                            <option value="5">Ph.D</option>
                                            <option value="4">PG</option>
                                            <option value="3">UG</option>
                                            <option value="2">12th</option>
                                            <option value="1">10th</option>
                                  </select><div id="study" class="errmsg"></div></td>
                                  <td colspan="2" width="523">
                                      <input type="submit" value="Show" name="submit" onclick="education()"/> </td>

  </tr>
                                <tr><td height="163"  colspan="4">
                                        <table border="1" cellpadding="5" align="center" cellspacing="0">
                                            <tr>

                                                <th>S.No</th> 
                                                <th>Course</th>
                                                <th>Subject</th>
                                                <th>Institution</th>
                                                <th>Year Of Passing</th>
                                                <th>Percentage</th>

                                            </tr>

                                            <%
                                                int name = 0;
                                                try {
                                                    name = Integer.parseInt(request.getParameter("course").toString());
                                                    for (int i = 1; i <= name; i++) {
                                            %>
                                            <tr>
                                                <td><%= i%></td>
                                                <td><input type="text" name="ph" value="" /></td>
                                                <td><input type="text" name="pgra" value="" /></td>
                                                <td><input type="text" name="ugra" value="" /></td>
                                                <td><input type="text" name="12" value="" /></td>
                                                <td><input type="text" name="10" value="" /></td>

                                            </tr>
                                            <% }
                                                } catch (RuntimeException e) {
                                                    e.printStackTrace(System.err);
                                                }%>
                                        </table>
</form>
</body>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.