简体   繁体   中英

Delete empty rows from all datagridviews

This way I can delete empty rows from one 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);
                }
            }

I got around 6 datagridviews and I want to delete empty rows of all.

Is there a way to delete empty rows from all the datagridviews in the interface??

You could create a method

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

and pass each of your 6 DataGridViews to the method.

clearGrid(PrimaryRadGridView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM