Solved

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

Posted on 2004-10-13
3
198 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
ID: 12304162
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
ID: 12304192
thanks alot. I just copy and pasted the code and it works.
0
 
LVL 5

Expert Comment

by:tomasX2
ID: 12304198
Glad to help... good luck
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

18 Experts available now in Live!

Get 1:1 Help Now