Auto sizing a DataGridView based on contents

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
My DataGridView width will vary based on a Description field that I am getting from my SQL Server Query and putting into my DataGridView.
I did Google this and tried the code below which was supposed to auto fit the contents of my data into the DataGridView...but when testing, this errored out with the following error message...
Specified argument was out of the range of valid values.
Parameter name: rowIndex
This is the C# code I tried via a web page and previous answer...int i = 0;
foreach (DataGridViewColumn c in DataGridViewCodeMainCodeSetValueInquiry.Columns)
{
i += c.Width;
}

DataGridViewCodeMainCodeSetValueInquiry.Width = i + DataGridViewCodeMainCodeSetValueInquiry.RowHeadersWidth + 2;
DataGridViewCodeMainCodeSetValueInquiry.Height =
DataGridViewCodeMainCodeSetValueInquiry.GetRowDisplayRectangle(DataGridViewCodeMainCodeSetValueInquiry.NewRowIndex, true).Bottom+
DataGridViewCodeMainCodeSetValueInquiry.GetRowDisplayRectangle(DataGridViewCodeMainCodeSetValueInquiry.NewRowIndex, false).Height;

Any help would GREATLY be appreciated in attempting to Auto size my DataGridView based on my SQL Server result set.
Thanks for your review and am hopeful for a reply.
PSULionRP

View the full article
 
Back
Top