Windows 양식에서 DataGridView
수동으로 삽입 DataGridViewRows
하여 작성하려고 하므로 코드는 다음과 같습니다.
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dgvArticles);
row.Cells[0].Value = product.Id;
row.Cells[1].Value = product.Description;
.
.
.
dgvArticles.Rows.Add(row);
그러나 다음과 같이 인덱스로 수행하는 대신 열 이름으로 Cell 값을 추가하고 싶습니다.
row.Cells["code"].Value = product.Id;
row.Cells["description"].Value = product.Description;
그러나 그렇게하면 "code"라는 열을 찾을 수 없다는 오류가 발생합니다. 다음과 같이 디자이너에서 DataGridView 열을 설정합니다.
내가 뭘 잘못하고 있니? 내가하고 싶은 일을 어떻게 할 수 있습니까?
따라서 원하는 접근 방식을 수행하려면 다음과 같이 수행해야합니다.
//Create the new row first and get the index of the new row
int rowIndex = this.dataGridView1.Rows.Add();
//Obtain a reference to the newly created DataGridViewRow
var row = this.dataGridView1.Rows[rowIndex];
//Now this won't fail since the row and columns exist
row.Cells["code"].Value = product.Id;
row.Cells["description"].Value = product.Description;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다