我需要以編程方式填充DataGridView。數據庫的列是固定的,行取決於列表的大小。其實我有List<MyCustomClass>
,我需要用該列表填寫數據。如何以編程方式填充DataGridView
目前,我這樣做:
public Constructor()
{
InitializeComponent();
dataGridViewFiles.AutoGenerateColumns = false;
dataGridViewFiles.ColumnCount = 3;
dataGridViewFiles.Columns[0].Name = "File Name";
dataGridViewFiles.Columns[1].Name = "Total Documents";
dataGridViewFiles.Columns[2].Name = "Total Pages";
}
Public LoadDGV()
{
for (int i = 0; i < nTotalInputFiles; i++)
{//add code here for adding rows to dataGridviewFiles
DataGridViewRow tempRow = new DataGridViewRow();
DataGridViewCell cellFileName = new DataGridViewTextBoxCell();
cellFileName.Value = selectedProject.InputFiles[i].FileName;
tempRow.Cells.Add(cellFileName);
DataGridViewCell cellDocCount = new DataGridViewTextBoxCell();
cellDocCount.Value = selectedProject.InputFiles[i].DocCount.ToString();
tempRow.Cells.Add(cellDocCount);
DataGridViewCell cellPageCount = new DataGridViewTextBoxCell();
cellPageCount.Value = selectedProject.InputFiles[i].PageCount.ToString();
tempRow.Cells.Add(cellPageCount);
tempRow.Tag = selectedProject.InputFiles[i].Id;
dataGridViewFiles.Rows.Add(tempRow);
}
但上面的代碼中的某些時間不完美。那麼還有其他方法嗎?或有任何改善以上的建議?
你看到了什麼錯誤?或者你看到了什麼結果? – Tim