Solved

How do I insert a column into a datagrid at position 0

Posted on 2004-10-13
3
197 Views
Last Modified: 2010-04-15
Hello all experts. I am populating a datagrid from a dataset. Afterwards I want to be able to insert a new column at positon zero. I am not using sql or any database. My grid data is all hardcode. Any help would be much appreciated.  
0
Comment
Question by:zstafa
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
tomasX2 earned 500 total points
Comment Utility
just adding a column to the dataset should show up in the datagrid.
you want to show the column as the first column ( at position zero );

I would create a new dataset and add the column you want to add and then copy the others... then merge.

                  DataSet newDS = new DataSet();                  
                  newDS.Tables.Add(new DataTable(ds.Tables[0].TableName));
                  newDS.Tables[0].Columns.Add(new DataColumn("MyFirstColumn",typeof(string)));                  
                  for( int i = 0; i < ds.Tables[0].Columns.Count;i++)
                  {
                        newDS.Tables[0].Columns.Add(new DataColumn(ds.Tables[0].Columns[i].ColumnName, ds.Tables[0].Columns[i].DataType ));
                  }                  
                  newDS.Merge(ds);
                  dataGrid1.DataSource = newDS.Tables[0];





0
 
LVL 3

Author Comment

by:zstafa
Comment Utility
thanks alot. I just copy and pasted the code and it works.
0
 
LVL 5

Expert Comment

by:tomasX2
Comment Utility
Glad to help... good luck
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Aspnet xml 2 36
asp.net mvc5 3 35
Windows Service to Receive TCP Packets 4 30
getting id from database 5 24
Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now