2 Replies - 409 Views - Last Post: 01 October 2017 - 07:20 PM Rate Topic: -----

#1 Booney440  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-November 16

Datagrid Validating

Posted 01 October 2017 - 06:30 AM

I have a Datagrid with several columns, if one column is edited on that row i need another column to be mandatory on the same row.The code below makes the the column mandatory regardless of what column in the row is edited. If I click the wrong row I have to give that row information Bad ides.This is what I have tried.


private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
             if (dataGridView1.Rows[e.RowIndex].Cells[10].FormattedValue.ToString() == string.Empty)
        {
            e.Cancel = true;
            dataGridView1.Rows[e.RowIndex].Cells[10].ErrorText = "Column 10";
        }
        else
        {
            dataGridView1.Rows[e.RowIndex].Cells[10].ErrorText = string.Empty;
        }
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Datagrid Validating

#2 Booney440  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-November 16

Re: Datagrid Validating

Posted 01 October 2017 - 06:49 AM

I have also tried this it pops up a similar error provider icon but I can ignore it, instead of forcing the user it input information in that cell.


 private void dataGridView1_CellValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            if ((e.ColumnIndex >= 0 && e.ColumnIndex < this.dataGridView1.Columns.Count) &&
            (e.RowIndex >= 0 && e.RowIndex < this.dataGridView1.Rows.Count))
            {
                if (this.dataGridView1.Columns[e.ColumnIndex].Name == "departureYNDataGridViewTextBoxColumn")


                //  if (dataGridView1.Rows[e.RowIndex].Cells[10].FormattedValue.ToString() == string.Empty)
                {
                    e.Cancel = true;
                    dataGridView1.Rows[e.RowIndex].Cells[10].ErrorText = "Yard Location is Mandatory";
                }
                else
                {
                    dataGridView1.Rows[e.RowIndex].Cells[10].ErrorText = string.Empty;
                }
            }
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5887
  • View blog
  • Posts: 20,095
  • Joined: 05-May 12

Re: Datagrid Validating

Posted 01 October 2017 - 07:20 PM

It seems to me that one approach would be to use the cell editing event to keep track of which cells were edited. When you finally get the row validation event, then check to which cells were actually edited and do you validation based on that. You'll need some XOR logic like this pseudo code:
if (columnDirty[requiredColumn1Index] ^ columnDirty[requiredColumn2Index])
{
    consider the row invalid
}



Alternatively something like this pseudo code if :
if ((requiredColumn1.Value != untouchedValueForColumn1) ^
    (requiredColumn2.Value != untouchedValueForColumn2))
{
    consider the row invalid
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1