Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
I need to export only visible columns from datatable to xml, I have this code
 
                DataTable dt = new DataTable();
 
                foreach (DataGridViewColumn col in dg1.Columns)
                {
                    dt.Columns.Add(col.HeaderText);
                }
 
                foreach (DataGridViewRow row in dg1.Rows)
                {
                    DataRow dRow = dt.NewRow();
 
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        dRow[cell.ColumnIndex] = cell.Value;
                    }
                    dt.Rows.Add(dRow);
                }
 
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                ds.WriteXml(saveFileDialog1.FileName);
it works but for all columns
Posted 21-Nov-12 3:20am
Edited 21-Nov-12 5:48am
v2
Comments
sinhasourabh at 21-Nov-12 8:29am
   
You need to remove the inivisible coloums first. Or Write your own customized XML Writer using XDOC.
shonezi at 21-Nov-12 8:45am
   
how to do this???
ryanb31 at 21-Nov-12 9:19am
   
As you are looping through the columns check it's visible property. Did you write this code?
shonezi at 21-Nov-12 9:30am
   
I have this
 
for (int i = 0; i < dg1.Columns.Count; i++)
{
if (dg1.Columns[i].Visible)
{
//code
}
}
but its not working
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

See a very similar question I answered for you yesterday?
 
exporting visible columns of datagridview to csv[^]
  Permalink  
Comments
shonezi at 21-Nov-12 12:19pm
   
I did mate and I cant get it running on this code :)))
shonezi at 21-Nov-12 12:37pm
   
I have modified my code to this and it works 500 percent because half of the columns that are hidden in dgv are hidden in exported xml, BUT I have a strange thing also shownand that is that I have 17 columns which are hidden but headertext also gets exported???????and I dont know what to do
 

DataTable dt = new DataTable();
 
dt.TableName = "MyTable";
foreach (DataGridViewColumn col in dg1.Columns)
{
dt.Columns.Add(col.HeaderText);
}
foreach (DataGridViewRow gridRow in dg1.Rows)
{
if (gridRow.IsNewRow)
continue;
DataRow dtRow = dt.NewRow();
for (int i = 0; i < dg1.Columns.Count; i++)
if (dg1.Columns[i].Visible)
dtRow[i] = (gridRow.Cells[i1].Value == null ? DBNull.Value : gridRow.Cells[i].Value);
dt.Rows.Add(dtRow);
}

DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.WriteXml(saveFileDialog1.FileName);
shonezi at 21-Nov-12 12:37pm
   
50 percent :)))
shonezi at 21-Nov-12 12:56pm
   
I found a problem, code works, its something I did that makes those columsn show up:))
Rob@Love2Code at 22-Nov-12 5:36am
   
Did my original Solution work for you in the end?
shonezi at 22-Nov-12 5:47am
   
yes mate, sorry I didnt reply,thank you again
Rob@Love2Code at 22-Nov-12 6:02am
   
No worries, Glad I could help.... Good luck with the rest of it :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Look at this project, it can export visible columns or all the columnhs from datatable to xml.
export data to xml[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,580
1 OriginalGriff 8,370
2 Peter Leow 4,859
3 Kornfeld Eliyahu Peter 3,210
4 Maciej Los 2,301


Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100