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.
Related
I want to remove a specific object from an array, put it in a smaller array without getting out of range. This is what I've tried but it won't work.
Skateboard[] newSkateboard = new Skateboard[_skateboards.Length - 1];
for (int i = 0; i < _skateboards.Length; i++)
{
if (skateboard.Code != _skateboards[i].Code)
{
newSkateboard[i] = _skateboards[i];
}
}
Sure.
var j = 0;
for (int i = 0; i < _skateboards.Length; i++)
{
if (skateboard.Code != _skateboards[i].Code)
{
newSkateboard[j] = _skateboards[i];
j = j + 1;
}
}
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.
I am using a data grid, but the values do not display as I would like them to. My current code is below, how would I go about inverting the rows?
string[] strOutput = strLine.Split('_', ',','=');
int totalRows = Convert.ToInt16(strOutput[4]);
int totalCols = Convert.ToInt16(strOutput[5]);
int itemIndex = 8;
for (int i = 0; i < totalCols; i++){
dataGridView1.Columns.Add("Col", "Col");
}
dataGridView1.Rows.Add(totalRows);
for (int i = 0; i < totalRows; i++) {
for (int j = 0; j < totalCols; j++) {
dataGridView1.Rows[i].Cells[j].Value = strOutput[itemIndex];
itemIndex += 2;
}
}
dataGridView1.Visible = true;
To invert i.e. reverse DataGridViewRows you can use this:
void ReverseDGVRows(DataGridView dgv)
{
List<DataGridViewRow> rows = new List<DataGridViewRow>();
rows.AddRange(dgv.Rows.Cast<DataGridViewRow>());
rows.Reverse();
dgv.Rows.Clear();
dgv.Rows.AddRange(rows.ToArray());
}
If you only need to do it once you could instead either:
loop over the lines of the source file in reverse
or instead of Adding the rows (to the end) Insert at the top:
dtnew.Rows.Insert(0, currentDataRowView.Row);
Inverting rows:
"DataGridView.Rows".- will give you "DataGridViewRowCollection"
Iterate the collection in reverse order and create a new datatable. (for loop from max size to zero)
Assign the new datatable to datagridview source.
This rough code written in note pad for your idea. I do not have IDE now.
DataGridViewRowCollection dgRowColllection = dataGridView1.Rows;
DataTable dtnew = new DataTable();
for(i = dataGridView1.RowCount; i < 1 ; i--)
{
DataRowView currentDataRowView = dgRowColllection[i].Row;
dtnew.Rows.Add(currentDataRowView.Row);
}
dataGridView1.source = dtnew;
Can't comment on Pavan's answer because I don't have 50 reputation, but are you getting the error because the loop should be something like:
int totalRows = Convert.ToInt16(strOutput[4]);
int totalCols = Convert.ToInt16(strOutput[5]);
int itemIndex = 8;
for (int i = 0; i < totalCols; i++)
{
dataGridView1.Columns.Add("Col", "Col");
}
dataGridView1.Rows.Add(totalRows);
for (int i = 0; i < totalRows; i++)
{
for (int j = 0; j < totalCols; j++)
{
dataGridView1.Rows[i].Cells[j].Value = strOutput[itemIndex];
itemIndex += 2;
}
DataGridViewRowCollection dgRowColllection = dataGridView1.Rows;
DataTable dtnew = new DataTable();
for (i = dataGridView1.Items.Count; i < 1; i--)
{
DataRowView currentDataRowView = dgRowColllection[i].Row;
dtnew.Rows.Add(currentDataRowView.Row);
}
dataGridView1.DataSource = dtnew;
}
dataGridView1.Visible = true;
}
If somebody could please help solve the following issue:
System.ArgumentOutOfRangeException : Index was out of range. Must be
non-negative and less than the size of the collection. Paarameter name
: index
The code:
for (int i = 0; i <= dataGridView2.Rows.Count ; i++)
{
for (int j = 0; j <= dataGridView3.Rows.Count; j++)
{
if (!string.IsNullOrEmpty(dataGridView2.Rows[i].Cells["supplier_name"].Value as string) && !string.IsNullOrEmpty(dataGridView3.Rows[j].Cells["brands_supplier"].Value as string))
{
if (dataGridView2.Rows[i].Cells["supplier_name"].Value.ToString() == dataGridView3.Rows[j].Cells["brands_supplier"].Value.ToString())
{
dataGridView2.Rows[i].Cells["brands_name"].Value += dataGridView3.Rows[j].Cells["brands_nume"].Value + " ";
}
}
else
{
break;
}
}
}
You try to access an element of your datagrid which doesn't exist.
You have to set your for condition to
for (int i = 0; i < dataGridView2.Rows.Count ; i++)
and
for (int j = 0; j < dataGridView3.Rows.Count; j++)
don't use <= because the index of dataGridView.Rows[] is 0 based.
For example, if your datagrid contains 3 elements you can reach them with:
var row1 = dataGrid.Rows[0]
var row2 = dataGrid.Rows[1]
var row3 = dataGrid.Rows[2]
And you try to access
var row4 = dataGrid.Rows[3] // Error because this item doesn't exist (System.ArgumentOutOfRangeException)
also, but this item doesn't exist
I think problem in cycle
for (int i = 0; i <= dataGridView2.Rows.Count ; i++)
You run one more time
use
for (int i = 0; i < dataGridView2.Rows.Count ; i++)
I am creating a program that holds customers details and sends them an email alerting them of special offers. I am having trouble when I export the email data as I want to store it in the sendto.text field separating each address with a comma, How can I get the data from each row of the datagrid and can it be stored and separated with a comma in a textfield?.
Hope this makes sense. Thanks
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
sendto.text = dataGridView1.Rows[i].Cells[3].Value.ToString());
}
Like this?:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
sendto.text += dataGridView1.Rows[i].Cells[3].Value.ToString() + (i < (dataGridView1.Rows.Count-1) ? "," : "");
}
you can add comma from second emailid onwards.
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if(i>0)
sendto.text +=","+ dataGridView1.Rows[i].Cells[3].Value.ToString());
else
sendto.text = dataGridView1.Rows[i].Cells[3].Value.ToString());
}
StringBuilder stremail = new StringBuilder();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
stremail.Append(dataGridView1.Rows[i].Cells[3].Value.ToString() + ",");
}
int stringWithoutLastComma = --stremail.Length;
sendto.text = stremail.ToString(0,stringWithoutLastComma);
You can:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
sendto.text += string.Format("{0},", dataGridView1.Rows[i].Cells[3].Value.ToString());
}