我有一个 DataGridView,它使用面板进行滚动以避免可怕的“跳转到列表开头”DataGridView 刷新问题。
我正在寻找的是一个标题行,它在垂直滚动期间保持可见并与水平滚动一起移动。
我在 Visual Studio 2012 中测试了这段代码,所以它应该是开箱即用的。
Form1.designer.cs
namespace DataGridViewPanel
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// panel1
//
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.panel1.AutoScroll = true;
this.panel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.panel1.Controls.Add(this.dataGridView1);
this.panel1.Location = new System.Drawing.Point(12, 73);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(260, 177);
this.panel1.TabIndex = 0;
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(0, 0);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.ScrollBars = System.Windows.Forms.ScrollBars.None;
this.dataGridView1.Size = new System.Drawing.Size(257, 174);
this.dataGridView1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(13, 41);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Clear";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(197, 41);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "Add Row";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
表格.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DataGridViewPanel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
fillDataGrid();
}
private void fillDataGrid()
{
dataBase Db = new dataBase();
List<dataBase> DbList = new List<dataBase>();
for (int i = 0; i < 10; i++)
{
DbList.Add(Db);
}
dataGridView1.DataSource = DbList;
int width = 0;
int height = 0;
foreach (DataGridViewColumn col in dataGridView1.Columns) width += col.Width;
foreach (DataGridViewRow row in dataGridView1.Rows) height += row.Height;
dataGridView1.Size = new Size(width, height);
}
private void button1_Click(object sender, EventArgs e)
{
dataBase Db = new dataBase();
List<dataBase> DbList = new List<dataBase>();
DbList.Add(Db);
dataGridView1.DataSource = DbList;
int width = 0;
int height = 0;
foreach (DataGridViewColumn col in dataGridView1.Columns) width += col.Width;
foreach (DataGridViewRow row in dataGridView1.Rows) height += row.Height;
dataGridView1.Size = new Size(width, height);
}
private void button2_Click(object sender, EventArgs e)
{
dataBase Db = new dataBase();
List<dataBase> DbList2 = new List<dataBase>();
DbList2 = (List<dataBase>)dataGridView1.DataSource;
dataGridView1.DataSource = null;
DbList2.Add(Db);
dataGridView1.DataSource = DbList2;
int width = 0;
int height = 0;
foreach (DataGridViewColumn col in dataGridView1.Columns) width += col.Width;
foreach (DataGridViewRow row in dataGridView1.Rows) height += row.Height;
dataGridView1.Size = new Size(width, height);
}
}
public class dataBase : IEnumerable<string>
{
public string one { get; set; }
public string two { get; set; }
public string three { get; set; }
public string four { get; set; }
public string five { get; set; }
public string six { get; set; }
public string seven { get; set; }
public string eight { get; set; }
public string nine { get; set; }
public string ten { get; set; }
public IEnumerator<string> GetEnumerator()
{
yield return one;
yield return two;
yield return three;
yield return four;
yield return five;
yield return six;
yield return seven;
yield return eight;
yield return nine;
yield return ten;
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}