#c# #scroll #datagridview #panel #header-row
#c# #прокрутка #datagridview #панель #строка заголовка
Вопрос:
У меня есть 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 amp;amp; (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;
}
}
Form.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();
}
}
}
Комментарии:
1. Вероятно, нам следует сосредоточиться на:
"Jump to beginning of list" DataGridView refresh issue.
2. Измените источник данных, и нет проблемы «страшного перехода к началу». Если вы удалите источник данных и предоставите новый, вы начинаете все сначала
3. Мой источник данных — это список <> Если я добавлю строку для обновления списка, никаких изменений в DataGridVeiw1 @Plutonix не произойдет
4. BindingList<> был бы лучшим выбором, чем список <>
5. BindingList работает хорошо. Я включил пример.
Ответ №1:
Я добился работоспособного решения с перекрывающейся панелью. Я создал вторую панель и DataGridView. Я заполняю вторую сетку данных заголовком. panel1.Scroll обрабатывается при горизонтальной прокрутке для перемещения прокрутки panel2. Поскольку panel1 перекрывает полосу прокрутки panel2, последний panel1.BringToFront() в обработчике прокрутки гарантирует, что panel1 скрывает полосы прокрутки panel2.
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 amp;amp; (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.panel2 = new System.Windows.Forms.Panel();
this.dataGridView2 = new System.Windows.Forms.DataGridView();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).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(13, 107);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(260, 207);
this.panel1.TabIndex = 0;
this.panel1.Scroll = panel1_Scroll;
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.ColumnHeadersVisible = false;
this.dataGridView1.Location = new System.Drawing.Point(0, 0);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.ReadOnly = true;
this.dataGridView1.ScrollBars = System.Windows.Forms.ScrollBars.None;
this.dataGridView1.Size = new System.Drawing.Size(257, 204);
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);
//
// panel2
//
this.panel2.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.panel2.AutoScroll = true;
this.panel2.Controls.Add(this.dataGridView2);
this.panel2.Location = new System.Drawing.Point(13, 84);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(260, 42);
this.panel2.TabIndex = 1;
//
// dataGridView2
//
this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView2.Location = new System.Drawing.Point(0, 0);
this.dataGridView2.Name = "dataGridView2";
this.dataGridView2.Size = new System.Drawing.Size(257, 40);
this.dataGridView2.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 326);
this.Controls.Add(this.panel2);
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.panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).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;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.DataGridView dataGridView2;
}
}
Form1.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
{
private dataBase Db = new dataBase();
private List<dataBase> DbList = new List<dataBase>();
private List<dataBase> headList = new List<dataBase>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
fillDataGrid();
}
private void fillDataGrid()
{
//add ten rows to the List
for (int i = 0; i < 10; i )
{
DbList.Add(Db);
}
//add one row to the List to populate headers
headList.Add(Db);
//populate datagrid by setting datasource
dataGridView1.DataSource = DbList;
dataGridView2.DataSource = headList;
resizeDataGrids();
}
private void resizeDataGrids()
{
//resize the datagrids so the panels enable the scrollbars when required
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);
width = 0;
height = 0;
foreach (DataGridViewColumn col in dataGridView2.Columns) width = col.Width;
foreach (DataGridViewRow row in dataGridView2.Rows) height = row.Height;
dataGridView2.Size = new Size(width, height);
//bring panel1 to front
panel1.BringToFront();
}
private void button1_Click(object sender, EventArgs e)
{
//populate datagridview1 with an empty List
DbList = new List<dataBase>();
DbList.Add(Db);
dataGridView1.DataSource = DbList;
resizeDataGrids();
}
private void button2_Click(object sender, EventArgs e)
{
//populate datagridview1 with List after adding a new row to the List
DbList = (List<dataBase>)dataGridView1.DataSource;
dataGridView1.DataSource = null;
DbList.Add(Db);
dataGridView1.DataSource = DbList;
resizeDataGrids();
}
void panel1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
{
//Handle horizontal scroll
if (e.ScrollOrientation == System.Windows.Forms.ScrollOrientation.HorizontalScroll)
{
panel2.HorizontalScroll.Value = panel1.HorizontalScroll.Value;
panel1.BringToFront();
}
}
}
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();
}
}
}
Ответ №2:
Как было предложено @Plutonix, я попытался обновить только источник данных.
Используя пару циклов for, я заполняю набор данных, подключенный к источнику данных dataGridView1. Мне это нравится, потому что это устраняет панель.
Я все еще мог использовать список<>!
Спасибо @Plutonix!
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 amp;amp; (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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// 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);
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(13, 102);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(259, 212);
this.dataGridView1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 326);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load = new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.DataGridView dataGridView1;
}
}
Form1.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
{
private dataBase Db = new dataBase();
private List<dataBase> DbList = new List<dataBase>();
private DataSet Ds = new DataSet();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//createColumns();
createColumns();
fillDataGrid();
//populate datagrid by setting datasource
dataGridView1.DataSource = Ds.Tables[0];
}
private void AddRow()
{
dataBase db = new dataBase();
db.one = DateTime.Now.Day.ToString();
db.two = DateTime.Now.Month.ToString();
db.three = DateTime.Now.Year.ToString();
db.four = DateTime.Now.Hour.ToString();
db.five = DateTime.Now.Minute.ToString();
db.six = DateTime.Now.Second.ToString();
db.seven = DateTime.Now.Millisecond.ToString();
DbList.Add(db);
fillDataGrid();
}
private void fillDataGrid()
{
for (int i = DbList.Count - 1; i > Ds.Tables[0].Rows.Count - 1; i--)
{
Ds.Tables[0].Rows.Add();
int loop = 0;
foreach (string valueStr in DbList[i])
{
int end = Ds.Tables[0].Rows.Count - 1;
Ds.Tables[0].Rows[end][loop] = valueStr;
loop ;
}
}
}
private void createColumns()
{
this.Invoke(new MethodInvoker(delegate
{
Ds.Tables.Add(new DataTable());
Ds.Tables[0].Columns.Add("one");
Ds.Tables[0].Columns.Add("two");
Ds.Tables[0].Columns.Add("three");
Ds.Tables[0].Columns.Add("four");
Ds.Tables[0].Columns.Add("five");
Ds.Tables[0].Columns.Add("six");
Ds.Tables[0].Columns.Add("seven");
Ds.Tables[0].Columns.Add("eight");
Ds.Tables[0].Columns.Add("nine");
Ds.Tables[0].Columns.Add("ten");
}));
}
private void button1_Click(object sender, EventArgs e)
{
//populate datagridview1 with an empty List
DbList = new List<dataBase>();
Ds.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
//populate datagridview1 with List after adding a new row to the List
//Ds.Tables[0].Rows.Add(Db);
AddRow();
}
}
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();
}
}
}
Ответ №3:
Решение с помощью BindingList, предложенное @LarsTech. Это очень просто по сравнению со списком<>/DataSet/DataGridView .
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 amp;amp; (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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.button3 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// 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";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click = new System.EventHandler(this.button2_Click);
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(13, 102);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(259, 212);
this.dataGridView1.TabIndex = 3;
//
// button3
//
this.button3.Location = new System.Drawing.Point(104, 41);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 4;
this.button3.Text = "Remove";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click = new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 326);
this.Controls.Add(this.button3);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load = new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button button3;
}
}
Form1.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
{
private dataBase Db = new dataBase();
private BindingList<dataBase> DbList = new BindingList<dataBase>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//populate datagrid by setting datasource
dataGridView1.DataSource = DbList;
}
private void AddRow()
{
//create a new set of data
dataBase db = new dataBase();
db.one = DateTime.Now.Day.ToString();
db.two = DateTime.Now.Month.ToString();
db.three = DateTime.Now.Year.ToString();
db.four = DateTime.Now.Hour.ToString();
db.five = DateTime.Now.Minute.ToString();
db.six = DateTime.Now.Second.ToString();
db.seven = DateTime.Now.Millisecond.ToString();
//add data to the list
DbList.Add(db);
}
private void removeRow()
{
//check if cells are selected
if (dataGridView1.SelectedCells.Count > 0)
{
//loop while rows are selected
while (dataGridView1.SelectedCells.Count > 0)
{
//remove selecteed cell 0
DbList.RemoveAt(dataGridView1.SelectedCells[0].RowIndex);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
//populate datagridview1 with an empty List
DbList.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
//add a new row to the List
AddRow();
}
private void button3_Click(object sender, EventArgs e)
{
//remove a row from the list
removeRow();
}
}
//custom class with string enumeration
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();
}
}
}