【问题标题】:DataGridView TextBox Cell Validating in C#?在 C# 中验证 DataGridView 文本框单元格?
【发布时间】:2013-05-28 10:39:30
【问题描述】:

我的表单中有两个 DataGridViewTextbox 列。我想将一个文本框与另一个文本框值进行比较。

查看那里提供的信息的图像...,

它们是蓝色的,它们是只读的......我试过了,我得到了答案

【问题讨论】:

  • 你有什么尝试吗?
  • 老板我没有任何想法......,我在想
  • 那么你需要比较什么?您需要比较所需数量与数量问题吗??

标签: c# datagridview add datagridviewtextboxcell


【解决方案1】:

试试这个

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

【讨论】:

  • 嗨@Amir 我不是在询问单元格值。发出的数量总和不大于所需消耗品的值
【解决方案2】:

将您的网格附加到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;
        }
    }
}

事件参数有FormattedValue,这是用户输入的新值。将 e.Cancel 设置为 true 将不允许用户离开当前单元格,只要值无效。

您可以更改我的 ColQuantityIssues 和 ColQuantityRequired 以按名称访问它们(即 dgv.Rows[e.RowIndex].Cells["Required"]),但这应该可以。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多