[英]Delete empty rows from all datagridviews
這樣,我可以從一個datagridview刪除空行。
bool Empty = true;
for (int i = 0; i < PrimaryRadGridView.Rows.Count; i++)
{
Empty = true;
for (int j = 0; j < PrimaryRadGridView.Columns.Count; j++)
{
if (PrimaryRadGridView.Rows[i].Cells[j].Value != null && PrimaryRadGridView.Rows[i].Cells[j].Value.ToString() != "")
{
Empty = false;
break;
}
}
if (Empty)
{
PrimaryRadGridView.Rows.RemoveAt(i);
}
}
我大約有6個datagridviews,我想刪除所有的空行。
有沒有一種方法可以從接口中的所有datagridviews中刪除空行?
您可以創建一個方法
private void clearGrid(DataGridView view) {
for (int row = 0; row < view.Rows.Count; ++row) {
bool isEmpty = true;
for (int col = 0; col < view.Columns.Count; ++col) {
object value = view.Rows[row].Cells[col].Value;
if (value != null && value.ToString().Length > 0) {
isEmpty = false;
break;
}
}
if (isEmpty) {
// deincrement (after the call) since we are removing the row
view.Rows.RemoveAt(row--);
}
}
}
並將您的6個DataGridViews中的每一個傳遞給該方法。
clearGrid(PrimaryRadGridView);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.