WPF的dataGrid基本用法

2021-03-20 23:26

阅读:379

标签:tco   foreach   items   mic   xmlns   data   val   初始   ict   

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace WpfApp1
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        ObservableCollection items = new ObservableCollection();

        public MainWindow()
        {
            InitializeComponent();
            InitData();
        }
        /// 
        /// 初始化
        /// 
        public void InitData()
        {
            for (int i = 0; i 
        /// 增加行
        /// 
        /// 
        /// 
        private void AddData_Click(object sender, RoutedEventArgs e)
        {
            dynamic item = new ExpandoObject();
            item.Aa = "New Item - A";
            item.Bb = "New Item - B";
            items.Add(item);
        }

        int newColumnIndex = 1;
        /// 
        /// 增加列
        /// 
        /// 
        /// 
        private void AddColumn_Click(object sender, RoutedEventArgs e)
        {
            foreach (IDictionary item in items)
            {
                item.Add("NewColumn" + newColumnIndex, "New Column Value - " + newColumnIndex.ToString());
            }

            dataGrid.Columns.Add(new DataGridTextColumn() { Header = "New Column" + newColumnIndex, Binding = new Binding("NewColumn" + newColumnIndex) });

            newColumnIndex++;
        }

        private void showWindow_Click(object sender, RoutedEventArgs e)
        {
            Window1 window1 = new Window1();
            window1.Show();
        }
        /// 
        /// 清除数据表
        /// 
        /// 
        /// 
        private void ClearDataGrid_Click(object sender, RoutedEventArgs e)
        {
            //dataGrid.Items.Clear();
            //dataGrid.ItemsSource = null;
            //dataGrid.Items.Refresh();
            //dataGrid.Items.Clear();
            dataGrid.ItemsSource = null;
            dataGrid.Items.Refresh();
            dataGrid.Columns.Clear();
            //
            
        }

        private void InitData_Click(object sender, RoutedEventArgs e)
        {
            items.Clear();
            InitData();
        }
        //private void Test()
        //{
        //    ThreadPool.QueueUserWorkItem();

        //    Delegate void WaitCallback(object state);
        //}
    }
}

 xaml


            
            
            
            
        

 

WPF的dataGrid基本用法

标签:tco   foreach   items   mic   xmlns   data   val   初始   ict   

原文地址:https://www.cnblogs.com/xuelixue/p/11835248.html


评论


亲,登录后才可以留言!