954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to bind selected columns to datagridview at runtime?

How to bind selected columns to datagridview at runtime.
I have written select * from tableName. And result i have stored in DataTable dt. Now i want only selected columns to be displayed in datagridview at runtime. I dont want to bound gridview at designtime.

Jigs28
Light Poster
42 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Do you need all of the columns out of the database or could you just select only the ones you need? Most of the time SELECT * isn't really needed.
If not, you can bind various columns of your gridview to the columns of the data source in code. This post maybe a little old but explains the theory:
http://www.gamedev.net/topic/417402-how-to-map-a-datagridview-column-to-a-column-of-the-datasource-datatable-csc-2005/

hericles
Practically a Posting Shark
823 posts since Nov 2007
Reputation Points: 136
Solved Threads: 167
 

Actuly i can write only required column name but my query is parameterized and cloumn which i am using for parameter that i dont wont to display

Jigs28
Light Poster
42 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

And other columns i am binding with other labels so thats why i have to do * option.

Jigs28
Light Poster
42 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Thanks hericles its working.
All i have done is instead of writing DataGridViewTextBoxColumn i have written DataGridViewColumn. And DataGridViewColumn's obj.CellTemplate = new DataGridViewTextBoxCell(); because DataGridViewTextBoxColumn do not have DataPropertyName property.
Thank you your link was helpfull

Jigs28
Light Poster
42 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You