Answer the question
In order to leave comments, you need to log in
How to update db content from dataGridView?
The essence of my task is that by clicking on any row and when clicking, the program automatically updates the value of the datagridView cell and that this changed table is updated in the MySQL database, but nothing happens.
Here is the code of my table, where I display the table through the dataset:
private void button1_Click(object sender, EventArgs e)
{
string CommandText = "Select " +
"transportation.number as 'номер перевозки', " +
"ticket.Name as 'ФИО пассажира', " +
"route.Route_number as 'номер маршрута', " +
"route.destination as 'место назначения', " +
"ticket.Place as 'Место', " +
"ticket.Cost as 'Стоимость', " +
"driver.Nname as 'ФИО водителя', " +
"transportation.active as 'Активность строки' " +
"From " +
"auto.transportation, " +
"auto.route, " +
"auto.ticket, " +
"auto.driver " +
"Where " +
"(transportation.ID_Route = route.ID_Route) AND " +
"(transportation.id_Ticket = ticket.id_Ticket) AND " +
"(transportation.id_Driver = driver.id_Driver)";
MySqlCommand cmd = new MySqlCommand(CommandText);
if (textBox1.Text != "")
{
if (comboBox1.SelectedIndex == 0)
CommandText = CommandText + " AND (transportation.number = '" + textBox1.Text + "')";
if (comboBox1.SelectedIndex == 1)
CommandText = CommandText + " AND (route.Route_number = '" + textBox1.Text + "')";
if (comboBox1.SelectedIndex == 2)
CommandText = CommandText + " AND (route.destination LIKE '" + textBox1.Text + "%') ";
if (comboBox1.SelectedIndex == 3)
CommandText = CommandText + " AND (ticket.Name LIKE '" + textBox1.Text + "%') ";
if (comboBox1.SelectedIndex == 4)
CommandText = CommandText + " AND (driver.Nname LIKE '" + textBox1.Text + "%') ";
}
adapter = new MySqlDataAdapter(CommandText, conn);
ds = new DataSet();
adapter.Fill(ds, "transportation");
dataGridView1.DataSource = ds.Tables["transportation"].DefaultView;
}
private void button3_Click(object sender, EventArgs e)
{
Form3 f = new Form3();
int row = dataGridView1.CurrentCell.RowIndex;
if (f.ShowDialog() == DialogResult.OK)
{
try
{
MySqlCommand command = new MySqlCommand("Update transportation set active = @active where [email protected]", conn);
command.Parameters.AddWithValue("@active", dataGridView1.Rows[row].Cells[7].Value = "X");
command.Parameters.AddWithValue("@id", dataGridView1.Rows[row].Cells[0].Value);
command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question