简体   繁体   中英

How to specify a cell in parameter in c#

I am trying to find a way to specify a cell in a parameter. I am trying to add " X" to the text from my database in a dataGridView and I am using the following code. the problem in this code is that it adds the " X" to the whole column, and I was wondering if there's a way to choose a cell in a parameter as I want to add the " X" to a certain cell.

    private void table1Button_Click(object sender, EventArgs e)
    {
        string constring = "Data Source = localhost; port = 3306; username = root; password = 0159";
        string Query1 = @"Insert into TopShineDB.Table1 (Timee, CarColorNumber, Interior, Exterior) values(@Timee, @CarColorNumber, @Interior, @Exterior)";
        using (MySqlConnection conn1 = new MySqlConnection(constring))
        using (MySqlCommand command = new MySqlCommand(Query1, conn1))
        {
            conn1.Open();

            command.Parameters.Add("@Timee", MySqlDbType.VarChar);
            command.Parameters.Add("@CarColorNumber", MySqlDbType.VarChar);
            command.Parameters.Add("@Interior", MySqlDbType.VarChar);
            command.Parameters.Add("@Exterior", MySqlDbType.VarChar);

            try
            {

                foreach (DataGridViewRow dr in dataGridView1.Rows)
                {
                    command.Parameters["@Timee"].Value = dr.Cells[0].Value;
                    command.Parameters["@CarColorNumber"].Value = dr.Cells[1].Value;
                    if (dataGridView1.CurrentCell.Style.BackColor == Color.GreenYellow)
                    {
                        command.Parameters["@Interior"].Value = dr.Cells[2].Value + " X";
                    }
                    if (dataGridView1.CurrentCell.Style.BackColor == Color.GreenYellow)
                    {
                        command.Parameters["@Exterior"].Value = dr.Cells[3].Value + " X";
                    }

                    command.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

thanks

firstly as common practice in any programming language I would change:

if (dataGridView1.CurrentCell.Style.BackColor == Color.GreenYellow)
{
    command.Parameters["@Interior"].Value = dr.Cells[2].Value + " X";
}
if (dataGridView1.CurrentCell.Style.BackColor == Color.GreenYellow)
{
    command.Parameters["@Exterior"].Value = dr.Cells[3].Value + " X";
}

to:

if (dataGridView1.CurrentCell.Style.BackColor == Color.GreenYellow)
{
    command.Parameters["@Interior"].Value = dr.Cells[2].Value + " X";
    command.Parameters["@Exterior"].Value = dr.Cells[3].Value + " X";
}

as the two if statements are equivalent. Also, what exactly are you using the BackColor of the cells for? Personally if I wanted to achieve adding an X to cell (2,3) for example I would do:

command.Parameters["@Interior"].Value = this.dataGridView1[2,3].Value + " X";
command.Parameters["@Exterior"].Value = this.dataGridView1[2,3].Value + " X";

instead of iterating through each row and doing multiple conditional statements.

Let me know if this achieves what you want, else I can update for you.

Best of luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM