I am using C# and I am creating an ArrayList called “importControlKeys” which creates fine. However, I can’t for the life of my figure out a way to loop through the arrayList and pick out the values in the ArrayList for use in later code.
I know I’m missing something easy, but what is the syntax to extract a value from an ArrayList. I was hoping it was somethign like importControlKeys[ii].value in my code below, but that isn’t working.
I’ve searched on these boards and can’t find the exact solution, though I’m sure it’s easy. msot of the solutions say to re-write as a List but I have to believe there is a way to get data out of an array list without re-writing as a List
private void button1_Click(object sender, EventArgs e)
{
ArrayList importKeyList = new ArrayList();
List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
foreach (DataGridViewRow row in grd1.Rows)
{
if (Convert.ToBoolean(row.Cells[Del2.Name].Value) == true)
{
rows_with_checked_column.Add(row);
importKeyList.Add(row.Cells[colImportControlKey.Name].Value);
//string importKey = grd1.Rows[grd1.SelectedCells[0].RowIndex].Cells[0].Value.ToString();
//grd1.ClearSelection();
//if (DeleteImport(importKey))
// LoadGrid();
}
}
for (int ii = 0; ii < rows_with_checked_column.Count; ii++)
{
//grd1.ClearSelection();
string importKey = importKeyList[ii].value; //ERRORS OUT
if (DeleteImport(importKey))
LoadGrid();
// Do what you want with the check rows
}
}
Not sure why you are using an ArrayList but if you need to loop thru it you could do something like this this
if an element is not convertible to the type, you’ll get an InvalidCastException. In your case, you cannot cast boxed int to string causing an exception to be thrown.
or you do a for loop