在C#中验证DataGridView文本框单元格?
我的表单中有两个DataGridViewTextbox列。我想将一个文本框与另一个文本框值进行比较 请参见此处提供的信息的图像 蓝色的是只读的……我试过了,我得到了答案在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"].
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我没有要求单元格值。发放的数量总和不大于所需耗材的值