DataGrid binding with mysql in WPF

DataGrid binding with mysql in WPF_第1张图片

 

References:

https://www.c-sharpcorner.com/UploadFile/47548d/data-binding-in-wpf-datagrid-control-using-mysql-database/

Related github is below:

https://github.com/LeoMingGit/WPF_UI_Design/tree/master/DataGridBind

App.config



  
    
  
     
        
    

MainWindow.xaml


    
        
        
            
                
                    
                    
                    
                    
                    
                
            
            

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
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 namespaces 
using System.Data;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace DataGridBind
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        #region MySqlConnection Connection
        MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);      
        public MainWindow()
        {
            InitializeComponent();
        }
        #endregion
        #region bind data to DataGrid.
        private void btnloaddata_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand("Select CustomerID,ContactName,Address,City,Phone,Email from customers", conn);
                MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adp.Fill(ds, "LoadDataBinding");
                dataGridCustomers.DataContext = ds;
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion
    }
}

 

你可能感兴趣的:(WPF学习)