[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

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

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
zstafa
Asked:
zstafa
  • 2
1 Solution
 
tomasX2Commented:
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
 
zstafaAuthor Commented:
thanks alot. I just copy and pasted the code and it works.
0
 
tomasX2Commented:
Glad to help... good luck
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now