用户右键点击TABLE控件,弹出一个菜单,选择菜单对应到相关的操作
1、增加一个contextMenuStrip6控件,在里面增加2个ITEM,名称用中文命名
2、给两个ITEM添加点击后的事件
3、在XPTABLE上增加点击事件:
4、当用户右键点击XPTABLE,弹出菜单,用户再点击菜单,完成相应的添加和删除数据:
void InitTableColumn6(){table6.ColumnModel = columnModel6;table6.TableModel = tableModel6;columnModel6.Columns.Add(new TextColumn("ID", 50));columnModel6.Columns.Add(new TextColumn("平台", 50));columnModel6.Columns.Add(new TextColumn("车型中文", 200)); NumberColumn nc = new NumberColumn("数量");nc.Maximum = 10000;columnModel6.Columns.Add(nc);columnModel6.Columns.Add(new DateTimeColumn("开始日期", 120));columnModel6.Columns.Add(new DateTimeColumn("结束日期", 120));}void ShowTableModel6(){tableModel6.Rows.Clear();foreach (var item in ListRulesAssign){XPTable.Models.Row r = new XPTable.Models.Row();tableModel6.Rows.Add(r);r.Cells.Add(new XPTable.Models.Cell(item.id));r.Cells.Add(new XPTable.Models.Cell(item.platform));r.Cells.Add(new XPTable.Models.Cell(item.pm_zh));r.Cells.Add(new XPTable.Models.Cell(item.amount));r.Cells.Add(new XPTable.Models.Cell(item.beginDate));r.Cells.Add(new XPTable.Models.Cell(item.endDate));}}private void table6_CellClick(object sender, XPTable.Events.CellMouseEventArgs e){ //显示菜单if (e.Button == MouseButtons.Right){rightid = tableModel6[e.Row, 0].Text;contextMenuStrip6.Show(System.Windows.Forms.Control.MousePosition);}}private void MenuItem6_1_Click(object sender, EventArgs e){ListRulesAssign.Add(new Rules { id = (ListRulesAssign.Count + 1).ToString(), platform = "F2", pm_zh = "问界 M9 增程 Max", amount = 100, beginDate = new System.DateTime(2024, 6, 19), endDate = new System.DateTime(2024, 7, 3) });ShowTableModel6();}private void MenuItem6_2_Click(object sender, EventArgs e){ListRulesAssign.Remove(ListRulesAssign.Where(p => p.id.Equals(rightid)).FirstOrDefault());ShowTableModel6();}