1

I have a DatagridView in my application. I want to serialize the gridview columns into a XML-file. Serializing just the columns name is easy but I want to serialize the columns index too.

Serializing the columns would I do like this:

ArrayList array = new ArrayList();
foreach(DataGridViewColumn column in datagridView1.Columns)
{
    array.Add(column.Name)
}

using (FileStream file = new FileStream("test.xml", FileMode.Create))
{
     SoapFormatter formatter = new SoapFormatter();
     formatter.Serialize(file, array);
}

But I´m wondering how can I serialize the column index too?

Thanks in advance for any help!

3
  • What for do you use SoapFormatter for XML serialization instead of XmlFormatter?? Commented May 31, 2011 at 18:11
  • 1
    What version of .NET are you using? If you're using .NET 2.0 or above, you shouldn't be using ArrayList at all. Commented May 31, 2011 at 18:20
  • @John Saunders - Iam using 3.5. Commented May 31, 2011 at 18:22

2 Answers 2

2

Two things to note up front:

  • Since you're not creating a soap message, I'd recommend BinarySerializer or XmlSerializer for ad-hoc serialization.
  • You should definitely prefer a strongly-typed List<string> over an untyped ArrayList.

With that said, I'd recommend against serializing altogether -- its too heavyweight for your purposes, and its not a very good approach to long-term storage or transfer of objects (e.g. let's say you serialize a class in v1.0 of your app, then deserialize it in v2.0, all sorts of weirdness can happen).

I'd recommend the following approach: sort your columns by index, then write them out line-by-line to your file. Your columns will be ordered correctly in your file:

using(FileStream file = new FileStream("test.xml", FileMode.Create))
{   
    var columnNames =
        dataGridView1.Columns
        .Cast<DataGridViewColumn>()
        .OrderBy(x => x.ColumnIndex)
        .Select(x => x.Name);

    foreach(string column in columnNames)
        file.WriteLine(column);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Create a public class ColumnInfo with Name and Index and you can store this to array list which can be searialized.

[Serializable]
public class ColumnInfo
{
    public string Name;
    public int Index;       
}

ArrayList array = new ArrayList();
foreach(DataGridViewColumn column in datagridView1.Columns)
{
    ColumnInfo ci = new ColumnInfo();
    ci.Name = column.Name;
    //ci.Index = column.Index; //decide how to get column index
    array.Add(ci);
}

using (FileStream file = new FileStream("test.xml", FileMode.Create))
{
     SoapFormatter formatter = new SoapFormatter();
     formatter.Serialize(file, array);
}

Update I personally use XmlFormater, I copied the source sample from question for example. Thank you abstishchev

6 Comments

@Gopalakrishnan Subramani - That could work but for deserialization then, how can deserialize the column name and the index for that column. Do you have any idea?
What for do you use SoapFormatter for XML serialization instead of XmlFormatter??
Honestly I don't know SoapFormatter.. Since the OP already knows to use serialization with SoapFormatter, I used the same as an example.
@abatishchev - Dont really know why. Wich one is better in my situation?
@Erika: I recommend to use XmlFormatter for XML output and BinaryFormatter for binary
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.