问题
In my datagridview
, four columns 1 & 2 are read only col 3 & 4 got number values. I want to compare that 4th column must be greater that 3rd column.
For example:
If the 4th column value is less than the 3rd column then I want to propose message doesn't navigate to another control.
My simple approach seems not working. How can I compare 2 specific columns for this kind of condition?
private void datagridview_CellValidating(object sender, CellValidatingEventArgs e)
{
try
{
int bbor = datagridview.CurrentCell.ColumnIndex;
int ebor = datagridview.CurrentCell.RowIndex;
if (ebor <= bbor)
{
MessageBox.Show("Please verify the value");
e.Cancel = true;
}
}
catch (Exception exception)
{
}
}
回答1:
we meet again. Use the cell_click event:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex != 0)
{
if (Double.Parse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) <= Double.Parse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString()))
{
MessageBox.Show("Please verify the value");
}
}
}
EDIT 1: This seems to work fine, lemme know.
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex != 0)
{
if (Double.Parse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) <= Double.Parse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString()))
{
MessageBox.Show("Please verify the value");
e.Cancel = true;
}
}
}
Edit 2: Updated for Telerik controls
private void radGridView1_CellValidating(object sender, Telerik.WinControls.UI.CellValidatingEventArgs e)
{
if (e.ColumnIndex != 0)
{
if (e.Value != null && radGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value != null)
{
if (Double.Parse(e.Value.ToString()) <= Double.Parse(radGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString()))
{
MessageBox.Show("error");
e.Cancel = true;
}
}
}
}
回答2:
I'd see something more or less like that:
If you want to check all rows:
DataRow dr;
for(int i = datagridview.Rows.Count-1; i > 0; i--) {
dr = datagridview.Rows[i];
if(dr[e.ColumnIndex] > dr[e.ColumnIndex+1]){
//your message code
e.Cancel = true;
break; (or return;)
}
}
If you want to check only the current row where the cell is being edited:
DataRow dr = datagridview.Rows[e.RowIndex];
e.Cancel = dr[e.ColumnIndex] > dr[e.ColumnIndex+1];
if(e.Cancel)
//your message code
Maybe you will need to convert objects to int for comparison.
回答3:
See the Rows Property for DataGridView http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx
this.dataGridView1[col, row].Value
references a specific cell for each row
foreach (Row r in this.dataGridView1.Rows) {
if (r.Cells[3].Value <= r.Cells[2].Value ) {
System.Console.WriteLine ("error");
}
}
回答4:
For your validation check you'll want to use the FormattedValue property to see what value your user wants to insert in the cell they've edited. You can't use the current cell value because it doesn't update to the new value until after the CellValidating
completes without DataGridViewCellValidatingEventArgs.Cancel
being set to true.
Something like this:
private void datagridview_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
// This is the new proposed value the user entered; could be for column 3 or 4.
int newValue = int.Parse(e.FormattedValue.ToString());
// See which column fired the CellValidating event and use the new proposed value for it
// in place of the cell's actual value for purposes of our validation.
int col3Value = (e.ColumnIndex == 2) ? newValue : (int)dataGridView1[2, e.RowIndex].Value;
int col4Value = (e.ColumnIndex == 3) ? newValue : (int)dataGridView1[3, e.RowIndex].Value;
if (col3Value <= col4Value) {
MessageBox.Show("Please verify the value");
e.Cancel = true;
}
}
The code I show here is to demonstrate a solution to your problem. In your actual production code
you'll want to verify that casting from object to int is successful (through int.TryParse) or catch the exception that's raised when this operation fails. When this happens you can Cancel = true
the cell validation and present to the user a message that he must enter a number.
And another quick note: don't use empty catch blocks (though I realize this probably isn't in your production code).
来源:https://stackoverflow.com/questions/13259013/compare-2-different-columns-in-the-datagridview