A
A
al_exan_der2019-08-13 11:28:47
MySQL
al_exan_der, 2019-08-13 11:28:47

How to load data from mysql into DataGridComboBoxColumn wpf?

I am writing a program for loading and further editing with sending data back to a remote mysql server, and everything would be fine, but I need to make a drop-down list from different tables in a couple of columns. Those. datagrid is one main table, and for the three combobox columns (correct structure is shown in the commented query, but the query itself is in doubt) other tables in the same database. I have been working with WPF recently and have not yet found a sensible answer on how to make this task feasible, maybe someone will tell / show / write how it should work.

program code. which needs to be redone

using System;
using System.Windows;
using System.Windows.Controls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using MySql.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MySql.Data.MySqlClient;
 
 
namespace conntoSQLServer
{
   
    public partial class MainWindow : Window
    {
        string connectionString;
        MySqlDataAdapter adapter;
        DataTable phonesTable;
 
        
 
        public MainWindow()
        {
            
            InitializeComponent();
 
        }
 
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
           string sql = "SELECT id, ask, offer, status, name, company, phone, ctime, email, cdate FROM orders";
         /*   string sql = "SELECT o.id, o.ask, o.offer, o.status, o.name, o.company, o.phone, o.ctime, o.email, o.cdate, c.name as city, cl.name as class, rc.name as rclass FROM orders as o INNER JOIN cities as c ON (o.city = c.id) INNER JOIN classes AS cl ON (o.class=cl.id) INNER JOIN rclasses as rc ON (o.rclass=rc.id)"; */
           
            //  string sql2 = "UPDATE orders SET ask = @ask, offer = offer";
            phonesTable = new DataTable();
 
           MySqlConnection connection = null;
            try
            {
                connection = new MySqlConnection(connectionString);
                MySqlCommand command = new MySqlCommand(sql, connection);
                adapter = new MySqlDataAdapter(command);
                
                adapter.Fill(phonesTable);
                phonesGrid.ItemsSource = phonesTable.DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (connection != null)
                    connection.Close();
            }
 
        }
 
        private void UpdateDB()
        {
            try
            {
                MySqlCommandBuilder comandbuilder = new MySqlCommandBuilder(adapter);
                adapter.Update(phonesTable);
                
            }
 
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }
 
        private void updateButton_Click(object sender, RoutedEventArgs e)
        {
            UpdateDB();
        }
 
        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (phonesGrid.SelectedItems != null)
            {
                for (int i = 0; i < phonesGrid.SelectedItems.Count; i++)
                {
                    DataRowView datarowView = phonesGrid.SelectedItems[i] as DataRowView;
                    if (datarowView != null)
                    {
                        DataRow dataRow = (DataRow)datarowView.Row;
                        dataRow.Delete();
                    }
                }
            }
            UpdateDB();
        }
    }
 
}

xml

<DataGrid AutoGenerateColumns="False" x:Name="phonesGrid">
            <DataGrid.Columns>
 
                <DataGridTextColumn Header="Спрос" Binding="{Binding ask}" FontFamily="Arial" Width="120"/>
                <DataGridTextColumn Header="Предложение" Binding="{Binding offer}" Width="140"/>
                <DataGridTextColumn Header="Статус" Binding="{Binding status}" Width="100"/>
                <DataGridTextColumn Header="Имя-Отчество" Binding="{Binding name}" Width="140"/>
                <DataGridTextColumn Header="Компания" Binding="{Binding company}" Width="120"/>
                <DataGridTextColumn Header="Город" Binding="{Binding city}" Width="100"/>
                <DataGridTextColumn Header="Род дей-сти" Binding="{Binding class}" Width="120"/>
                <DataGridComboBoxColumn Header="Р" ItemsSource="{Binding rclass}" SelectedItemBinding="{Binding ask}"  Width="50"/>
                <DataGridTextColumn Header="Телефон" Binding="{Binding phone}" Width="120"/>
                <DataGridTextColumn Header="Время" Binding="{Binding ctime}" Width="110"/>
                <DataGridTextColumn Header="Емейл" Binding="{Binding email}" Width="140"/>
 
            </DataGrid.Columns>
        </DataGrid>
 
        <StackPanel HorizontalAlignment="Center"  Grid.Row="1" Orientation="Horizontal">
            <Button x:Name="updateButton" Content="Обновить" Click="updateButton_Click" />
            <Button x:Name="deleteButton" Content="Удалить" Click="deleteButton_Click" />
        </StackPanel>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question