I'm currently trying to write a 2d int array to file. I've gotten it to write the data to file but it all appears on the same line.
Currently its appearing like this:
15101219816911171076171411881213567131441415101069101413878101181210156976139188711571210891267910
But I want it to appear like this:
15,10,12,19,8
16,9,11,17,10
7,6,17,14,11
8,8,12,13,5
6,7,13,14,4
1,4,15,10,10
6,9,10,14,13
8,7,9,10,11
8,12,10,15,6
9,7,6,13,9
18,8,7,11,5
7,12,10,8,9
12,6,7,9,10
This is my code.
{
StreamWriter outputfile;
File.WriteAllText("ClosingStock.Txt", string.Empty);
outputfile = File.AppendText("ClosingStock.Txt");
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
outputfile.Write(Global_Stock[i , j]);
}
}
outputfile.Close();
}
}
Any help with this problem would be much appreciated. Thanks.
{
StreamWriter outputfile;
string comma = "";
File.WriteAllText("ClosingStock.Txt", string.Empty);
outputfile = File.AppendText("ClosingStock.Txt");
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
outputfile.Write(comma);
outputfile.Write(Global_Stock[i , j]);
comma = ",";
}
comma = System.Environment.NewLine);
}
outputfile.Close();
}
You should first create a string to write into a file and then write it at once
StringBuilder sb = new StringBuilder();
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
sb.Append(Global_Stock[i , j]);
sb.Append(",")
}
}
File.WriteAllText("ClosingStock.Txt",sb.ToString());
Just add a new line after each inner loop:
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
outputfile.Write(Global_Stock[i , j]);
}
outputfile.Write(Environment.NewLine);
}
If I had to guess you should switch StockColumns with StockRows in your loops to have the output you want.
Also, see #Mihir Dave for better implementation of writing a string to a file.
I'm coding like below but it works incorrect.It perform(plus and delete) only 2->3 rows if data has 5->6 duplicate data.
Update and It works
for (int i = 0; i < dataGridView1.RowCount - 1; i++) //compare data
{
var Row = dataGridView1.Rows[i];
string abc = Row.Cells[1].Value.ToString() + Row.Cells[2].Value.ToString().ToUpper();
// MessageBox.Show(abc);
for (int j = i + 1; j < dataGridView1.RowCount; j++)
{
var Row2 = dataGridView1.Rows[j];
string def = Row2.Cells[1].Value.ToString() + Row2.Cells[2].Value.ToString().ToUpper();
if (abc == def)
{
Row.Cells[5].Value = Convert.ToDouble(Row.Cells[5].Value.ToString()) + Convert.ToDouble(Row2.Cells[5].Value.ToString());
dataGridView1.Rows.Remove(Row2);
j--;
}
}
}
This should do the trick for you:
for (int i = 0; i < dataGridView1.RowCount - 1; i++) //compare data
{
var Row = dataGridView1.Rows[i];
string abc = Row.Cells[0].Value.ToString() + Row.Cells[1].Value.ToString().ToUpper();
for (int j = i+1; j < dataGridView1.RowCount; j++)
{
var Row2 = dataGridView1.Rows[j];
string def = Row2.Cells[0].Value.ToString() + Row2.Cells[1].Value.ToString().ToUpper();
if (abc == def)
{
Row.Cells[2].Value = (int)Row.Cells[2].Value + (int)Row2.Cells[2].Value;
dataGridView1.Rows.Remove(Row2);
j--;
}
}
}
You basically need to keep track of j variable as you remove rows from the collection.
If you're a fan of LINQ and don't mind a bit of a convoluted code, here is another approach:
for (int i = 0; i < dataGridView1.RowCount; i++) //compare data
{
var R = dataGridView1.Rows[i];
var V = R.Cells[0].Value.ToString() + R.Cells[1].Value.ToString().ToUpper();
var DupRows = dataGridView1.Rows.Cast<DataGridViewRow>().Skip(i + 1).
Where(r => r.Cells[0].Value.ToString() + r.Cells[1].Value.ToString().ToUpper() == V);
R.Cells[2].Value = (int)R.Cells[2].Value + DupRows.Sum(r => (int)r.Cells[2].Value);
foreach (var DupRow in DupRows)
DupRow.Tag = "Del";
}
for (int i = 0; i < dataGridView1.RowCount; i++)
{
var R = dataGridView1.Rows[i];
if (R.Tag?.ToString() == "Del")
{
dataGridView1.Rows.Remove(R);
i--;
}
}
As a word of advice, this kind of stuff is handled far more easily in the back-end. Whatever your DataGridView is bound to, be it a DataTable or a generic collection, you should implement duplicate removal there instead of directly playing with DataGridView cells.
ArrayList myArrayList = new ArrayList();
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
myArrayList.Add(dt.Rows[i][j].ToString());
}
}
I want to send a mail to all mail id's which I am storing in array list. How to bind that total array list to a Textbox.
If I understand your question correctly, this code may help you
foreach(object item in myArrayList)
{
TextBoxId.Text += item.ToString();
}
Or you can forget your arrayList, populate TextBox directly while looping in table.
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
//myArrayList.Add(dt.Rows[i][j].ToString());
TextBoxId.Text += dt.Rows[i][j].ToString();
}
}
ArrayList myArrayList = new ArrayList();
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
myArrayList.Add(dt.Rows[i][j].ToString());
}
}
StringBuilder strb = new StringBuilder();
foreach (var item in myArrayList)
{
strb.Append(item + ";");
}
You can bind a strb.ToString() to your text box or instead of creating an ArrayList you can directly append email ids from your DataTable to StringBuilder and bind it to your text box.
I have been trying to separate two numbers using "." but in Excel it sets ",". How can I fix it?
int col= 2;
for (int i = 0; i < proc; i++)
{
for (int j = 0; j < mac; j++)
{
wse.Cells[1, col] = (i + 1).ToString()+"." + (j + 1).ToString();
col++;
}
}
I'm using this code for sum datagridview's cells.
int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToInt32 (dataGridView1.Rows[i].Cells[1].Value);
}
int count_row = dataGridView1.Rows.Count;
label1.Text = (sum.ToString());
The code works well if I just use numbers. However, when I use commas, e.g., 1,34 and 2,20, it says that format exception was unhandled.
it is quite simple, juste do it with decimals.
Decimal sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.Decimal(dataGridView1.Rows[i].Cells[1].Value);
}
int count_row = dataGridView1.Rows.Count;
label1.Text = (sum.ToString());
Modify the line as shown below:
sum += Convert.ToInt32 (dataGridView1.Rows[i].Cells[1].Value.ToString().Replace(",",""));
and it should work with commas as well.
You can do like this:
double sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += double.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString().Replace(',','.'));
}
int count_row = dataGridView1.Rows.Count;
label1.Text = (((int)sum).ToString());