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

    Allow Only positive numbers in datagridview cell

    Hello friends,

    Its my first windows application where i have a datagridview with

    ItemName(combobox column),Qty,Price,Amount column

    i want user to enter Qty only numbers >0 and should not be (-1) or like that

    can anyone please help me

    Thanks

    Raju.
  • #730452
    Hi Raju,

    Better to call javascript function, Refer below sample javascript to restrict


    <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;
    }
    else if (event.keyCode == 46) { // to handle "."
    if (element.value.indexOf('.', 0) == -1) {
    event.returnValue = true;
    return;
    }
    }
    else if (event.keyCode == 43) { // to handle +
    if (element.value == undefined)
    element.value = "+";
    else if (element.value.charAt(0) != "+" && element.value.charAt(0) != "-")
    element.value = "+" + element.value;
    }
    else if (event.keyCode == 45) { // to handle -
    if (element.value == undefined)
    element.value = "-";
    else if (element.value.charAt(0) != "-" && element.value.charAt(0) != "+")
    element.value = "-" + element.value;
    }
    event.returnValue = false;
    }
    </script>


    Hope this will help you..

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

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

  • #730456
    Hi,

    There is lots of difference between girdview using in Windows application and in Web Apps

    To validate cell value in windows app for allowing only positive numbers, try the following code


    private void dataGridView1_CellValidating(object sender,
    DataGridViewCellValidatingEventArgs e)
    {
    dataGridView1.Rows[e.RowIndex].ErrorText = "";
    int newInteger;

    // Validating the 'new row', you should avoid, as your are editing it
    if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
    if (!int.TryParse(e.FormattedValue.ToString(),
    out newInteger) || newInteger < 0)
    {
    e.Cancel = true;
    dataGridView1.Rows[e.RowIndex].ErrorText = "Please enter only positive values";
    }
    }

  • #730465
    You can try to given code snippet as a guideline for only allow positive numbers in datagridview cell
    private void dataGV1_CellValidating(object sender, DataGVCellValidatingEventArgs e)
    {
    dataGV1.Rows[e.RowIndex].ErrorText = "";
    int newInteger;

    .
    if (dataGV1.Rows[e.RowIndex].IsNewRow) { return; }

    if (!int.TryParse(e.FormattedValue.ToString(),
    out newInteger) || newInteger < 0|| newInteger > 255)
    {
    e.Cancel = true;
    dataGV1.Rows[e.RowIndex].ErrorText = "Enter you values grate then Zero and less then 255";
    }
    }
    I refer to link
    http://blogs.msdn.com/b/ketaanhs/archive/2005/09/19/471117.aspx

  • #730500
    You can check the value of the datagrid cell and can validate as follows


    private void dataGridView1_CellValidating(object sender,
    DataGridViewCellValidatingEventArgs e)
    {
    dataGridView1.Rows[e.RowIndex].ErrorText = "";
    int intVal;


    if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
    if (!int.TryParse(e.FormattedValue.ToString(),
    out intVal) || intVal < 0)
    {
    e.Cancel = true;
    dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a non-negative integer";
    }
    }

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #730809
    Dear Raju,


    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
    if ((int)(((System.Windows.Forms.DataGridView)(sender)).CurrentCell.ColumnIndex) == 1)
    {
    e.Control.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TextboxNumeric_KeyPress);

    }
    }

    private void TextboxNumeric_KeyPress(object sender, KeyPressEventArgs e)
    {
    Boolean nonNumberEntered;

    nonNumberEntered = true;

    if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8)
    {
    nonNumberEntered = false ;
    }

    if (nonNumberEntered == true)
    {
    // Stop the character from being entered into the control since it is non-numerical.
    e.Handled = true ;
    }
    else
    {
    e.Handled = false;
    }

    }


  • Sign In to post your comments