日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

DataGridView怎样实现添加、删除、上移、下移一行

發布時間:2025/3/19 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DataGridView怎样实现添加、删除、上移、下移一行 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景

在Winform中使用DataGridView實現添加一行、刪除一行、上移一行、下移一行。

注:

博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。

實現

添加一行

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;}}}}

效果

?

總結

以上是生活随笔為你收集整理的DataGridView怎样实现添加、删除、上移、下移一行的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。