Wednesday, August 18, 2010

I have 10 datagrid views in a form i want to write a loop for it in c#.net.How do i wirte the code.?

I have written in this way


for(i=0;i%26lt;10;i++)


{


(DataGridView+i).DataSource=Da...


(DataGridView+i).Datamember=Da...


}


But for this iam getting a error .








Pls tell me how to write the code for the following.I have 10 datagrid views in a form i want to write a loop for it in c#.net.How do i wirte the code.?
Sounds to me like you want a list of DataGridViews, so first you have to create the list.





Note you will need to declare System.Collections.Generic to have access to List.





using System.Collections.Generic;





dataGridViews = new List%26lt;DataGridView%26gt;();








Next you need to populate the list





dataGridVIews.Add( new DataGridView );





then to loop through it, simply do this:





foreach( DataGridView mGrid in dataGridViews )


{


mGrid.DataSource=blah;


mGrid.DataMember=blah;


}





This will loop through all of them in the list. I'm pretty sure you can also use your method and just use dataGridViews[i].DataSource=blah;





Hopefully that accomplishes what you needed.I have 10 datagrid views in a form i want to write a loop for it in c#.net.How do i wirte the code.?
for(i=0;i%26lt;10;i++)


{


(DataGridView+i+).DataSource=Da...


(DataGridView+i+).Datamember=Da...


}


The way it was before does not get that i is variable.


Hope this will work.

No comments:

Post a Comment