0

我正在制作startlist,所以很明显我想防止出现具有相同起始编号的竞争对手。我有用于输入值的文本框,其中保存按钮转换为 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!");
    }
}

我希望当点击SaveColumn1按钮时,检查(start number textbox ) 中的先前值,textStN如果有相同的值,则错误框如下:“具有该起始编号的竞争对手已经存在!”

感谢您的帮助,对不起我的英语。

4

1 回答 1

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!");
    }
}

希望它可能会有所帮助。

于 2013-07-13T18:19:11.567 回答