zoukankan      html  css  js  c++  java
  • DataGridView怎样完成添加、删除、上移、下移一行

      场景
      在Winform中使用DataGridView完成增加一行、删去一行、上移一行、下移一行
      完成
      增加一行
      private void TaskViewEditHelper_OnAddStep(object sender, EventArgs e)
      {
      DataGridViewRow dr = new DataGridViewRow();
      dr.CreateCells(this.dataGridView_Task_ViewEdit);
      dr.Cells[0].Value = "公众号" + this.dataGridView_Task_ViewEdit.Rows.Count;
      dr.Cells[1].Value = "林口";
      dr.Cells[2].Value = "很多编程教程与资源";
      //this.dataGridView_Task_ViewEdit.Rows.Insert(0, dr); //增加的行作为榜首行
      this.dataGridView_Task_ViewEdit.Rows.Add(dr);//增加的行作为末了一行
      }
      删去一行
      private void TaskViewEditHelper_OnRemoveStep(object sender, EventArgs e)
      {
      if (this.dataGridView_Task_ViewEdit.SelectedRows == null || this.dataGridView_Task_ViewEdit.SelectedRows.Count == 0)
      {
      XtraMessageBox.Show("请先挑选删去步,单击榜首列以选中行");
      }
      else
      {
      if (XtraMessageBox.Show("确定要删去选中步吗?") == System.Windows.Forms.DialogResult.OK)
      {
      foreach (DataGridViewRow dr in this.dataGridView_Task_ViewEdit.SelectedRows)
      {
      if (dr.IsNewRow == false)
      {
      //假设不是已提交的行,默许情况下在增加一行数据成功后,DataGridView为新建一行作为新数据的刺进位置
      this.dataGridView_Task_ViewEdit.Rows.Remove(dr);
      }
      }
      }
      }
      }
      上移一行
      private void TaskViewEditHelper_OnUpStep(object sender, EventArgs e)
      {
      if (this.dataGridView_Task_ViewEdit.SelectedRows == null || this.dataGridView_Task_ViewEdit.SelectedRows.Count == 0)
      {
      XtraMessageBox.Show("请先挑选一行,单击榜首列以选中行");
      }
      else
      {
      if (this.dataGridView_Task_ViewEdit.SelectedRows[0].Index <= 0)
      {
      XtraMessageBox.Show("此行已在顶端,不能再上移!");
      }
      else
      {
      //留心:这儿好坏绑定命据情况的上移行
      // 挑选的行号
      int selectedRowIndex = GetSelectedRowIndex(this.dataGridView_Task_ViewEdit);
      if (selectedRowIndex >= 1)
      {
      // 仿制选中的行
      DataGridViewRow newRow = dataGridView_Task_ViewEdit.Rows[selectedRowIndex];
      // 删去选中的行
      dataGridView_Task_ViewEdit.Rows.Remove(dataGridView_Task_ViewEdit.Rows[selectedRowIndex]);
      // 将仿制的行,刺进到选中的上一行位置
      dataGridView_Task_ViewEdit.Rows.Insert(selectedRowIndex - 1, newRow);
      dataGridView_Task_ViewEdit.ClearSelection();
      // 选中开始选中的行
      dataGridView_Task_ViewEdit.Rows[selectedRowIndex - 1].Selected = true;
      }
      }
      }
      }
      注:
      这儿是没绑定命据源情况下的上移一行,增加的一行时经过是上面新增的办法完成的。

      此刻dataGridView的dataSource是为空的。

      此中用到获取选中行的办法:

      private int GetSelectedRowIndex(DataGridView dgv)
      {
      if (dgv.Rows.Count == 0)
      {
      return 0;
      }
      foreach (DataGridViewRow row in dgv.Rows)
      {
      if (row.Selected)
      {
      return row.Index;
      }
      }
      return 0;
      }
      作用
      下移一行
      private void TaskViewEditHelper_OnDownStep(object sender, EventArgs e)
      {
      if (this.dataGridView_Task_ViewEdit.SelectedRows == null || this.dataGridView_Task_ViewEdit.SelectedRows.Count == 0)
      {
      XtraMessageBox.Show("请先挑选一行,单击榜首列以选中行");
      }
      else
      {
      if (this.dataGridView_Task_ViewEdit.SelectedRows[0].Index >= this.dataGridView_Task_ViewEdit.Rows.Count - 1)
      {
      XtraMessageBox.Show("此行已在底端,不能再下移!");
      }
      else
      {
      int selectedRowIndex = GetSelectedRowIndex(this.dataGridView_Task_ViewEdit);
      if (selectedRowIndex < dataGridView_Task_ViewEdit.Rows.Count - 1)
      {
      // 仿制选中的行
      DataGridViewRow newRow = dataGridView_Task_ViewEdit.Rows[selectedRowIndex];
      // 删去选中的行
      dataGridView_Task_ViewEdit.Rows.Remove(dataGridView_Task_ViewEdit.Rows[selectedRowIndex]);
      // 将仿制的行,刺进到选中的下一行位置
      dataGridView_Task_ViewEdit.Rows.Insert(selectedRowIndex + 1, newRow);
      dataGridView_Task_ViewEdit.ClearSelection();
      // 选中开始选中的行
      dataGridView_Task_ViewEdit.Rows[selectedRowIndex + 1].Selected = true;
      }
      }
      }
      }

  • 相关阅读:
    Caffe proto閱讀
    C++ 基本知識回顧
    Caffe 源碼閱讀(二) SyncedMemory.hpp
    Caffe 源碼閱讀(一) Blob.hpp
    Matlab
    python
    MxNet下训练alexnet(一)
    服务器自己用户名下编译gcc
    Using python to process Big Data
    23 October
  • 原文地址:https://www.cnblogs.com/hite/p/12691814.html
Copyright © 2011-2022 走看看