Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Make Datagridview Column Fully visible
 

Make Datagridview Column Fully visible

I have a datagridview with 10 columns. One of the columns is partially visible when i moved the horizontal scroll bar. I want to make the column fully visible when I clicked one of the cells in it.

Is there any way for doing this?
void Func  Friday, July 17, 2009 1:58 PM
Hi Manu,

You can use the GetColumnDisplayRectangle method of the DataGridView and the column width to determine wether the clicked cell is partially displayed. If so, use the HorizontalScrollingOffSet property to scroll the DataGridView. The following is a sample:

dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

Rectangle entireRect = this.dataGridView1.GetColumnDisplayRectangle(e.ColumnIndex,false);

Rectangle visiblePart = this.dataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, true);

if (visiblePart.Width < entireRect.Width)

this.dataGridView1.HorizontalScrollingOffset += entireRect.Width - visiblePart.Width;

else if(visiblePart.Width < this.dataGridView1.Columns[e.ColumnIndex].Width)

this.dataGridView1.HorizontalScrollingOffset -= this.dataGridView1.Columns[e.ColumnIndex].Width - visiblePart.Width;

Please remember to mark the replies as answers if they help and unmark them if they provide no help. end us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.

Linda Liu  Monday, July 27, 2009 10:39 AM
Hello Manulji,
You can change autosizemode of datagriviewcolumns in cell_click event. try like,
 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].AutoSizeMode != DataGridViewAutoSizeColumnMode.DisplayedCells)
                dataGridView1.Columns[e.ColumnIndex].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
        }
NareshG  Saturday, July 18, 2009 8:45 PM
Hi Naresh,

Thanks For the response.

We have fixed width columns. The solution that you specified will not work in that case. Is there any other solution?
void Func  Sunday, July 19, 2009 10:15 AM
Hello manuliji,
If you want to keep,fixed width of columns, then what do you mean by "make column fully visible" ?
NareshG  Monday, July 20, 2009 6:32 AM
Hi Naresh,

My grid can only show 5 columns within the gridview area. I have then added two more columns. As a result the horizontal scroll bar appeared. When I moved the horizontal scroll bar the last column is partially appeared in the visible area.

Now what I want is if i click on a cell on the last column, it should be displayed fully in the visible area.

I want the same behavior with the tab and arrow key. IF i press the tab key or right arrow key from a cell left of the partially visible column, the column becomes fully visible.


I am not able to attach a screen shot..

Manu
void Func  Monday, July 20, 2009 3:28 PM
Hi Manu,

You can use the GetColumnDisplayRectangle method of the DataGridView and the column width to determine wether the clicked cell is partially displayed. If so, use the HorizontalScrollingOffSet property to scroll the DataGridView. The following is a sample:

dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

Rectangle entireRect = this.dataGridView1.GetColumnDisplayRectangle(e.ColumnIndex,false);

Rectangle visiblePart = this.dataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, true);

if (visiblePart.Width < entireRect.Width)

this.dataGridView1.HorizontalScrollingOffset += entireRect.Width - visiblePart.Width;

else if(visiblePart.Width < this.dataGridView1.Columns[e.ColumnIndex].Width)

this.dataGridView1.HorizontalScrollingOffset -= this.dataGridView1.Columns[e.ColumnIndex].Width - visiblePart.Width;

Please remember to mark the replies as answers if they help and unmark them if they provide no help. end us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.

Linda Liu  Monday, July 27, 2009 10:39 AM

You can use google to search for other answers

Custom Search

More Threads

• Web application
• Combobox rebinds unexpectedly
• Bind DataGridView to Custom Collection
• Informing BEFORE deleteting
• different controls in a datagrid?
• Trying to make a simple database application: Updating a ListBox with data related to another
• Error: LookupBox.Dispose: no suitable method in LookupControlWalkthrough sample
• Any good examples of custom paging with data adapters?
• Always display Vertical ScrollBar in DataGridView
• Advice needed on changing database schema after deployment