Skip to main content

Disable DataGridView control automatically generate columns

In WinForm programming, when you use a DataGridView to display your data from a data source, you might see a problem: You've only specified the columns which should be display. But the gridview isn't only display that columns but also display others.

The reason: Your data source contains many data fields. Although you've created for it some need columns, the gridview will display all of them for users through a funtions called AutoGenerateColumn.

How to resolve this: It's easily. You just need disabling AutoGenerateColumn function:

  • Specify the columns which you want to display it for users. Of course, you must set Header text and DataPropertyName for them.

  • Browse your form code, go to where you set data source for the grid view. Put below code before that set data source code line (like: YOUR_GRIDVIEW_NAME.DataSource = YOUR_DATA_TABLE;):

[csharp]YOUR_GRIDVIEW_NAME.AutoGenerateColumns = false;[/csharp]
That's all.
Wish succeed!


Popular posts from this blog

Fix network printer error 0x0000709

When printing in the network, sometimes you see the error "Operation could not be completed (error 0x00000709). Double click the printer name and make sure that the printer is connected to the network".  Here we show how to fix the error: Open Windows Control Panel Open Credential Manager Choose Windows Credentials Click Add a Windows credential Internet or network address: input the computer name which share the printer User name: input "guest" Password: leave it blank Then click OK and reconnect the shared printer Do not need: uninstall Windows updates; change Windows registry... This maybe applied for error: 0x00000005, 0x0000011b... Watch the step by step video on here: