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

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 に答える