Mava
Mava

Reputation: 83

C# - Get the sum of the total amount of width for each column in datagridview

Is there any faster way other than looping through each column and increment a variable with their width?

For now this is the best solution, but is pretty slow:

int contentWidth = 0;
foreach (DataGridViewColumn column in grid.Columns) {
    contentWidth += column.Width;
}

Upvotes: 0

Views: 829

Answers (1)

Mava
Mava

Reputation: 83

This will return the length of visible columns:

contentWidth = grid.Columns.GetColumnsWidth(DataGridViewElementStates.Displayed);

Upvotes: 1

Related Questions