Posts Tagged ‘valuetype’

DataGridView - setting error message for wrong data in ValueType columns

Tuesday, April 8th, 2008

I had to write an application where I used a DataGridView with various DataTypes columns. For the dateTime column I used a Calendar Column, but for the Int32 columns, I wanted to display a message that would say to the user that they inserted some wrong data. I have found an example on http://www.codeguru.com/forum/showthread.php?t=418306 , but the example is in VB.NET. I wrote something in C#:

private void callOnEdit(object sender, DataGridViewDataErrorEventArgs e){
try
{
switch (e.ColumnIndex) {
case 1: MessageBox.Show(“Message.”);break;
//and so on
}
}
catch
{
MessageBox.Show(
“Message!”);
}
}

To use this, you only have to go in the Events panel for the DataGridView and set the DataError event to be handled by this function.

Please note that in VB.NET you can use “Handles”, but in C# you can’t. This is why:

http://www.velocityreviews.com/forums/t367616-using-quothandlesquot-vb-vs-c.html