WPF DataGrid入门示例

一、添加,删除、插入行

<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>
        <DataGrid x:Name="myDataGrid" AutoGenerateColumns="False" CanUserAddRows="False">
            
            <DataGrid.ContextMenu>
                <ContextMenu Name="myDataGridContexMenu" StaysOpen="true" Background="White">
                    <MenuItem Header="添加" Click="AddDataGrid_Click" />
                    <MenuItem Header="删除" Click="DeleteDataGrid_Click" />
                    <MenuItem Header="插入" Click="InsertDataGrid_Click" />
                </ContextMenu>
            </DataGrid.ContextMenu>

            <DataGrid.Columns>
                <DataGridTextColumn Width ="*" Header="姓名"      Binding="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
                <DataGridTextColumn Width ="*" Header="性别"      Binding="{Binding Gender,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
                <DataGridTextColumn Width ="*" Header="年龄"      Binding="{Binding Age,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
前台代码
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<RegisterInformation> registerInformation = new ObservableCollection<RegisterInformation>();
        public MainWindow()
        {
            InitializeComponent();
            myDataGrid.ItemsSource = registerInformation;//绑定源信息
        }
        //删除选中的行
        private void DeleteDataGrid_Click(object sender, RoutedEventArgs e)
        {
            while (myDataGrid.SelectedIndex != -1)
                registerInformation.RemoveAt(myDataGrid.SelectedIndex);          
        }
        //插入行
        private void InsertDataGrid_Click(object sender, RoutedEventArgs e)
        {
            if (myDataGrid.SelectedIndex == -1)
                return;
            registerInformation.Insert(myDataGrid.SelectedIndex, new RegisterInformation { });
            myDataGrid.SelectedIndex = -1;//不选中任何行            
        }
        //添加行
        private void AddDataGrid_Click(object sender, RoutedEventArgs e)
        {
            registerInformation.Add(new RegisterInformation());
        }      
    }

    //登记信息
    public class RegisterInformation
    {
        public string Name { get; set; }//姓名
        public string Gender { get; set; }//性别
        public string Age { get; set; }//年龄
    }
}
后台代码

二、加载、保存并更改行背景色

 

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using Brushes = System.Windows.Media.Brushes;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<RegisterInformation> registerInformation = new ObservableCollection<RegisterInformation>();
        ColorDialog colorDialog = new ColorDialog();
        public MainWindow()
        {
            InitializeComponent();
            myDataGrid.ItemsSource = registerInformation;//绑定源信息
        }
        //删除选中的行
        private void DeleteDataGrid_Click(object sender, RoutedEventArgs e)
        {
            while (myDataGrid.SelectedIndex != -1)
                registerInformation.RemoveAt(myDataGrid.SelectedIndex);          
        }
        //插入行
        private void InsertDataGrid_Click(object sender, RoutedEventArgs e)
        {
            if (myDataGrid.SelectedIndex == -1)
                return;
            registerInformation.Insert(myDataGrid.SelectedIndex, new RegisterInformation { });
            myDataGrid.SelectedIndex = -1;//不选中任何行            
        }
        //添加行
        private void AddDataGrid_Click(object sender, RoutedEventArgs e)
        {
            registerInformation.Add(new RegisterInformation());
        }

        private void SaveDataGrid_Click(object sender, RoutedEventArgs e)
        {
            SaveDocument();
        }
        #region 保存文档
        private void SaveDocument()
        {
            var dlg = new Microsoft.Win32.SaveFileDialog
            {
                FileName = "",
                DefaultExt = ".csv",
                Filter = "CSV Files|*.csv|TSV Files|*.tsv|Text Files|*.txt|All Files|*.*"
            };
            var result = dlg.ShowDialog();
            if (result == true)
            {
                var filename = dlg.FileName;
                FileStream savefs = new FileStream(dlg.FileName, FileMode.Create);
                StreamWriter savesw = new StreamWriter(savefs);
                for (int _rowIndex = 0; _rowIndex < registerInformation.Count; _rowIndex++)
                {
                    savesw.Write(registerInformation[_rowIndex].Name + ",");
                    savesw.Write(registerInformation[_rowIndex].Gender + ",");
                    savesw.Write(registerInformation[_rowIndex].Age + ",");
                    savesw.Write(registerInformation[_rowIndex].RowBackgroud + ",");
                    savesw.Write("
");
                }
                savesw.Flush();
                savesw.Close();
                savefs.Close();
                Title = filename;//显示全路径
            }
        }
        #endregion

        private void LoadDataGrid_Click(object sender, RoutedEventArgs e)
        {
            OpenDocument();
        }

        #region 打开文档

        private SolidColorBrush myConvertFromString(string color)
        {
            SolidColorBrush solidColorBrush;
            if (color != "")
                solidColorBrush = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(color));
            else
                solidColorBrush = Brushes.White;
            return solidColorBrush;

        }
        private void OpenDocument()
        {
            var dlg = new Microsoft.Win32.OpenFileDialog
            {
                FileName = "",
                DefaultExt = ".csv",
                Filter = "CSV Files|*.csv|TSV Files|*.tsv|Text Files|*.txt|All Files|*.*"
            };
            var result = dlg.ShowDialog();
            if (result == true)
            {
                var filename = dlg.FileName;
                FileStream openfs = new FileStream(dlg.FileName, FileMode.Open);
                StreamReader opensw = new StreamReader(openfs);
                String line;
                while ((line = opensw.ReadLine()) != null)
                {
                    string[] strArray = line.Split(',');
                    registerInformation.Add(new RegisterInformation
                    {
                        Name = strArray[0],
                        Gender = strArray[1],
                        Age = strArray[2],
                        RowBackgroud = myConvertFromString(strArray[3])
                    });
                }
                openfs.Close();
            }
        }
        #endregion

        //更改背景色
        private void BackColorDataGrid_Click(object sender, RoutedEventArgs e)
        {
            if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)//调用颜色选择器对话框
            {
                //提取选中的颜色
                SolidBrush sb = new SolidBrush(colorDialog.Color);
                SolidColorBrush solidColorBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(sb.Color.A, sb.Color.R, sb.Color.G, sb.Color.B));

                foreach (var item in myDataGrid.SelectedItems)
                {
                    var selected_item = item as RegisterInformation;
                    selected_item.RowBackgroud = solidColorBrush;
                }
            }
        }
    }

    //登记信息
    public class RegisterInformation : INotifyPropertyChanged
    {
        public string Name { get; set; }//姓名
        public string Gender { get; set; }//性别
        public string Age { get; set; }//年龄

        private SolidColorBrush _backgroud;
        public SolidColorBrush RowBackgroud
        {
            get { return _backgroud; }
            set { _backgroud = value; NotifyPropertyChanged(); }
        }//背景色


        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
后台代码
<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>
        <DataGrid x:Name="myDataGrid" AutoGenerateColumns="False" CanUserAddRows="False">
            
            <DataGrid.ContextMenu>
                <ContextMenu Name="myDataGridContexMenu" StaysOpen="true" Background="White">
                    <MenuItem Header="添加" Click="AddDataGrid_Click" />
                    <MenuItem Header="删除" Click="DeleteDataGrid_Click" />
                    <MenuItem Header="插入" Click="InsertDataGrid_Click" />
                    <MenuItem Header="保存" Click="SaveDataGrid_Click" />
                    <MenuItem Header="加载" Click="LoadDataGrid_Click" />
                    <MenuItem Header="背景色" Click="BackColorDataGrid_Click" />
                    
                </ContextMenu>
            </DataGrid.ContextMenu>

            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="Background" >
                        <Setter.Value>
                            <Binding Path="RowBackgroud" Mode="TwoWay"  UpdateSourceTrigger="PropertyChanged"/>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.RowStyle>

            <DataGrid.Columns>
                <DataGridTextColumn Width ="*" Header="姓名"      Binding="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
                <DataGridTextColumn Width ="*" Header="性别"      Binding="{Binding Gender,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
                <DataGridTextColumn Width ="*" Header="年龄"      Binding="{Binding Age,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
前台代码

 源码下载地址:https://github.com/lizhiqiang0204/WPF_DataGrid

原文地址:https://www.cnblogs.com/lizhiqiang0204/p/14178229.html