How to remove rows from DataGridView?

前端 未结 1 1913
再見小時候
再見小時候 2021-01-17 11:49

I have a winform with preloaded DataGridView over it...I want to remove rows from datagridview on selecting or highlighting the rows and clicking over the button...

1条回答
  •  执笔经年
    2021-01-17 12:12

    If you have AllowUserToAddRows enabled on your DataGridView then you might be accidently deleting the empty row at the bottom of the DataView which is a placeholder for the next user created row. Try disabling this option if not required, otherwise try using code like this:

    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
    {
        if(!row.IsNewRow)
           dataGridView1.Rows.Remove(row);
    }
    

    0 讨论(0)
提交回复
热议问题