You must Sign In to post a response.
  • Category: Visual Studio

    Gridview Cell Validate with Numeric values

    Hi All,

    In vb.net windows form, i have a gridview with 8 columns.
    MobileNumber is one of the column in 8's.
    So I need to validate the column, to enter only numeric values, if they type any other character, should alert them with messagebox.


    And also validate length of the column, also allow's like +91-xxxxxxxxxx

    Regards,
    Ramkumar G.R
  • #753850
    There are following two ways to check validation of textboxes for entering data.

    1) Validate through javascript.
    2) use of ajax toolkit – FilteredTextBoxExtender
    Code snippet for validation in C#

    private void dGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
    if (e.ColumnIndex == 0) {
    string val = dGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
    if (!(Microsoft.VisualBasic.Information.IsNumeric(val) || val.Contains(".")))
    {
    MessageBox.Show("Please Enter Numberic value'.' character");
    dGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = String.Empty;
    }
    }
    }


    Reference link
    asp.net/ajax/ajaxcontroltoolkit/Samples/FilteredTextBox/FilteredTextBox.aspx

    codeproject.com/Questions/418807/validating-data-in-data-gridview-using-csharp-net

  • #753868
    Hi,

    If you want to enter numeric numbers then refer below sample code.


    <script type="text/javascript">
    function IsNumFieldKeyPress() {
    var element;
    element = event.srcElement;
    if (event.keyCode >= 48 && event.keyCode <= 57) {//to handle num 0 -9
    event.returnValue = true;
    return;
    }
    event.returnValue = false;
    }
    </script>



    Using above sample it's accept only numeric values.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #753870
    Hai Ramkumar,
    As you are working with the windows application, so the validations will be different than the web application validations.
    You need to have the keypress event of the editable textbox which is inside the GridView. The EditingControlShowing is the event handler which will make your textbox editable and once it is editable,you can validate.So you need to attach the keypress event inside this event handler.
    Below is the code snippet which you can use to validate the textbox for the validation of numeric:

    private void grViewMedications_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
    var textControl = e.Control as DataGridViewTextBoxEditingControl;
    if (textControl != null)
    {
    textControl.KeyPress += new KeyPressEventHandler(textControl_KeyPress);
    }
    }
    }

    Now you need to handle the keypress event as below:

    void textControl_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (!Char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back)
    {
    e.Handled = true;
    MessageBox.Show("Please enter only numbers!!!");
    }
    }

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #753884
    [Response removed by Admin. Read forum policies.]
    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM


  • Sign In to post your comments