DataGridView Last Column Fill When Hiding Columns?

I understand DataGridView column 'Fill' property to make the last column 'Auto-Size'. I have a DataGridView where a user can hide columns including the last one.

Anyone know how to make the last DataGridView column 'Fill' if a user can hide columns?

pointemanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Death259Commented:
I wrote something simple that might do what you want. You will need this code portion to run everytime a column is made invisible though. If this doesn't solve your problem exactly, it should hopefully point you in the right direction.
int columnCount = dgv.Columns.Count;
int visibleColumn = 0;
for (int i = 0; i < dgv.Columns.Count; i++)
{
        if (!dgv.Columns[i].Visible)
        {
        	columnCount--;
        }
        else
        {
        	visibleColumn = i;
	}
}
if (columnCount == 1)
{
	dgv.Columns[visibleColumn].Width = dgv.Width;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.