我正在制作启动列表,所以很明显我想防止竞争对手的起始编号相同。我有输入值的TextBoxes,保存按钮转换为DataGridView表。像这样:
bool CheckFiledIsEmpty()
{
if (textStN.Text == string.Empty ||
textN.Text == string.Empty ||
textSN.Text == string.Empty ||
textC.Text == string.Empty ||
textYB.Text == string.Empty)
return false;
else
return true;
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (CheckFiledIsEmpty())
{
string Column1 = textStN.Text;
string Column2 = textN.Text;
string Column3 = textSN.Text;
string Column4 = textC.Text;
string Column5 = textYB.Text;
string[] row = { Column1, Column2, Column3, Column4, Column5 };
dataGridView1.Rows.Add(row);
textStN.Text = "";
textN.Text = "";
textSN.Text = "";
textC.Text = "";
textYB.Text = "";
}
else
{
MessageBox.Show("Input all information about competitor!");
}
}
我希望在点击保存按钮时,检查Column1
(起始编号文本框textStN
)中的前一个值,如果错误框中有相同的值喜欢:“那个起始号码的竞争对手已经存在!”
感谢您的帮助,对不起我的英语。
答案 0 :(得分:0)
修改您的代码如下:
bool CompetitorAlreadyExist()
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.ColumnIndex == 0) // set your column index
{
// do your stuff here… i.e., compare `textStN.Text` with all values
// in column start # and return true or false
}
}
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (CheckFiledIsEmpty() && CompetitorAlreadyExist())
{
string column1 = textStN.Text;
string column2 = textN.Text;
string column3 = textSN.Text;
string column4 = textC.Text;
string column5 = textYB.Text;
string[] row = { column1, column2, column3, column4, column5 };
dataGridView1.Rows.Add(row);
textStN.Text = "";
textN.Text = "";
textSN.Text = "";
textC.Text = "";
textYB.Text = "";
}
else
{
MessageBox.Show("Input all information about competitor!");
}
}
希望它可能会有所帮助。