Any code of your own that you haven't looked at for six or more months might as well have been written by someone else. --Eagleson's Law
Welcome to my blog about software development and the Microsoft stack.

I've been a full time .NET developer for ten years, but I didn't start my professional life as a programmer ... more
Share/Print this page:

Subscribe for news, updates and more:

Adding Columns to DataGrid Programmatically

BoundColumn and HyperLinkColumn

By steve on January 01, 2007.
Updated on January 22, 2012.
Viewed 86,252 times (32 times today).
Article TypesLanguagesMicrosoft StackSoftware DevelopmentTechnologiesTechnologies
SnippetC#SQL ServerData.NET ControlsASP.NET

For those trying to implement this, you'll notice the GetNorthwindProductTable(). This could be anything that returns a DataTable, but here's a quick example for how to get pull records from the Northwind database.

Snippet 2: Fill a DataSet from a Microsoft SQL Server Database using SQLDataAdapter and DataSet classes

Contents
private DataTable GetNorthwindProductTable()
{
	string connectionString = "workstation id=SERVERMASTER;packet size=4096;" 
		+ "integrated security=SSPI;data source=SERVERMASTER;"
		+ "persist security info=False;initial catalog=Northwind";

	string query = "select * from Products";
	SqlDataAdapter da = new SqlDataAdapter(query, connectionString);
	DataSet ds = new DataSet();
	da.Fill(ds);

	return ds.Tables["Table"];

}
Back to Top

User Comments (5)

Posted 2007 Jul 04 05:53 AM. reply
Hi,

I want add columns to the datagrid one is boundcolumn and hyperlink , i have added these two columns but when ever i am filling rows, the data is not storing in the mentioned columns, its storing only @ auto generated columns, please send reply as soon as possible.

Srinivas Reddy
Reply 2007 Sep 15 22:21 PM by steve. reply
Hi Srinivas,

I'm thinking that you haven't set the AutoGenerateColumns property to false.

DataGrid1.AutoGenerateColumns = false;

Cheers,
Steve
Posted 2008 Jun 25 13:17 PM. reply
Hi,

Can you please add a snapshot how it would look with the field details in it? It would be helpful in understanding the concept.

Thanks,
Shal

Shal
Posted 2010 May 12 08:31 AM. reply
Muchas Gracias Amigo me has salvado de una...

Marco A. Herrera G.
Posted 2012 Jan 12 06:42 AM. reply
I have a datagrid with 4 column as below: A B C D I want to when application is loaded, my datagrid will added a new column F into front column B asw below: A F B C D can you help me? thanks.

priya
Post Your Comment
  You may post without logging in or login here.
Display Name: Required.
Email: Required. Will not be shown. Used for identicon.
Comment:
Allowed tags: <quote></quote>, <code></code>, <b></b>, <i></i>, <u></u>, <red></red>
 
   Please type text as shown in the image at left.