先把代码贴上,后面再编辑
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace NoDataSourceDataGridView
{
public partial class Form1 : Form
{
private const int TXTWIDTH = 150;
private const int TXTHEIGHT = 20;
private int selectIndex;
private string[] row0 = { "11/22/1968", "29", "Revolution 9",
"Beatles", "The Beatles [White Album]" };
private string[] row1 = { "1960", "6", "Fools Rush In",
"Frank Sinatra", "Nice 'N' Easy" };
private string[] row2 = { "11/11/1971", "1", "One of These Days",
"Pink Floyd", "Meddle" };
private string[] row3 = { "1988", "7", "Where Is My Mind?",
"Pixies", "Surfer Rosa" };
private string[] row4 = { "5/1981", "9", "Can't Find My Mind",
"Cramps", "Psychedelic Jungle" };
private string[] row5 = { "6/10/2003", "13",
"Scatterbrain. (As Dead As Leaves.)",
"Radiohead", "Hail to the Thief" };
private string[] row6 = { "6/30/1992", "3", "Dress", "P J Harvey", "Dry" };
private List<string[]> list = new List<string[]>();
private void InitList()
{
list.Add(row0);
list.Add(row1);
list.Add(row2);
list.Add(row3);
list.Add(row4);
list.Add(row5);
list.Add(row6);
}
public Form1()
{
InitializeComponent();
InitList();
}
private Panel buttonPanel = new Panel();
private DataGridView songsDataGridView = new DataGridView();
private Button addNewRowButton = new Button();
private Button deleteRowButton = new Button();
private Panel textboxPanel = new Panel();
private TextBox textbox_ReleaseDate = new TextBox();
private TextBox textbox_Track = new TextBox();
private TextBox textbox_Title = new TextBox();
private TextBox textbox_Artist = new TextBox();
private TextBox textbox_Album = new TextBox();
private void SetupTextbox()
{
textbox_ReleaseDate.Width = TXTWIDTH;
textbox_Track.Width = TXTWIDTH;
textbox_Title.Width = TXTWIDTH;
textbox_Artist.Width = TXTWIDTH;
textbox_Album.Width = TXTWIDTH;
textbox_ReleaseDate.Height = TXTHEIGHT;
textbox_Track.Height = TXTHEIGHT;
textbox_Title.Height = TXTHEIGHT;
textbox_Artist.Height = TXTHEIGHT;
textbox_Album.Height = TXTHEIGHT;
textbox_ReleaseDate.Location = new Point(8, 0);
textbox_Track.Location = new Point(8, 25);
textbox_Title.Location = new Point(8, 50);
textbox_Artist.Location = new Point(8, 75);
textbox_Album.Location = new Point(8, 100);
textboxPanel.Height = 300;
textboxPanel.Location = new Point(8,265);
textboxPanel.Controls.Add(textbox_ReleaseDate);
textboxPanel.Controls.Add(textbox_Track);
textboxPanel.Controls.Add(textbox_Title);
textboxPanel.Controls.Add(textbox_Artist);
textboxPanel.Controls.Add(textbox_Album);
this.Controls.Add(this.textboxPanel);
}
private void Form1_Load(object sender, EventArgs e)
{
SetupLayout();
SetupDataGridView();
PopulateDataGridView();
SetupTextbox();
}
private void SetupLayout()
{
this.Size = new Size(600, 600);
addNewRowButton.Text = "Add Row";
addNewRowButton.Location = new Point(10, 10);
addNewRowButton.Click += new EventHandler(addNewRowButton_Click);
deleteRowButton.Text = "Delete Row";
deleteRowButton.Location = new Point(100,10);
deleteRowButton.Click += new EventHandler(deleteRowButton_Click);
buttonPanel.Controls.Add(addNewRowButton);
buttonPanel.Controls.Add(deleteRowButton);
buttonPanel.Height = 50;
buttonPanel.Dock = DockStyle.Bottom;
this.Controls.Add(this.buttonPanel);
}
private void addNewRowButton_Click(object sender, EventArgs e)
{
//this.songsDataGridView.Rows.Add();
string[] temp = new string[5];
temp[0] = textbox_ReleaseDate.Text.ToString();
temp[1] = textbox_Track.Text.ToString();
temp[2] = textbox_Title.Text.ToString();
temp[3] = textbox_Artist.Text.ToString();
temp[4] = textbox_Album.Text.ToString();
list.Add(temp);
songsDataGridView.Rows.Clear();
PopulateDataGridView();
}
private void deleteRowButton_Click(object sender, EventArgs e)
{
if (this.songsDataGridView.Rows.Count > 0)
{
list.RemoveAt(this.songsDataGridView.SelectedRows[0].Index);
this.songsDataGridView.Rows.RemoveAt(this.songsDataGridView.SelectedRows[0].Index);
}
}
private void SetupDataGridView()
{
this.Controls.Add(songsDataGridView);
songsDataGridView.ColumnCount = 5;
songsDataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
songsDataGridView.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
songsDataGridView.ColumnHeadersDefaultCellStyle.Font = new Font(songsDataGridView.Font, FontStyle.Bold);
songsDataGridView.Name = "songsDataGridView";
songsDataGridView.Location = new Point(8, 8);
songsDataGridView.Size = new Size(500, 250);
songsDataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
songsDataGridView.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
songsDataGridView.GridColor = Color.Black;
songsDataGridView.RowHeadersVisible = false;
songsDataGridView.Columns[0].Name = "Release Date";
songsDataGridView.Columns[1].Name = "Track";
songsDataGridView.Columns[2].Name = "Title";
songsDataGridView.Columns[3].Name = "Artist";
songsDataGridView.Columns[4].Name = "Album";
songsDataGridView.Columns[4].DefaultCellStyle.Font =
new Font(songsDataGridView.DefaultCellStyle.Font, FontStyle.Italic);
songsDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
songsDataGridView.MultiSelect = false;
//songsDataGridView.Dock = DockStyle.Fill;
songsDataGridView.CellFormatting += new DataGridViewCellFormattingEventHandler(songsDataGridView_CellFormatting);
songsDataGridView.CellMouseDown += new DataGridViewCellMouseEventHandler(this.songsDataGridView_CellMouseDown);
songsDataGridView.AllowUserToAddRows = false;
}
private void songsDataGridView_CellFormatting(object sender,
System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
if (e != null)
{
if (this.songsDataGridView.Columns[e.ColumnIndex].Name == "Release Date")
{
if (e.Value != null)
{
try
{
e.Value = DateTime.Parse(e.Value.ToString()).ToLongDateString();
e.FormattingApplied = true;
}catch(FormatException)
{
Console.WriteLine("{0} is not a valid date", e.Value.ToString());
}
}
}
}
}
private void PopulateDataGridView()
{
foreach (string[] row in list)
{
songsDataGridView.Rows.Add(row);
}
songsDataGridView.Columns[0].DisplayIndex = 3;
songsDataGridView.Columns[1].DisplayIndex = 4;
songsDataGridView.Columns[2].DisplayIndex = 0;
songsDataGridView.Columns[3].DisplayIndex = 1;
songsDataGridView.Columns[4].DisplayIndex = 2;
}
private void songsDataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex > -1)
{
if (e.Button == MouseButtons.Right)
{
ContextMenuStrip menu = new ContextMenuStrip();
ToolStripMenuItem itemEdit = new ToolStripMenuItem("Edit");
itemEdit.Click += new EventHandler(itemEdit_Click);
ToolStripMenuItem itemDelete = new ToolStripMenuItem("Delete");
itemDelete.Click += new EventHandler(itemDelete_Click);
menu.Items.Add(itemEdit);
menu.Items.Add(itemDelete);
songsDataGridView.ContextMenuStrip = menu;
selectIndex = e.RowIndex;
}
}
}
private void itemEdit_Click(object sender, EventArgs e)
{
string[] temp = list.ElementAt(selectIndex);
textbox_ReleaseDate.Text = temp[0];
textbox_Track.Text = temp[1];
textbox_Title.Text = temp[2];
textbox_Artist.Text = temp[3];
textbox_Album.Text = temp[4];
}
private void itemDelete_Click(object sender, EventArgs e)
{
list.RemoveAt(selectIndex);
songsDataGridView.Rows.Clear();
PopulateDataGridView();
}
}
}