• 【C#语言】DataGridView删除行


            在实际项目中,有时需要将数据表中的数据删除,因此需要使用DataGridView控件中删除行的功能。

            控件:

     ContextMenuStrip.

            方法:

    指定的屏幕位置定位 

     public void Show(Point screenLocation);

    指定的控件位置定位

     public void Show(Control control, Point position);

            控件:

    DataGridView.

            方法:

     从集合中移除指定位置处的行。

     public virtual void RemoveAt(int index);

     

     

     

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. namespace Test_DataGridView
    10. {
    11. public partial class Form1 : Form
    12. {
    13. public Form1()
    14. {
    15. InitializeComponent();
    16. }
    17. //全局变量
    18. int rowIndex;
    19. private void Form1_Load(object sender, EventArgs e)
    20. {
    21. dataGridView1.DataSource = Source();
    22. }
    23. //数据表资源
    24. private DataTable Source()
    25. {
    26. DataTable mydt = new DataTable();
    27. mydt.Columns.Add("姓名");
    28. mydt.Columns.Add("年龄");
    29. mydt.Columns.Add("分数");
    30. String[,] str = new String[,] { { "张三", "21", "90" }, { "李四", "22", "93" }, { "王五", "23", "99" } };
    31. for (int i = 0; i < 3; i++)
    32. {
    33. DataRow dr = mydt.NewRow();
    34. dr[0] = str[i, 0];
    35. dr[1] = str[i, 1];
    36. dr[2] = str[i, 2];
    37. mydt.Rows.Add(dr);
    38. }
    39. return mydt;
    40. }
    41. //获取单元格信息
    42. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    43. {
    44. //获取单元格坐标
    45. int col = dataGridView1.CurrentCellAddress.X + 1;
    46. int row = dataGridView1.CurrentCellAddress.Y + 1;
    47. //获取单元格内容
    48. String content = dataGridView1.CurrentCell.Value.ToString();
    49. MessageBox.Show("行:" + row.ToString() + " 列:" + col.ToString() + " 内容:" + content);
    50. }
    51. //隐藏年龄
    52. private void button1_Click(object sender, EventArgs e)
    53. {
    54. String bts = button1.Text;
    55. if (bts == "隐藏年龄")
    56. {
    57. dataGridView1.Columns[1].Visible = false;
    58. bts = "显示年龄";
    59. button1.Text = bts;
    60. }
    61. else
    62. {
    63. dataGridView1.Columns[1].Visible = true;
    64. bts = "隐藏年龄";
    65. button1.Text = bts;
    66. }
    67. }
    68. //右键点击事件
    69. private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
    70. {
    71. if (e.Button == MouseButtons.Right)
    72. {
    73. this.dataGridView1.Rows[e.RowIndex].Selected = true;
    74. this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[0];
    75. this.contextMenuStrip1.Show(this.dataGridView1, e.Location);
    76. contextMenuStrip1.Show(Cursor.Position);
    77. rowIndex = e.RowIndex;
    78. }
    79. }
    80. //删除行事件的内容
    81. private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
    82. {
    83. if (!this.dataGridView1.Rows[this.rowIndex].IsNewRow)
    84. {
    85. this.dataGridView1.Rows.RemoveAt(rowIndex);
    86. }
    87. }
    88. }
    89. }



  • 相关阅读:
    Elasticsearch部署中的两大常见问题及其解决方案
    6.1810: Operating System Engineering <LEC 1>
    3D虚拟数字人定制,推动传统文化传播新高度
    中建三局,宁德时代,金证科技,途游游戏,得物,蓝禾,顺丰,康冠科技24春招内推
    优秀的 Verilog/FPGA开源项目介绍(三十五)- TinyML
    HarmonyOS har制作与引用
    JavaScript 16 JavaScript 对象
    debian设置允许ssh连接
    PIC12F510作为PMBus主机
    哪些有哪些搜索引擎及app的下拉词可以·5月昔年优化新盘点
  • 原文地址:https://blog.csdn.net/NickStudent/article/details/126200188