如果输入的金额低于已知金额,我想强制取消选中DataGridView
中的复选框。
我该怎么做?
以下是代码:
private void Amount(object sender, DataGridViewCellEventArgs e)
{
_columnIndexes = new List<int>(new int[] { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 } );
foreach (int index in _columnIndexes)
{
if (e.ColumnIndex == index)
{
var value = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
if (value != null)
{
if (Convert.ToBoolean(value) == true)
{
_amount.ShowDialog();
if (_amount._price != _amount._amountPrice)
{
MessageBox.Show("You have entered an amount: Rp " + _amount._price + "\nColumn Number: " + e.ColumnIndex + "\nYou have provided the wrong amount!", "Cannot proceed");
//Uncheck the checkbox
}
else
{
MessageBox.Show("You have entered an amount: Rp " + _amount._price + "\nColumn Number: " + e.ColumnIndex + "\nYou have provided the correct amount!", "Proceeded");
//Check the checkbox
}
}
else
{
MessageBox.Show("Not Checked. \n" + "Column Number: " + e.ColumnIndex + "", "Not Checked");
}
}
}
}
}
以下是Amount
表单代码:
public int _price;
public int _amountPrice = 500000;
public Amount()
{
InitializeComponent();
}
private void Amount_Load(object sender, EventArgs e)
{
this.numericTextBox1.Text = "0";
}
private void button1_Click(object sender, EventArgs e)
{
_price = Convert.ToInt32(this.numericTextBox1.Text);
if (this.numericTextBox1.Text == "0")
{
MessageBox.Show("You cannot leave the amount blank or as zero (0)", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.numericTextBox1.Focus();
}
else
{
this.Hide();
this.Close();
}
}
以下是DataGridView
中的数据库:
每当我检查Amount
中的CheckBox
时,DataGridView
表单就会出现:
我希望当输入的Amount
与已知的Amount
不同时,取消选中该复选框,以及输入的Amount
是否与已知的Amount
相同,复选框将保持选中状态。
目前,只要输入的Amount
与已知的Amount
不同,该复选框仍会保持选中状态。
任何帮助将不胜感激!
谢谢!
答案 0 :(得分:1)
string yourValue="x";
if (dataGridView1.Rows[0].Cells[1].Value.ToString() == yourValue)
{
dataGridView1.Rows[0].Cells[1].Value = true;
//or
//dataGridView1.Rows[0].Cells[3].Value = false;
}
或者当您定义新行时,可以将其设置为true或false
之类的值