1 Replies - 237 Views - Last Post: 13 July 2014 - 09:24 AM Rate Topic: -----

#1 cjl123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 30-June 14

trying to save datagridview to binary file

Posted 13 July 2014 - 09:06 AM

hi world, i tried to dave my datagridview to a binary file it worked at first and then when i tried it again it said file is being used by another process. i have know idea why it done this but help would be much appreciated.


this is the code that loads the binary file
:
public void loadingparty()
{
string file = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\mygrid.bin";
if (File.Exists(file))
{
dataGridView1.Rows.Clear();

using (BinaryReader bw = new BinaryReader(File.Open(file, FileMode.Open)))
{
int n = bw.ReadInt32();
int m = bw.ReadInt32();
for (int i = 0; i < m; ++i)
{
dataGridView1.Rows.Add();
for (int j = 0; j < n; ++j)
{
if (bw.ReadBoolean())
{
dataGridView1.Rows[i].Cells[j].Value = bw.ReadString();
}
else bw.ReadBoolean();
}
}
}

}

this is the code that saves it :
public void savingparty()
{
string file = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\mygrid.bin";

using (BinaryWriter bw = new BinaryWriter(File.Open(file, FileMode.Create)))
{
bw.Write(dataGridView1.Columns.Count);
bw.Write(dataGridView1.Rows.Count);
foreach (DataGridViewRow dgvR in dataGridView1.Rows)
{
for (int j = 0; j < dataGridView1.Columns.Count; ++j)
{
object val = dgvR.Cells[j].Value;
if (val == null)
{
bw.Write(false);
bw.Write(false);
}
else
{
bw.Write(true);
bw.Write(val.ToString());
}
}
}
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: trying to save datagridview to binary file

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9073
  • View blog
  • Posts: 34,117
  • Joined: 12-June 08

Re: trying to save datagridview to binary file

Posted 13 July 2014 - 09:24 AM

Please do not create duplicate topics. Closing.

(not to mention this looks to be C# and not VB.NET)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1