C# C语言中数据网格的验证#
我希望验证datagrid单元,以便它进行检查以确保用户输入的值为100C# C语言中数据网格的验证#,c#,validation,datagridview,C#,Validation,Datagridview,我希望验证datagrid单元,以便它进行检查以确保用户输入的值为100 您可以尝试对CellValueChanged事件进行验证 private void dgvAssetCost_CellValueChanged(object sender, DataGridViewCellEventArgs e) { int percentColumnIndex = 2; //This will represent the column index of the c
您可以尝试对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
}
}
}
你可以试试这个技巧
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
if (e.ColumnIndex == 0)
{
if (int.Parse(dataGridView1[e.ColumnIndex, e.RowIndex].EditedFormattedValue.ToString()) != 100)
{
e.Cancel = true;
MessageBox.Show("The inputed is not 100%. Please check. Press Esc to cancel change.");
}
else
{
//Successcully entered
}
}
}
如果(PerCentV!=“100”){您的说法是什么?如果PerCentV!=100,请插入条件,否则显示消息框“提交错误”?
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
if (e.ColumnIndex == 0)
{
if (int.Parse(dataGridView1[e.ColumnIndex, e.RowIndex].EditedFormattedValue.ToString()) != 100)
{
e.Cancel = true;
MessageBox.Show("The inputed is not 100%. Please check. Press Esc to cancel change.");
}
else
{
//Successcully entered
}
}
}