I'm currently attempting to use datagridview for the first time, I have managed to do many things with it so far (such as importing text files) however, I have trouble when attempting to save the datagridview's contents to a text file.
The output I'm currently getting is:
0,
Cat,
Yes,
10,
20,
30,
40,
50,
1,
Dog,
No,
10,
20,
30,
40,
50,
I want the export to look like this:
0, Cat, Yes, 10, 20, 30, 40, 50
1, Dog, No, 10, 20, 30, 40, 50
etc.
This is the code I'm currently using:
using (TextWriter tw = new StreamWriter("example.txt"))
{
for(int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for(int j = 0; j < dataGridView1.Columns.Count; j++)
{
tw.WriteLine($"{dataGridView1.Rows[i].Cells[j].Value.ToString()},");
}
}
}
Anyone here able to help me with this issue? Thank you!
Try the following changes tw.Write() insted of tw.WriteLine():
using (TextWriter tw = new StreamWriter("example.txt"))
{
for(int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for(int j = 0; j < dataGridView1.Columns.Count; j++)
{
tw.Write($"{dataGridView1.Rows[i].Cells[j].Value.ToString()}");
if(!j == dataGridView1.Columns.Count - 1)
{
tw.Write(",");
}
}
tw.WriteLine();
}
}
So, DGV to Text file? This is how I do it.
private void button1_Click(object sender, EventArgs e)
{
//This line of code creates a text file for the data export.
System.IO.StreamWriter file = new System.IO.StreamWriter(#"C:\\your_path_here\\sample.txt");
try
{
string sLine = "";
//This for loop loops through each row in the table
for (int r = 0; r <= dataGridView1.Rows.Count - 1; r++)
{
//This for loop loops through each column, and the row number
//is passed from the for loop above.
for (int c = 0; c <= dataGridView1.Columns.Count - 1; c++)
{
sLine = sLine + dataGridView1.Rows[r].Cells[c].Value;
if (c != dataGridView1.Columns.Count - 1)
{
//A comma is added as a text delimiter in order
//to separate each field in the text file.
//You can choose another character as a delimiter.
sLine = sLine + ",";
}
}
//The exported text is written to the text file, one line at a time.
file.WriteLine(sLine);
sLine = "";
}
file.Close();
System.Windows.Forms.MessageBox.Show("Export Complete.", "Program Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.Exception err)
{
System.Windows.Forms.MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
file.Close();
}
}
Use tw.Write() instead of tw.WriteLine() until you are finished with all but the last column for the row, then you tw.WriteLine() on the last column data to end the line.
I don't work in C#, but often use these forums as a reference. I develop interfaces for industrial applications. Anyway, the simplest way to do this is through the clipboard. Use the SelectAll() method on your DGV, Throw that DGV data into the Clipboard with the Clipboard.SetDataObject() method, use the File.WriteAllText(), and Clipboard.GetText() methods to write the clipboard text to a file. My code is below. You will notice I have to write out everything from it's namespace so I apologize for it not being exactly how C# is. Note: If you want to make a csv file, DGVs view cell breaks as horizontal tabs (ascii 9). You can do a replace() method for commas. Note: DGV.ClearSelection() is a nice way to finish it off. Note: you may have to enable ClipboardCopyMode on your DGV.
DataGridView1.SelectAll();
System.Windows.Forms.Clipboard.SetDataObject(DataGridView1.GetClipboardContent());
System.IO.File.WriteAllText("File.txt",System.Windows.Forms.Clipboard.GetText());
With the following code you can save and restore the content of every dataGridView.
How to do it:
string sContent = "";
private void btnSaveDataGridViewContent_Click(object sender, EventArgs e)
{
sContent = ReadStringFromDataGridView(ref dataGridView1);
[Write sContent to File/Database/Whatever]
}
private void btnRestoreDataGridViewContent_Click(object sender, EventArgs e)
{
[Read sContent from File/Database/Whatever]
WriteStringToDataGridView(ref dataGridView1, sContent);
}
Helper functions:
string ReadStringFromDataGridView(ref DataGridView MyDataGridView)
{
string sContent = "";
for (int row = 0; row<MyDataGridView.RowCount; row++)
{
if (row > 0) sContent += "\r";
for (int col = 0; col<MyDataGridView.ColumnCount; col++)
{
if (col > 0) sContent += ";";
sContent += MyDataGridView[col, row].Value;
}
}
return sContent;
}
void WriteStringToDataGridView(ref DataGridView MyDataGridView, string sContent)
{
MyDataGridView.Rows.Clear();
string[] Rows = sContent.Split("\r".ToCharArray());
for (int row=0; row<Rows.Length; row++)
{
MyDataGridView.RowCount = Rows.Length;
string[] Cols = Rows[row].Split(";".ToCharArray());
for (int col=0; col<Cols.Length; col++)
{
MyDataGridView[col, row].Value = Cols[col];
}
}
}
string file = "C:\\Users\\Sangeeth\\Desktop\\Excel.txt";
using (TextWriter tw = new StreamWriter(file))
{
int i, j = 0;
for(i = 0; i < dataGridView1.Rows.Count -1; i++)
{
for (j = 0; j < dataGridView1.Columns.Count; j++)
{
tw.Write($"{dataGridView1.Rows[i].Cells[j].Value.ToString()}");
tw.Write(",");
}
tw.WriteLine(" ");
}
}
How to Save multiple files in asp.net 4.0 by Fileupload Control with multiple fileupload control?
I have two fileupload control one for image and second for thumbimage. So I want to save multiple image and thumbimage ?
You are actually overwriting the fist file with the second one when you are in the loop. I would suggest you to create a list for the files and add to the list in the loop like below. You will then have a list of file when you can use firstOrDefault() for the first item as well as use Skip() and Take() to select any item you want.
HttpFileCollection uploadedFiles = Request.Files;
List<HttpPostedFile> fileList1 = new List<HttpPostedFile>();
List<HttpPostedFile> fileList2 = new List<HttpPostedFile>();
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile hpf = uploadedFiles[i];
var hpfKey = uploadedFiles.Keys[i];
if (hpfKey.IndexOf("FileUpload1") > 0)
{
fileList1.Add(hpf);
}
if (hpfKey.IndexOf("FileUpload2") > 0)
{
fileList2.Add(hpf);
}
}
Update:
Now to get the first file you call FirstOrDefault() on the list like below:
fileList1.FirstOrDefault();
And to get the second file :
fileList1.Skip(1).FirstOrDefault();
HttpFileCollection uploadedFiles = Request.Files;
int i = uploadedFiles.Count;
List<HttpPostedFile> fileList1 = new List<HttpPostedFile>();
List<HttpPostedFile> fileList2 = new List<HttpPostedFile>();
if (i > 0)
{
for (int j = 0; j < i/2; j++)
{
fileList1.Add(uploadedFiles[j]);
}
}
if (i > 0)
{
for (int j = i / 2; j < i; j++)
{
fileList2.Add(uploadedFiles[j]);
}
}
int filecount = fileList1.Count;
if (filecount > 0)
{
for (int j = 0; j < filecount; j++)
{
string image = fileList1[j].FileName;
fileList1[j].SaveAs(imagepath);
string image = fileList2[j].FileName;
fileList2[j].SaveAs(imagepath);
}
}
I'm trying to get data from a file and store them in an array then display the data in a listbox the find then sum and display it in a text box. Here's my code and it doesn't work. I'm not sure what i'm doing wrong.
private void findClick(object sender, EventArgs e)
{
int sum;
using (OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() == DialogResult.OK)
{
using (StreamReader InputFile = new StreamReader(ofd.FileName))
{
while (InputFile.EndOfStream == false)
{
int[] array = new int[listBox.Items.Count];
for (int i = 0; i < listBox.Items.Count; i++)
{
// array[i] = Convert.ToInt32(listBox.Items[i].ToString());
array[i] = int.Parse(listBox.Items[i].ToString());
sum = array.Sum();
TotalAmtlabel.Text = sum.ToString("N0");
TotalNumberslabel.Text = listBox.Items.Count.ToString();
TotalAmountlabel.Text = string.Format("{0:N0}", sum);
}
}
}
}
}
}
listBox.Items.AddRange(File.ReadAllLines(ofd.FileName));
Try this and modify according to your needs:
string[] amounts = File.ReadAllLines(ofd.FileName);
int currentSum = 0;
int totalSum = 0;
ListItem[] amountItems = new ListItem[amounts.Length];
for (int i = 0; i < amounts.Length; i++)
{
if (int.TryParse(amounts[i], out currentSum))
{
totalSum += currentSum;
}
amountItems[i] = amounts[i];
}
listBox.Items.AddRange(amountItems);
TotalAmountlabel.Text = string.Format("{0}", totalSum);
You can also datasource to bind the list. Please go through below MSDN references once atleast to understand security cautions:
ListItem
ListBox
private void write(object sender, EventArgs e)
{
FileStream outputFileStream = new FileStream("test.txt", FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(outputFileStream);
Random r = new Random();
for (int i = 0; i < dataGridView1.RowCount; i++)
{
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
double d = 0;
Double.TryParse(Convert.ToString(dataGridView1.Rows[i].Cells[j].Value), out d);
writer.Write(d + "\t");
}
writer.Write("\n");
}
writer.Close();
outputFileStream.Close();
}
So this is the method to write the text file. It works fine because I have opened it successfully with an Excel. I even tried to copy and paste it and it would work. Now the problem is.....
private void read(object sender, EventArgs e)
{
char TAB = '\t';
char NEWLINE = '\n';
FileStream inputFileStream = new FileStream("test.txt", FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(inputFileStream);
string line;
string[] fields;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
line = reader.ReadLine();
fields = line.Split(TAB, NEWLINE);
dataGridView1.Rows[i].Cells[j].Value = fields;
}
}
inputFileStream.Close();
reader.Close();
}
However when I read the file back onto a DataGridView it does not work properly. Now this is the exact text file that I wrote on my code. Instead what happens is that it is displayed on only 1 row. How do I get back the amount of columns and rows from what the user entered? I prefer keeping it a text file.
I have used default properties for my dataGridView1
RowCount returns displayed rows count. So here it returns 1 because you are displaying one row and it's empty:
for (int i = 0; i < dataGridView1.RowCount; i++)
Instead of this, you should create new rows and add it to the Rows collection like this:
// Use File.ReadAllLines, it's easier
string[] lines = File.ReadAllLines("test.txt");
foreach(line in lines)
{
var text = line.Split('\t','\n');
dataGridView1.Rows.Add(text);
}
System.IO.StreamReader file = new System.IO.StreamReader("yourfile.txt");
string[] columnnames = file.ReadLine().Split(' ');
DataTable dt = new DataTable();
foreach (string c in columnnames)
{
dt.Columns.Add(c);
}
string newline;
while ((newline = file.ReadLine()) != null)
{
DataRow dr = dt.NewRow();
string[] values = newline.Split(' ');
for (int i = 0; i < values.Length; i++)
{
dr[i] = values[i];
}
dt.Rows.Add(dr);
}
file.Close();
dataGridView1.DataSource = dt;
try this one with your own delimiters i.e. \t and \n. And First try to search your problems on internet before posting queries and try to solve them by yourself. I am not giving full code.
im having a little trouble with a binary writer issue, my program is set to load SHIFT-JIS characters, and write in the same encoding, but it returns a file bigger in size :s although i remove several Characters, here is a sample of the files before and after writing, these are the open and save codes:
private void Openbtn_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
textBox1.Text = "";
menuItem12.Text = "file type is: ";
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open File";
ofd.Filter = "All Files (*.*)|*.*";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
menuItem13.Enabled = true;
menuItem14.Enabled = true;
menuItem15.Enabled = true;
path = ofd.FileName;
BinaryReader br = new BinaryReader(File.OpenRead(path), Encoding.GetEncoding("SHIFT-JIS"));
foreach (char mychar in br.ReadChars(4)) menuItem12.Text += mychar;
if (menuItem12.Text != "file type is: TXTD")
{
MessageBox.Show("This is not a TXTD file...", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
MessageBox.Show("File opened Succesfully!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
br.BaseStream.Position = 0x8;
int Pntrnum = br.ReadInt16();
menuItem11.Visible = true;
menuItem11.Text = Pntrnum.ToString();
List<int> offsets = new List<int>();
br.BaseStream.Position = 0x10;
for (int i = 0; i < Pntrnum; i++)
{
offsets.Add(br.ReadInt32());
}
Dictionary<int, string> values = new Dictionary<int, string>();
for (int i = 0; i < offsets.Count; i++)
{
int currentOffset = offsets[i];
int nextOffset = (i + 1) < offsets.Count ? offsets[i + 1] : (int)br.BaseStream.Length;
int stringLength = (nextOffset - currentOffset - 1);
br.BaseStream.Position = currentOffset;
var chars = br.ReadChars(stringLength);
values.Add(currentOffset, new String(chars));
}
foreach (int offset in offsets)
{
listView1.Items.Add(offset.ToString("X")).SubItems.Add(values[offset]);
}
br.Close();
br = null;
}
}
ofd.Dispose();
ofd = null;
}
private void Savebtn_Click(object sender, EventArgs e)
{
BinaryWriter bw = new BinaryWriter(File.OpenWrite(path));
int number_pointers = Convert.ToInt32(menuItem11.Text);
Encoding enc = Encoding.GetEncoding("SHIFT-JIS");
bw.BaseStream.Position = 0x10;
int curr_pointer = 16 + number_pointers * 4;
//pointers writing
for (int i = 0; i < number_pointers; i++)
{
bw.Write(curr_pointer);
curr_pointer += enc.GetByteCount(listView1.Items[i].SubItems[1].Text) + '\0';
}
for (int i = 0; i < number_pointers; i++)
{
bw.Write(enc.GetBytes(listView1.Items[i].SubItems[1].Text + '\0'));
}
bw.Flush();
bw.Close();
bw = null;
}
i truly appreciate your help.
Looking inside the "After" file in the Samples.zip you provided seems to indicate that you're outputting the same string twice, or something...
I would suggest you start with smaller files to test on, and to use the debugger to step in.
Actually, having tried that myself, i think i found out what's going on: you're using "nextOffset - currentOffset" as a stringLength, but it's actually a BYTE-Length... The char array will end (have a '\0' inside it) after only half this number, and when outputting you'll also get the second half, that you haven't yet read.
[Edit]
Posting the code from my comment below, for better formatting:
int index;
for (index = 0; index < stringLength; ++index) if (chars[index] == '\0') break;
if (index < stringLength) {
char[] relevantPart = new char[index];
Array.Copy(chars, relevantPart, index);
chars = relevantPart;
}