0

Im working on a Project right now I did the same thing in WinForms a couple months back, so im pretty new to WPF and now I need some help.

I've been struggeling the past 2 Weeks with a multiple row insert from a DataGrid in WPF to my MySql Database.

I tried a couple solutions from online but non of them worked. Since WPF Datagrids dont have a "Rows" feature (I think, correct me if im wrong).

My Code in WinForms

foreach (DataGridViewRow dr in dataGridView1.Rows)
{
       try
       {
           string query = "INSERT INTO database VALUES (@b, @sn, @mac, @fb, @bf, @o, @be, @date)";
           MySqlCommand cmd = new MySqlCommand(query, con);
           cmd.Parameters.AddWithValue("@b", dr.Cells[2].Value ?? DBNull.Value);
           cmd.Parameters.AddWithValue("@sn", dr.Cells[3].Value ?? DBNull.Value);
           cmd.Parameters.AddWithValue("@mac", dr.Cells[4].Value ?? DBNull.Value);
           cmd.Parameters.AddWithValue("@fb", dr.Cells[5].Value ?? DBNull.Value);
           cmd.Parameters.AddWithValue("@bf", dr.Cells[6].Value ?? DBNull.Value);
           cmd.Parameters.AddWithValue("@o", dr.Cells[7].Value ?? DBNull.Value);
           cmd.Parameters.AddWithValue("@be", dr.Cells[8].Value ?? DBNull.Value);
           cmd.Parameters.AddWithValue("@date", dateTimePicker1.Text);
           con.Open();
           cmd.ExecuteNonQuery();
           con.Close();
       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
       }
}

I would Like to get some help or solutions on how to do the same thing in WPF since I've been struggling for the past 2 - 3 weeks.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • Did you already checked https://stackoverflow.com/questions/15686381/wpf-iterate-through-datagrid ? – Fruchtzwerg Apr 15 '19 at 08:10
  • Thanks for the fast reply, I will check and let you know. – MrCode00 Apr 15 '19 at 08:30
  • So I tried the Solution you Posted, its kinda works but the main problem is with that solution I get a message box after another that shows me the values of the cells starting from 0 - 10 for instance. But how to I insert that data to mysql now? – MrCode00 Apr 15 '19 at 09:05
  • You need to implement your own code to the linked solution. – Fruchtzwerg Apr 15 '19 at 09:25

0 Answers0