开发者

Numeric validations for DataGridView in WinForm application

I have a DataGridView in which I have to do cell validations for double a开发者_运维知识库nd integer values as well as checking whether they are empty. How to do it?


For cell value validation you may handle DataGridView.CellValidatingEvent. Here you can process cell values as you wish. For example:

private void dgvMarks_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if(dataGridView1[e.ColumnIndex, e.RowIndex].Value == null)
        e.Cancel = true;
}

If you want to validate numbers, you may do something like that:

int temp = 0;
if(!int.TryParse(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString(), out temp))
    e.Cancel = true;


The datagrid you can cycle through the rows, columns and cells. The solution would be to know which cells you want to get to modify and then change the value. Seeking methods to perform these operations in the msdn. I have no time to give you an example, but at least I hope I've aimed a little.


Here is an article how to validate DataGridView cells. And for double and integer validation use int.TryParse and double.TryParse methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜