2

我正在寻找验证数据网格单元格,以便它检查以确保用户输入的值是 <> 到 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 
            }
        }       
    }
4

2 回答 2

0

你可以试试这个技巧

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
                    }
                }
        }
于 2012-12-02T14:09:13.893 回答
0

您可以尝试对 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 
                }
            }
        }
于 2012-11-27T12:54:51.333 回答