DataGridView to XLS/CSV
-
G'day all, I'm writing an application that requires me to output the values stored within a DataGridView to an cls/csv file (or basically anything that M$ Excel can read). This is as far as i have gotten:
string strValue = " "; for (int i = 0; i < stocksDataGridView.Rows.Count - 1; i++) { for (int j = 0; j < stocksDataGridView.Rows\[i\].Cells.Count; j++) { if (!string.IsNullOrEmpty(stocksDataGridView\[j, i\].Value.ToString())) { if (j > 0) strValue += "," + stocksDataGridView\[j, i\].Value.ToString(); else { if (!String.IsNullOrEmpty(strValue)) strValue = stocksDataGridView\[j, i\].Value.ToString(); else strValue = strValue + Environment.NewLine + stocksDataGridView\[j, i\].Value.ToString(); } } } strValue = strValue + Environment.NewLine; } string fileName = @saveFileDialog1.FileName; if (File.Exists(fileName) && !String.IsNullOrEmpty(strValue)) { File.WriteAllText(fileName, strValue); }
I'm sure its not very pretty and i have no idea how/where/why i have it in there, but all i can say is that it doesn't actually output anything. Any and all help will be greatly appreciated. Thanks in Advance, Tom
-
G'day all, I'm writing an application that requires me to output the values stored within a DataGridView to an cls/csv file (or basically anything that M$ Excel can read). This is as far as i have gotten:
string strValue = " "; for (int i = 0; i < stocksDataGridView.Rows.Count - 1; i++) { for (int j = 0; j < stocksDataGridView.Rows\[i\].Cells.Count; j++) { if (!string.IsNullOrEmpty(stocksDataGridView\[j, i\].Value.ToString())) { if (j > 0) strValue += "," + stocksDataGridView\[j, i\].Value.ToString(); else { if (!String.IsNullOrEmpty(strValue)) strValue = stocksDataGridView\[j, i\].Value.ToString(); else strValue = strValue + Environment.NewLine + stocksDataGridView\[j, i\].Value.ToString(); } } } strValue = strValue + Environment.NewLine; } string fileName = @saveFileDialog1.FileName; if (File.Exists(fileName) && !String.IsNullOrEmpty(strValue)) { File.WriteAllText(fileName, strValue); }
I'm sure its not very pretty and i have no idea how/where/why i have it in there, but all i can say is that it doesn't actually output anything. Any and all help will be greatly appreciated. Thanks in Advance, Tom
Hi Tom, are you sure that the command "File.WriteAllText" is executed? You only enter the if-block if your file exists! Maybe you have forgotten a "!" before "File.Exists"? Regards Sebastian
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
-
Hi Tom, are you sure that the command "File.WriteAllText" is executed? You only enter the if-block if your file exists! Maybe you have forgotten a "!" before "File.Exists"? Regards Sebastian
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.