Possible way to do to with foreach instead of for [closed]

Possible way to do to with foreach instead of for [closed]



I am trying to convert DataGridView columns into an ArrayList. I would like to make this code into a foreach version. I tried to follow this answer but, it doesn't worked for me. With this for method my problem is, when I am deleting a bunch of rows from the datagridview, then it gives an error or my binary search doesn't work well. That's why I want to try with foreach method.


DataGridView


ArrayList


foreach


for


foreach


for (int i = 0 ; i < dataGridView2.Rows.Count; i++)

ListOfPeople[i] = dataGridView2.Rows[i].Cells[0].Value.ToString().Trim();



This question appears to be off-topic. The users who voted to close gave this specific reason:





Show your attempt with foreach and explain what's wrong with it.
– mason
Aug 30 at 19:51


foreach





The code in that example is wrong by the way. There's a syntax error in DataGridView Row row
– Phiter
Aug 30 at 19:51


DataGridView Row row





Where/how are you deleting the rows? With foreach the enumeration (i.e dataGridView2.Rows cannot be changed, by the way
– Camilo Terevinto
Aug 30 at 19:53


foreach


dataGridView2.Rows





"With this for method my problem is, when I am deleting a bunch of rows from the datagridview, then it gives an error or my binary search doesn't work well." - so that's your actual problem. What error do you get?
– stuartd
Aug 30 at 19:53






Non of these answers are helped but I fixed my problem. I removed ArrayList.sort(); and I used ArrayList.ToArray(); instead. Now it's perfectly working!
– Galarist
Aug 30 at 20:18



ArrayList.sort();


ArrayList.ToArray();




4 Answers
4



As an alternative to loops you can try query dataGridView2.Rows with a help of Linq and build ListOfPeople in one go:


dataGridView2.Rows


ListOfPeople


// ArrayList is an obsolete class; let's use List<string>
List<string> ListOfPeople = dataGridView2
.Rows
.OfType<DataGridViewRow>()
.Select(row => row.Cells[0].Value.ToString().Trim())
.ToList();



Why can't you use foreach likewise for any other collection


foreach


foreach (DataGridViewRow row in dataGridView2.Rows)
// code here



The problem with the conversion of for to foreach is what to do with loop index i. Two situations are possible - when you need i, and when you don't need i.


for


foreach


i


i


i



When i is necessary, use Select that passes the index to you, like this:


i


Select


foreach (var p in dataGridView2.Rows.Select((r, i) => new Row = (DataGridViewRow)r, Index = i)
ListOfPeople[p.Index] = p.Row.Cells[0].Value.ToString().Trim();



When i is not necessary, you can use "straight" foreach, or even drop the loop altogether:


i


foreach


ListOfPeople = dataGridView2.Rows.Cast<DataGridViewRow>()
.Select(r => r.Cells[0].Value.ToString().Trim())
.ToList();



The old-school answer to your question (how to use a foreach instead of for) is pretty close to what @Rahul describes. However, the Rows collection was a pre-generic collection (i.e., it's been there since .NET v1.x), and when you foreach over it, you get objects, and not rows. Instead, this works:


foreach


for


foreach (var row in dataGridView2.Rows.Cast<DataGridViewRow>())
// code here



The old school way of keeping track of an index variable (i) is something like:


i


var i = 0;
foreach (var row in dataGridView2.Rows.Cast<DataGridViewRow>())

// your code here
++i;

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế