1. 程式人生 > 其它 >Winform 刪除DataGridView指定行後所有行

Winform 刪除DataGridView指定行後所有行

 private void skinDataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 0 || e.RowIndex < 0) return;
            if (skinDataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() == "結束")//如果當前行0列為“結束”
            {
                skinDataGridView1.AllowUserToAddRows 
= false;//則自動增加行為false for (int i = e.RowIndex+1 ; i < skinDataGridView1.Rows.Count; i++)//刪除“結束”行後所有行 { DataGridViewRow row = skinDataGridView1.Rows[i]; skinDataGridView1.Rows.Remove(row); i--;//關鍵中的關鍵 } }
else//否則 { if(e.RowIndex== skinDataGridView1.Rows.Count-1)//如果當前點選行為表格的最後一行 skinDataGridView1.AllowUserToAddRows = true;//則開啟自動增加行功能 }

自己寫軟體的一些筆記,大神請忽略!