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; }
}
}
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;
/// <summary>
/// 绑定数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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("绑定数据");
}
/// <summary>
/// 添加数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// 刷新数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
/// <summary>
/// 修改数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}
}
/// <summary>
/// 清除数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btClear_Click(object sender, EventArgs e)
{
dataList.Clear();
dataGridView1.Rows.Clear();
}
//private int count = 0;
/// <summary>
/// 直接对DataGridView操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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++;
}
}
}
3、运行