C#中DataGridView实现某一列只能输入数字

public  DataGridViewTextBoxEditingControl CellEdit = null; // 声明 一个 CellEdit

  private void datagridyf_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
        
            CellEdit = (DataGridViewTextBoxEditingControl)e.Control; // 赋值
            CellEdit.SelectAll();
            CellEdit.KeyPress += Cells_KeyPress; // 绑定到事件 
        }

  // 自定义事件

        private void Cells_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (datagridyf.CurrentCellAddress.X == 2) // 判断当前列是不是要控制的列 我是控制的索引值为2的  列(即第三列)
            {
                if ((Convert.ToInt32(e.KeyChar) < 48 || Convert.ToInt32(e.KeyChar) > 57) && Convert.ToInt32(e.KeyChar) != 46 && Convert.ToInt32(e.KeyChar) != 8 && Convert.ToInt32(e.KeyChar) != 13)
                {
                    e.Handled = true;  // 输入非法就屏蔽
                }
                else
                {
                    if ((Convert.ToInt32(e.KeyChar) == 46) && (txtjg.Text.IndexOf(".") != -1))
                    {
                        e.Handled = true;
                    }
                }
            }
        }

下面是在输入完成后才验证的 这个主要是在 CellValidating事件中完成

 private void datagridyf_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == datagridyf.Columns["Pric"].Index )
            {
                datagridyf.Rows [e.RowIndex].ErrorText ="";
                int NewVal=0;
                if (!int.TryParse (e.FormattedValue.ToString (),out NewVal ) || NewVal <0)
                {
                    e.Cancel=true ; 
                    datagridyf.Rows [e.RowIndex].ErrorText ="价格列只能输入数字";
                    return ;
                }
            }
        } 
C#,可以通过以下代码获取DataGridView一列不同的值: ```csharp List<string> distinctValues = new List<string>(); foreach (DataGridViewRow row in dataGridView.Rows) { string value = row.Cells\["ColumnName"\].Value.ToString(); if (!distinctValues.Contains(value)) { distinctValues.Add(value); } } // distinctValues列表存储了该列的不同值 ``` 这段代码遍历了DataGridView的每一行,获取指定列的值,并将不同的值添加到distinctValues列表。最后,distinctValues列表存储了该列的不同值。请注意,"ColumnName"需要替换为你实际使用的列名。 #### 引用[.reference_title] - *1* [C#DataGridView绑定数据bindingSource,获取datagridview某一行某一列的值](https://blog.csdn.net/dream_follower/article/details/52843567)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C#控件DataGridView通过一列的值改变整行颜色](https://blog.csdn.net/baidu_38995168/article/details/86629995)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值