에서 DataGridView를을 만들 수 있습니다. 그러나 grid.Rows
카운트는 0입니다. 반면는 프로그래밍 나는 다음과 같은 코드가
, 나는이의 WinForm에 DataGridView
를 만든 다음 그 DataSource
DataTable
에 할당하는 경우 DataGridView.Rows
가 자동으로 추가됩니다.
여기에 누락 된 코드는 무엇입니까? DataGridView.Rows
이 맞나요?
에서 DataGridView를을 만들 수 있습니다. 그러나 grid.Rows
카운트는 0입니다. 반면는 프로그래밍 나는 다음과 같은 코드가
, 나는이의 WinForm에 DataGridView
를 만든 다음 그 DataSource
DataTable
에 할당하는 경우 DataGridView.Rows
가 자동으로 추가됩니다.
여기에 누락 된 코드는 무엇입니까? DataGridView.Rows
이 맞나요?
이 DataGridView
컨트롤을 폼에 프로그래밍 방식으로 추가하면 작동합니다. 아무도 알려지지 않은 이유는 무엇입니까?
DataTable table = new DataTable();
//DataTable is filled with values here...
DataGridView grid = new DataGridView();
// assuming (this) is a reference to the current form
this.Controls.Add(grid);
grid.DataSource = table;
DataTable table = new DataTable();
//DataTable is filled with values here...
DataGridView grid = new DataGridView();
grid.DataSource = table;
또는
foreach (DataColumn column in table.Columns)
{
grid.Columns.Add(column.ColumnName, column.ColumnName);
}
grid.Rows.Add(table.Rows.count);
Int32 i=0;
foreach (DataRow rw in table.Rows)
{
grid.Rows[i].Cell[0].value = rw["col1"].ToString();
i+=1;
}
이 코드
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex == 5)
{
string Task = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
if (Task == "Delete")
{
if (MessageBox.Show("Bạn có chắc chắm muốn xóa không?", "Đang xóa...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
int rowIndex = e.RowIndex;
dataGridView1.Rows.RemoveAt(rowIndex);
dataset.Tables["tbl_students"].Rows[rowIndex].Delete();
sqlAdapter.Update(dataset, "tbl_students");
}
}
else if(Task == "Insert")
{
int row = dataGridView1.Rows.Count - 2;
DataRow dr = dataset.Tables["tbl_students"].NewRow();
dr["id"] = dataGridView1.Rows[row].Cells["id"].Value;
dr["name"] = dataGridView1.Rows[row].Cells["name"].Value;
dr["address"] = dataGridView1.Rows[row].Cells["address"].Value;
dr["phone"] = dataGridView1.Rows[row].Cells["phone"].Value;
dr["email"] = dataGridView1.Rows[row].Cells["email"].Value;
dataset.Tables["tbl_students"].Rows.Add(dr);
dataset.Tables["tbl_students"].Rows.RemoveAt(dataset.Tables["tbl_students"].Rows.Count - 1);
dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 2);
dataGridView1.Rows[e.RowIndex].Cells[5].Value = "Delete";
sqlAdapter.Update(dataset, "tbl_students");
}
else if (Task == "Update")
{
int r = e.RowIndex;
dataset.Tables["tbl_students"].Rows[r]["id"] = dataGridView1.Rows[r].Cells["id"].Value;
dataset.Tables["tbl_students"].Rows[r]["name"] = dataGridView1.Rows[r].Cells["name"].Value;
dataset.Tables["tbl_students"].Rows[r]["address"] = dataGridView1.Rows[r].Cells["address"].Value;
dataset.Tables["tbl_students"].Rows[r]["phone"] = dataGridView1.Rows[r].Cells["phone"].Value;
dataset.Tables["tbl_students"].Rows[r]["email"] = dataGridView1.Rows[r].Cells["email"].Value;
sqlAdapter.Update(dataset, "tbl_students");
dataGridView1.Rows[e.RowIndex].Cells[5].Value = "Delete";
}
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
을 시도 웹 사이트 세부로 이동 : http://laptrinhvb.net/bai-viet/chuyen-de-csharp/Lap-trinh-ung-dung-them---xoa---sua-truc-tiep-tren-DataGridview-(Crub-database-on-DataGridview-use-Cshap)/e95de75579022678.html
이 링크가 질문에 대답 할 수 있지만 여기에 대한 대답의 핵심 부분을 포함하고 참조 용 링크를 제공하는 것이 좋다. 링크 된 페이지가 변경되면 링크 전용 답변이 유효하지 않게 될 수 있습니다. - http://stackoverflow.com/help/how-to-answer –
질문 디스플레이가 무엇입니까? 옳은? – OneFineDay
열을 추가하지 않고 단순히 테이블을 데이터 소스로 지정하면 행이 표시됩니까? – rune711
'AutoGenerateColumns' 속성? – OneFineDay