Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Maintaining column width during scrolling
 

Maintaining column width during scrolling

I have a datagridview where I am setting the column width to match the content length by doingAutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)

But during scrolling, columns are resized for each scroll to fit the content. How do I freeze the column width during scrolling? Basically, maintain the same width as it's original loading state and not re-adjusting itself during scrolling? Even if it means some content will be cut off.

Thanks!
penn10  Thursday, August 20, 2009 3:33 PM
Hello,

Thanks for your post on MDSN forum.

Based on my test, the column's width is fixed to the largest content within that column. When scrolling, the width is not changed. I've tried with the folloing code.

private void Form2_Load(object sender, EventArgs e)
{
string val = "test";
DataTable dt = new DataTable();
dt.Columns.Add("test");
for (int i = 1; i < 500; i++)
{
dt.Rows.Add(val);
val += "t";
}
this.dataGridView1.DataSource = dt;
this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;
}

However, if you want to fix the column's size when scrolling, my suggestion is that we can disable the autosize functionality when the mouse left button is pressed, and enable the autosize when the left button is up.

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Rong-Chun Zhang  Friday, August 21, 2009 10:36 AM
Hello,

Thanks for your post on MDSN forum.

Based on my test, the column's width is fixed to the largest content within that column. When scrolling, the width is not changed. I've tried with the folloing code.

private void Form2_Load(object sender, EventArgs e)
{
string val = "test";
DataTable dt = new DataTable();
dt.Columns.Add("test");
for (int i = 1; i < 500; i++)
{
dt.Rows.Add(val);
val += "t";
}
this.dataGridView1.DataSource = dt;
this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;
}

However, if you want to fix the column's size when scrolling, my suggestion is that we can disable the autosize functionality when the mouse left button is pressed, and enable the autosize when the left button is up.

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Rong-Chun Zhang  Friday, August 21, 2009 10:36 AM

Hello,

I am writing to check the status of the issue on your side. Would you mind letting me know the result of the suggestions? If you have any additional question, welcome to post here.

Have a great day!

Thanks,
Rong-Chun Zhang


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Rong-Chun Zhang  Friday, August 28, 2009 12:30 PM

You can use google to search for other answers

Custom Search

More Threads

• cONTEXTmeNU ON lEFT cLICK
• Data binding and Checkbox values
• mail sending using VB
• using datagridview with a mySQL server
• Lookup list in datagridview
• Populating a Combox
• Changing a cell color in a DataGridView based on a condition
• help me i can't save (Concurrency violation when handle Concurrency violation !!)
• FillByParameter - using parameters with the Select Statement
• datagridview create multiple column header rows