DataGridView怎樣實現添加、刪除、上移、下移一行

場景

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

注:this

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

實現

添加一行

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

 

注: .net

這裏是沒綁定數據源狀況下的上移一行,添加的一行時經過是上面新增的方法實現的。 code

此時dataGridView的dataSource是爲空的。 orm

其中用到獲取選中行的方法: blog

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

 

效果

 

 

 

相關文章
相關標籤/搜索