I got an issue i cannot help myself out with.
My program loads an CSV (excel) file which has a database build-in all assigned in [a] line1.
ex; A-1 = testcol,testcol,testcol
ex; A-2 = value1,value2,value3
now i've got my things to read just fine in their own columns, but i cant seem to save them correctly.
once its saved my program decides to add another Column and a whole Empty Row.
so my file now occurs as;
ex; A-1 = testcol,testcol,testcol,column1
ex; A-2 = NULL,NULL,NULL,NULL
i would like to remove the Column1 (and save the file without doing this).
aswell as removing the empty row..
This is how my save procedure looks like:
toolStripProgressBar1.Value = 0;
if (dataGridView1.Rows.Count == 0)
{
return;
}
StringBuilder sb = new StringBuilder();
string columnsHeader = "";
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
columnsHeader += dataGridView1.Columns[i].Name + ",";
}
sb.Append(columnsHeader + Environment.NewLine);
foreach (DataGridViewRow dgvRow in dataGridView1.Rows)
{
if (!dgvRow.IsNewRow)
{
for (int c = 0; c < dgvRow.Cells.Count; c++)
{
sb.Append(dgvRow.Cells[c].Value + ",");
}
sb.Append(Environment.NewLine);
}
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "CSV files (*.csv)|*.csv";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
int maxnumber = 100;
toolStripProgressBar1.Value = 0;
toolStripProgressBar1.Maximum = maxnumber;
for (int x = 0; x <= maxnumber - 1; x++)
{
toolStripProgressBar1.Value += 1;
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName, false))
{
string result;
result = Path.GetFileNameWithoutExtension(sfd.FileName);
tabPage1.Text = result;
toolStripStatusLabel1.Text = "[1]File Saved! - " + result;
sw.WriteLine(sb.ToString());
}
}
}
i have no space here on this website to either put my load example here, but its below this line incase you do want to read it all.
OpenFileDialog openFile = new OpenFileDialog();
openFile.InitialDirectory = "c:\\";
openFile.Filter = " All Files (*.*) | *.*| CSV Files (*.csv) | *.csv";
openFile.FilterIndex = 2;
openFile.RestoreDirectory = true;
try
{
if (this.dataGridView1.DataSource != null)
{
this.dataGridView1.DataSource = null;
}
else
{
this.dataGridView1.Rows.Clear();
}
toolStripProgressBar1.Value = 0;
if (openFile.ShowDialog() == DialogResult.OK)
{
comboBox1.Items.Clear();
string file = openFile.FileName;
StreamReader sr = new StreamReader(file);
string result;
result = Path.GetFileNameWithoutExtension(file);
tabPage1.Text = result;
string[] str = File.ReadAllLines(file);
DataTable dt = new DataTable();
string[] temp = str[0].Split(',');
foreach (string t in temp)
{
dt.Columns.Add(t, typeof(string));
comboBox1.Items.Add(t);
}
var lines = File.ReadAllLines(file);
var count = lines.Length;
for (int i = 1; i < str.Length; i++)
{
toolStripProgressBar1.Value += 1;
toolStripProgressBar1.Maximum = count;
List<string> list = new List<string>(str[i].Split(','));
if (str[i].Contains('"'))
{
for (int j = 0; j < list.Count; j++)
{
while ((list[j].Split('"').Length % 2) == 1)
{
list[j] += "," + list[j + 1];
list.RemoveAt(j + 1);
}
if (list[j].StartsWith("\"") && list[j].EndsWith("\""))
list[j] = list[j].Substring(1, list[j].Length - 2);
}
}
else
{
string[] t = str[i].Split(',');
dt.Rows.Add(t);
}
}
dataGridView1.DataSource = dt;
toolStripStatusLabel1.Text = "[1]Lines loaded: " + count;
}
}
catch (Exception ex)
{
MessageBox.Show("[1]Error: The CSV you selected could not be loaded \n\n" + ex.Message, "Load Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
please help me, i have no idea how to fix this and its quite important i get my database saving right :)