• DataGridView绑定数据更新


    1、创建数据类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DataGridViewTest
    {
        internal class UserData
        {
            public string Name { get; set; }
            public int Weight { get; set; }
            public int Height { get; set; }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2、DataGridView数据操作

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace DataGridViewTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private List<UserData> dataList = new List<UserData>();
            private Random random = new Random();
            private bool dgv1 = false;
    
            /// 
            /// 绑定数据
            /// 
            /// 
            /// 
            private void btBind_Click(object sender, EventArgs e)
            {
                dataGridView1.DataSource = dataList;
                dataGridView1.Columns["Name"].HeaderText = "姓名";
                dataGridView1.Columns["Weight"].HeaderText = "体重";
                dataGridView1.Columns["Height"].HeaderText = "身高";
                Console.WriteLine("绑定数据");
            }
            /// 
            /// 添加数据
            /// 
            /// 
            /// 
            private void btAdd_Click(object sender, EventArgs e)
            {
                for(int i = 0; i < 10000; i++)
                {
                    UserData userData = new UserData();
                    userData.Name = "胡" + i.ToString();
                    userData.Weight = random.Next(1, 100);
                    userData.Height = random.Next(10, 250);
                    dataList.Add(userData);
                    //dataList.Insert(0, userData);
                }
                //倒序
                dataList.Reverse();
            }
            /// 
            /// 刷新数据
            /// 
            /// 
            /// 
            private void btRefresh_Click(object sender, EventArgs e)
            {
                if (dataList.Count == 0)
                {
                    dataGridView1.DataSource = null;
                    return;
                }
                if (dataGridView1.DataSource != null)
                {
                    if (dataList.Count != dataGridView1.Rows.Count)
                    {
                        dataGridView1.DataSource = null;
                        dataGridView1.DataSource = dataList;
                        dataGridView1.Columns["Name"].HeaderText = "姓名";
                        dataGridView1.Columns["Weight"].HeaderText = "体重";
                        dataGridView1.Columns["Height"].HeaderText = "身高";
                    }
                }
                if (!dgv1)
                {
                    dataGridView1.DataSource = dataList;
                    dgv1 = true;
                }
                dataGridView1.Invalidate();
                //dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);
                //dataGridView1.Rows[0].Selected = false;
                //dataGridView1.Rows[dataGridView1.Rows.Count-1].Selected = true;
            }
            /// 
            /// 修改数据
            /// 
            /// 
            /// 
            private void btUpdate_Click(object sender, EventArgs e)
            {
                if (dataList.Count > 0)
                {
                    dataList[0].Name = "加" + dataList[0].Name;
                    dataList[0].Weight = random.Next(1, 100);
                    dataList[0].Height = random.Next(10, 250);
                }
            }
            /// 
            /// 清除数据
            /// 
            /// 
            /// 
            private void btClear_Click(object sender, EventArgs e)
            {
                dataList.Clear();
                dataGridView1.Rows.Clear();
            }
            //private int count = 0;
            /// 
            /// 直接对DataGridView操作
            /// 
            /// 
            /// 
            private void btRowTest_Click(object sender, EventArgs e)
            {
                if (this.dataGridView1 != null)
                {
                    this.dataGridView1.Columns.Clear();
                    this.dataGridView1.Rows.Clear();
                }
                //添加列名
                this.dataGridView1.Columns.Add("column0", "姓名");
                this.dataGridView1.Columns.Add("column1", "体重");
                this.dataGridView1.Columns.Add("column2", "身高");
                for (int i = 0; i < 10000; i++)
                {
                    //if (this.dataGridView1 != null && this.dataGridView1.Rows.Count > 1000)
                    //{
                    //    this.dataGridView1.Rows.RemoveAt(this.dataGridView1.Rows.Count-2);
                    //}
    
                    //在头部添加
                    DataGridViewRow row = new DataGridViewRow();
                    DataGridViewTextBoxCell name = new DataGridViewTextBoxCell();
                    name.Value = "胡" + (i).ToString();
                    row.Cells.Add(name);
                    DataGridViewTextBoxCell weight = new DataGridViewTextBoxCell();
                    weight.Value = random.Next(1, 100);
                    row.Cells.Add(weight);
                    DataGridViewTextBoxCell height = new DataGridViewTextBoxCell();
                    height.Value = random.Next(1, 100);
                    row.Cells.Add(height);
                    this.dataGridView1.Rows.Insert(0, row);
    
                    //在底部添加
                    //int index = this.dataGridView1.Rows.Add();
                    //this.dataGridView1.Rows[index].Cells[0].Value = "胡" + i.ToString();
                    //this.dataGridView1.Rows[index].Cells[1].Value = random.Next(1, 100);
                    //this.dataGridView1.Rows[index].Cells[2].Value = random.Next(10, 250);
                }
                //count++;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159

    3、运行
    在这里插入图片描述

  • 相关阅读:
    含文档+PPT+源码等]精品基于SSM的民宿预订管理系统[包运行成功]计算机毕设Java项目源码
    LeetCode每日一题(324. Wiggle Sort II)
    Android-Framework 三方应用默认权限都不弹窗
    Nacos集群下使用Nginx访问出现404问题
    Python async模块使用(杂文)
    基于截止至 2023 年 5 月 30 日,在 App Store 上进行交易的设备数据统计,iOS/iPadOS 各版本更新情况
    激光器选型指标
    Docker基础操作容器
    搜索算法(DFS和BFS 蓝桥杯 C++)
    BEVFormer 论文阅读
  • 原文地址:https://blog.csdn.net/qq_27474555/article/details/132742748