如何在gridview中的验证错误上设置当前单元格的焦点? [英] How do I set focus on current cell on validation error in gridview?

查看:116
本文介绍了如何在gridview中的验证错误上设置当前单元格的焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hie Friends,



我试图在C#的Windows应用程序中将焦点设置在网格视图的当前单元格上。当在单元格中输入的数据无效时,在显示适当的提示后,光标焦点应设置为当前单元格,但不能设置为下一个单元格,因为我还写了光标移动到gridview keydown上的下一个单元格的功能活动。



请帮我解决这个问题。



谢谢

Varun



我尝试过:



Hie Friends,

I am trying to set focus on current cell of grid view in windows application with C#. When the data entered in the cell is not valid then after showing an appropriate prompt the cursor focus should be set to current cell, but not to the next cell, As I have also written the functionality of cursor being moved to next cell on gridview keydown event.

Kindly help me resolve this issue.

Thanks
Varun

What I have tried:

private void dvItemGrid_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (dvItemGrid.Columns[e.ColumnIndex].Name == "ItemID")
            {
                string strItemID = e.FormattedValue.ToString();
                if (strItemID == "")
                {
                    MessageBox.Show("Item ID cannot be blank.");
                    dvItemGrid.CurrentCell = this.dvItemGrid.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    return;
                }
            }
        }
        private void dvItemGrid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;

                int iColumn = dvItemGrid.CurrentCell.ColumnIndex;
                int iRow = dvItemGrid.CurrentCell.RowIndex;
                if (iColumn == dvItemGrid.ColumnCount - 1)
                    dvItemGrid.CurrentCell = dvItemGrid[1, iRow - 1];
                else
                    dvItemGrid.CurrentCell = dvItemGrid[iColumn + 1, iRow];
            }
        }

推荐答案

尝试使用dvItemGrid_CellValidating void中的sender和e对象,它具有你需要处理的属性,让我们认为对象e具有属性索引。所以你可以将这个索引归属于一个公共的int var,然后将焦点设置为这个索引。
Try to use the object "sender" and "e" from your dvItemGrid_CellValidating void, it has the properties you need to handle , let's supose that the object "e" has the property index. so you can atribute this index to a public int var and after set the focus to this index.


这篇关于如何在gridview中的验证错误上设置当前单元格的焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆