gpt4 book ai didi

c# - 在 C# 中验证数据网格

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:08 24 4
gpt4 key购买 nike

我正在寻找验证数据网格单元格,以便它检查以确保用户输入的值是 <> 到 100

private void InsertCostSpilt () 
{
{
try
{
string AssetNumberV = txtNumber.Text;
string DeptV = dgvAssetCost.Rows[dgvAssetCost.CurrentRow.Index].Cells["DEPT"].Value.ToString(); // Gets values of the department cell
string CCV = dgvAssetCost.Rows[dgvAssetCost.CurrentRow.Index].Cells["CC"].Value.ToString(); // Gets values of the Cost centre cell
string PerCentV = dgvAssetCost.Rows[dgvAssetCost.CurrentRow.Index].Cells["PER_CENT"].Value.ToString(); // Gets Values of the Precentage cell

SQLMETHODS.InsertCostSpilt(AssetNumberV, DeptV, CCV, PerCentV); // SQL insert methods, for insering new cost spilts

MessageBox.Show("Cost Spilt details have been successfully entered.", "Success!"); // Success Messagebox
}

catch (Exception ex)
{
MessageBox.Show(" Error submitting Cost Spilt details into entry table. processed with error:" + ex.Message); // Error Messagebox
}
}
}

最佳答案

您可以尝试对 CellValueChanged 事件进行验证。

private void dgvAssetCost_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int percentColumnIndex = 2; //This will represent the column index of the column you want to check.
int comparisonValue = 100;

try
{
if (e.ColumnIndex == PercentColumnIndex)
{
//Perform your check in here.
if (Convert.ToInt32(dgvAssetCost.Rows[e.RowIndex].Cells[e.ColumnIndex].Value) != comparisonValue)
{
//Do nothing or perform an acrtion when their value is correct.
SQLMETHODS.InsertCostSpilt(AssetNumberV, DeptV, CCV, PerCentV); // SQL insert methods, for insering new cost spilts

MessageBox.Show("Cost Spilt details have been successfully entered.", "Success!"); // Success Messagebox
}
else
{
//Do something to alert user that their input is incorrect.
MessageBox.Show("Cost Spilt details have not been successfully entered!", "Unsuccessful!"); // Success Messagebox
}
}
}
catch(Exception ex)
{
MessageBox.Show(" Error submitting Cost Spilt details into entry table. processed with error:" + ex.Message); // Error Messagebox
}
}
}

关于c# - 在 C# 中验证数据网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13584459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com