Paradox
Paradox

Reputation: 129

How to validate datagridview input by the value type of column in c#?

I am trying to validate datagridview input by the value type of respective column, this is my code:

 private void dgvLoadTable_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
 {
   foreach (DataGridViewRow dr in dgvLoadTable.Rows)
   {
    foreach (DataGridViewCell dc in dr.Cells)
    {
      if (dc.GetType() == typeof(Int32) || dc.GetType() == typeof(Double))//if(dc.ColumnIndex==0)
      {
       DataGridViewTextBoxCell cell = dgvLoadTable[e.ColumnIndex, e.RowIndex] as DataGridViewTextBoxCell;
       if (cell != null)
       {
        char[] chars = e.FormattedValue.ToString().ToCharArray();
        foreach (char c in chars)
        {
         if (char.IsDigit(c) == false)
         {
          MessageBox.Show("You have to enter digits only");
          e.Cancel = true;
          break;
         }} }
          }
         }
        }
    }

But this is not working, what should be the type for a number column? FYI, this is an winForm application and different data is loaded in the datagridview on runtime by user choice. So columns are not known previously. The datagridview is bound to an entity model.

Upvotes: 0

Views: 8373

Answers (3)

Fahad
Fahad

Reputation: 1

Sorry about the last one, here is a new block of code. I've tried it its working.

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        int x;

        if (this.Visible && !int.TryParse(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString(), out x))
        {
            MessageBox.Show("You have to enter digits only");
            dataGridView1[e.ColumnIndex, e.RowIndex].Value = "";
        }
    }

Upvotes: 0

Fahad
Fahad

Reputation: 1

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        foreach (DataGridViewRow dr in dataGridView1.Rows)
        {
            foreach (DataGridViewCell dc in dr.Cells)
            {
                int x;
                double y;
                if (int.TryParse(dc.Value.ToString(),out x) || double.TryParse(dc.Value.ToString(),out y))
                {
                    MessageBox.Show("You have to enter digits only");
                }

            }
        }
    }

I hope this helps you. I recommend you use the tryparse functions to see if the value entered is a number or not. You may use this function for DataGridView_CellValueChanged function as well.

Upvotes: 0

SebSky
SebSky

Reputation: 171

First this depends on a datatype in your source collection. So it may be decimal or some other numeric values. Best if you put conditional a breakpoint in this method for certain cell (column).

Also I would avoid iterating thru each row anc column because this method is called for specific cell.

So rather than doing foreach for row / cell you may want to get exact data for cell by using e.RowIndex and e.ColumnIndex to extract cell data object from your data source.

My src looks is a list of objects:

class TestData
{
     public Decimal Value { get; set; }
     public string Name { get; set; }
}

Initialized like this:

List<TestData> src = new List<TestData>();
src.Add(new TestData() { Name = "Test 1", Value = 10 });
src.Add(new TestData() { Name = "Test 2", Value = 20 });
dataGridView1.DataSource = src;

And my test can be done here:

  private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        var row = dataGridView1.Rows[e.RowIndex];
        if ( null != row)
        {
            var cell = row.Cells[e.ColumnIndex];
            if ( null != cell)
            {
                object value = cell.Value;
                if ( null != value )
                {
                    // Do your test here in combination with columnindex etc
                }
            }
        }
    }

Upvotes: 1

Related Questions