zoukankan      html  css  js  c++  java
  • WPF的dataGrid基本用法

    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
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            ObservableCollection<ExpandoObject> items = new ObservableCollection<ExpandoObject>();
    
            public MainWindow()
            {
                InitializeComponent();
                InitData();
            }
            /// <summary>
            /// 初始化
            /// </summary>
            public void InitData()
            {
                for (int i = 0; i < 5; i++)
                {
                    
                    dynamic item = new ExpandoObject();
                    item.Aa = "Property A value - " + i.ToString();
                    item.Bb = "Property B value - " + i.ToString();
                    items.Add(item);
                }
                
                dataGrid.Columns.Add(new DataGridTextColumn() { Header = "Aa", Binding = new Binding("Aa") });
                dataGrid.Columns.Add(new DataGridTextColumn() { Header = "Bb", Binding = new Binding("Bb") });
                dataGrid.ItemsSource = items;
            }
            /// <summary>
            /// 增加行
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            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;
            /// <summary>
            /// 增加列
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void AddColumn_Click(object sender, RoutedEventArgs e)
            {
                foreach (IDictionary<String, Object> 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();
            }
            /// <summary>
            /// 清除数据表
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            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

    <Window x:Class="WpfApp1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <StackPanel Orientation="Horizontal">
                <Button Content="Add Column" Click="AddColumn_Click" Margin="5"/>
                <Button Content="Add Data" Click="AddData_Click" Margin="5"/>
                <Button Content="Show Window" Click="showWindow_Click" Margin="5"></Button>
                <Button Content="清除内容" Click="ClearDataGrid_Click" Margin="5"></Button>
                <Button Content="初始化"   Click="InitData_Click" Margin="5"></Button>
            </StackPanel>
            <DataGrid x:Name="dataGrid" AutoGenerateColumns="True" Grid.Row="1"/>
        </Grid>
    </Window>
    
  • 相关阅读:
    SQL GUID和自增列做主键的优缺点
    php 一维数组去重
    php + crontab 执行定时任务
    PHP内置函数生成随机数的方法汇总
    PHP替换回车换行的三种方法
    Yii2查询之where条件拼装
    yii2 使用阿里大鱼短信
    javascript对数据处理
    Vue 404页面处理
    vue 中view层中方法的使用
  • 原文地址:https://www.cnblogs.com/xuelixue/p/11835248.html
Copyright © 2011-2022 走看看