Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#中验证DataGridView文本框单元格?_C#_Datagridview_Add_Datagridviewtextboxcell - Fatal编程技术网

在C#中验证DataGridView文本框单元格?

在C#中验证DataGridView文本框单元格?,c#,datagridview,add,datagridviewtextboxcell,C#,Datagridview,Add,Datagridviewtextboxcell,我的表单中有两个DataGridViewTextbox列。我想将一个文本框与另一个文本框值进行比较 请参见此处提供的信息的图像 蓝色的是只读的……我试过了,我得到了答案 if (DataGridView.Rows[0].Cells["Quantity"].Value > DataGridView.Rows[0].Cells["other"].Value) { //do something } 试试这个 if (DataGridView.Rows[0].Cells["Quantity"].

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

请参见此处提供的信息的图像

蓝色的是只读的……我试过了,我得到了答案

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

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;
        }
    }
}
private void GridCellValidating(对象发送方,DataGridViewCellValidatingEventArgs e)
{
如果(e.ColumnIndex==ColQuantityIssues.Index)//数量问题文本框值已更改
{
int quantityIssued=0;
如果(!Int32.TryParse(e.FormattedValue.ToString(),out quantityRequired))//值不是有效数字
{
e、 取消=真;
}
int quantityRequired=Int32.Parse(dgv.Rows[e.RowIndex].Cells[ColQuantityRequired.Index].Value.ToString());
如果(需要的数量<发出的数量)
{
e、 取消=真;
}
}
}
事件args具有用户输入的新值
FormattedValue
。只要值无效,将
e.Cancel
设置为true将不允许用户离开当前单元格


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

将网格连接到
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;
        }
    }
}
private void GridCellValidating(对象发送方,DataGridViewCellValidatingEventArgs e)
{
如果(e.ColumnIndex==ColQuantityIssues.Index)//数量问题文本框值已更改
{
int quantityIssued=0;
如果(!Int32.TryParse(e.FormattedValue.ToString(),out quantityRequired))//值不是有效数字
{
e、 取消=真;
}
int quantityRequired=Int32.Parse(dgv.Rows[e.RowIndex].Cells[ColQuantityRequired.Index].Value.ToString());
如果(需要的数量<发出的数量)
{
e、 取消=真;
}
}
}
事件args具有用户输入的新值
FormattedValue
。只要值无效,将
e.Cancel
设置为true将不允许用户离开当前单元格


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

您尝试了什么吗?老板,我不知道……,我想您需要比较什么?你需要比较所需数量和数量问题吗?你试过什么吗?老板我不知道…,我在想你需要比较什么?您需要比较所需数量和数量问题吗?Hi@Amir我没有要求单元格值。发放的数量总和不大于所需耗材的值Hi@Amir我没有要求单元格值。发放的数量总和不大于所需耗材的值