登山-C#-DataGridView在不使用数据库时,实现增加删除操作,在Row上右击可以编辑或删除
来源:互联网 发布:ipad怎么上淘宝卖家版 编辑:程序博客网 时间:2024/06/05 08:39
先把代码贴上,后面再编辑
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(); } }}
0 0
- 登山-C#-DataGridView在不使用数据库时,实现增加删除操作,在Row上右击可以编辑或删除
- 在DataGridView里实现删除操作
- 在不涉及数据库的情况下,对DataGridView中的数据进行删除操作
- 数据库增加列或删除列操作
- 给datagridview增加编辑,删除列
- DataGrid的使用:(一)、在DataGrid控件中实现基本的操作(编辑、删除、分页)
- DataGrid的使用:(一)在DataGrid控件中实现基本的操作(编辑、删除、分页)
- IOS 图片的编辑(可删除或从相册添加图片),主要是可以在初始化时添加图片
- 使用OTL对oracle数据库进行增加,删除,编辑和查询操作。
- 【数据库】数据库增加列或删除列操作
- 数据库增加或删除一列
- 编辑表格@tableView实现增加、删除、移动的操作
- csdn 在c++ builder中,使用treeview实现对xml文档增加删除修改和查询等基本操作
- 在siverlight 的datagrid控件中实现每一行数据后增加一列(操作),此列含编辑、删除、详细等操作
- Oracle 不使用DBCA在Oracle 11中删除数据库
- 动态的在页面上增加或删除一个文本框
- 动态的在页面上增加或删除一个文本框
- 如何在windows环境下删除或增加服务!
- Another A+B
- 在Ubuntu下编程出现的Q&A
- Linux命令学习手册-arp命令
- PAT- Hello World for U (20)
- 默认参数的构造函数
- 登山-C#-DataGridView在不使用数据库时,实现增加删除操作,在Row上右击可以编辑或删除
- 获取select下面选中的value值和文本值。--http://blog.163.com/yiba_suanzao/blog/static/130557377201038113543135/
- 利用100行代码动态创建并部署流程
- Laravel框架学习(Request请求数据、Cookie、文件上传)
- NGUI相关注意事项
- TP框架微信支付目录
- 虚拟机和主机ping不通解决办法
- NSTimer 和runloop的问题,将timer加入到runloop----同时注意在viewwilldisappear里面设置为空和无效
- css中伪元素:before :after的作用