Set Error Provider for Datagridview

Posted by Naraayanan under C# on 1/25/2013 | Points: 10 | Views : 14412 | Status : [Member] | Replies : 2
Hi All,
How to set Error Provider for Datagridview when Row is empty? I am working on Windows Application.

Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/



Responses

Posted by: Durgammahesh22 on: 1/30/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

Instead of using ErrorProvider, you can use the DataGridView built-in functionality

A DataGridViewCell has an ErrorText property, you can use it to show the error message.

What you have to do is, you handle the CellValidating event and if fails the validation, you set the error text property, and you get that red error icon to show up in the cell.

pseudo code:


private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (Your Validation) //Your custom validation
{
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Error";
e.Cancel = true; //will prevent user from leaving cell, you can decide that yourself.
}
}


Thanks,
Mahesh

Thanks,
Mahesh

Naraayanan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naraayanan on: 1/30/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Mahesh .
Thanks for your reply.
I just used this code;
  public static bool CheckRowEmpty(DataGridView DGV)

{
if (DGV.Rows[0].Cells.Count > 0)
{


foreach (DataGridViewCell cell in DGV.Rows[0].Cells)
{
if (cell.Value != null)
{
rowIsEmpty = false;
break;
}
else
{
rowIsEmpty = true;
break;
}
}
}
return rowIsEmpty;
}


    if (CheckRowEmpty(datagridiview1))

{
.ErrProvider.SetError(datagridiview1, "Please Fill Skills Information");
}
else
{
ErrProvider.SetError(datagridiview1, "");
}


Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

Naraayanan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response