簡體   English   中英

DataGridView TextBox單元格在C#中驗證?

[英]DataGridView TextBox Cell Validating in C#?

我的窗體中有兩個DataGridViewTextbox列。 我想將一個文本框與另一個文本框值進行比較。

看到圖像那里提供的信息...,

它們是藍色的,它們是只讀的...我嘗試過,我得到了答案

嘗試這個

if (DataGridView.Rows[0].Cells["Quantity"].Value > DataGridView.Rows[0].Cells["other"].Value)
{
//do something
}

將網格附加到CellValidating事件:

private void GridCellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if (e.ColumnIndex == ColQuantityIssues.Index)  // Quantity Issues TextBox value has changed
    {
        int quantityIssued = 0;

        if (!Int32.TryParse(e.FormattedValue.ToString(), out quantityRequired))  //value is not a valid number
        {
            e.Cancel = true;
        }

        int quantityRequired = Int32.Parse(dgv.Rows[e.RowIndex].Cells[ColQuantityRequired.Index].Value.ToString());

        if (quantityRequired < quantityIssued)
        {
            e.Cancel = true;
        }
    }
}

事件args具有FormattedValue ,這是用戶輸入的新值。 只要值無效,將e.Cancel設置為true不會允許用戶離開當前單元格。

您可以更改我的ColQuantityIssues和ColQuantityRequired以通過名稱訪問它們(即dgv.Rows [e.RowIndex] .Cells [“ Required”]),但這應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM