Delete row from d atagridview by right click

大发捕鱼 http://www.cnmpsolar 评论

using System;using System.Data;using System.Drawing;using System.Windows.Forms;namespace WindowsFormsApplication1{public partial class Form1 : Form{private int rowIndex = 0;public Form1(){InitializeComponent();}private void Form1_Load(ob

using System; using System.Data; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { private int rowIndex = 0; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(int)); dt.Columns.Add("Publisher Name", typeof(string)); dt.Columns.Add("Book", typeof(string)); for (int i = 1; i < 11; i++) { dt.Rows.Add(i, "PubName" + i, "Book"+i); } dataGridView1.DataSource = dt; this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque; this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige; } private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) { if (e.Button == MouseButtons.Right) { this.dataGridView1.Rows[e.RowIndex].Selected = true; this.rowIndex = e.RowIndex; this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[1]; this.contextMenuStrip1.Show(this.dataGridView1, e.Location); contextMenuStrip1.Show(Cursor.Position); } } private void contextMenuStrip1_Click(object sender, EventArgs e) { if (!this.dataGridView1.Rows[this.rowIndex].IsNewRow) { this.dataGridView1.Rows.RemoveAt(this.rowIndex); } } } }

Delete row from d atagridview by right click

大发捕鱼_黑桃K平台:Delete row from d atagridview by right click

喜欢 (0) or 分享 (0)
发表我的评论
取消评论

表情

您的回复是我们的动力!

  • 昵称 (必填)

网友最新评论