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

    How to validate gridview cell ?


    Are you looking for a way to validate gridview cell ? then read this thread to know more about it



    Hi,

    I want to key only two characters in datagridview cell. In Datagridview I have 3 columns. Only first column i want to validate that key only 2 characters. I have tried with the below code. But not working.

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
    DataGridViewTextBoxColumn inputbreak = new DataGridViewTextBoxColumn();

    inputbreak.MaxInputLength = 2;

    }

    I have tried this in KeyPress Event too... But not working. Could anyone help me to do this.
  • #744351
    Refer this sample code for the same.



    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

  • #744361
    Hi Anil,

    Thanks for the help. I have done this in the below code.

    private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
    ((DataGridViewTextBoxColumn)dataGridView1.Columns[0]).MaxInputLength = 1;

    }

    -----------------------------------------------------------------------------
    Regards,
    Gopi A.
    +91 9894315571
    Skype:gopi.net
    http://asaigopi-dotnet.blogspot.in/

  • #744498
    Hi,

    To validate gridview cell --

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

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

    Regards

    Regards,
    Dipti Choudhari


  • Sign In to post your comments