使datagridview的cell只能輸入整數的方法

經小弟驗研總結如下,可以在datagridview  CellValidating 事件裏寫入如下方法:      
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            int newInteger;
            if ((!int.TryParse(e.FormattedValue.ToString(),out newInteger) && !(dataGridView1.CurrentCell.EditedFormattedValue.ToString() == "")) || ((newInteger < 0) && !(dataGridView1.CurrentCell.EditedFormattedValue.ToString() == "")))
            {
                MessageBox.Show("錄入數量:“"+dataGridView1.CurrentCell.EditedFormattedValue.ToString()+"”出錯,必須爲整數!");
                e.Cancel = true;
            }

        }
//有新的高招請各位過路大俠留下代碼,共同學習!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章