dimanche 28 juin 2015

Type of object to cast to when parsing DataGrid.SelectedItems (bound to a MySql DataTable)

I have a simple C# / WPF application that reads a MySql database into a DataGrid.

    <DataGrid  Height="470" Width="800" AutoGenerateColumns="True" CanUserResizeColumns="True" CanUserReorderColumns="True" x:Name="DgrReadWrite" ItemsSource="{Binding ''}"  HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" />

I load the Sql data into it like this:

    string connStr = Service.getConnectionString();

        string sql = "SELECT * FROM adat";
        try
        {
            MySqlConnection connection = new MySqlConnection(connStr);
            MySqlCommand cmdSel = new MySqlCommand(sql, connection);

            MySqlDataAdapter da2 = new MySqlDataAdapter(cmdSel);
            da2.Fill(this.dt2);
            DgrReadWrite.DataContext = dt2;
        }
        catch (Exception ex)
        {
            MessageBox.Show("MySQL kapcsolódási hiba!", "Hiba!", MessageBoxButton.OK, MessageBoxImage.Error);
        }

When I press the delete button, this would happen:

   for (int i = 0; i < this.dt2.Rows.Count; i++)
        {

            // looping through DgrReadWrite.SelectedItems.Cast<SomethingReadable??>
            if (somethingReadable[0] == this.dt2.Rows[i][0].ToString())
            {   
                dt2.Rows[i].Delete();
                DgrReadWrite.Items.Refresh();
            }
        }

My question: what type should I cast the selected items so I can make sense of them in the method? Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire