这是我以编程方式添加dataGridview的方式
DataGridView grid = new DataGridView();
grid.Location = new Point(5, 30);
grid.Width = container.Width - 10;
grid.Height = container.Height - 40;
grid.Name = "Section " + countSections.ToString();
grid.Columns.Add("StudentID", "StudentID");
grid.Columns.Add("StudentName","StudentName");
container.Controls.Add(grid);
现在我们假设我们有5个dataGridView
//I have list of students
string [] names = {"name","name2","name3","name4","name5","name6"};
//This is how I indentify DGV
foreach (Control grid in Pan.Controls)
{
if (grid is DataGridView)
{
DataGridView myGrid = grid as DataGridView;
//here how can i divide those names in different datagridview
//I use this syntax to add rows
mygrid.Rows.Add(studentId,names);
}
}
此模块为您提供了一个想法,我称之为自动分段,对于逻辑和想法很抱歉
答案 0 :(得分:1)
您需要遍历names
数组,方法是获取其枚举数amnd,然后在每次将名称添加到DataGrid时对其调用MoveNext。
//I have list of students
string [] names = {"name","name2","name3","name4","name5","name6"};
// enumerator
var nameEnumerator = names.GetEnumerator();
//This is how I indentify DGV
foreach (Control grid in Pan.Controls)
{
if (grid is DataGridView)
{
DataGridView myGrid = grid as DataGridView;
// check if we still have names
if (nameEnumerator.MoveNext())
{
mygrid.Rows.Add(studentId, nameIterator.Current);
}
}
}
答案 1 :(得分:0)
//I have list of students
string [] names = {"name","name2","name3","name4","name5","name6"};
//This is how I identify DGV
int nameIndex=0;
foreach (Control grid in Pan.Controls)
{
if (grid is DataGridView)
{
DataGridView myGrid = grid as DataGridView;
//here how can i divide those names in different datagridview
//I use this syntax to add rows
mygrid.Rows.Add(studentId,names[nameIndex++]);
}
}