2011-09-14 9 views
0

DataSet 및 TableAdapter를 사용하여 Datagridview를 채 웁니다. Datagridview는 새로운 레코드를 하단에 삽입 할 수 있습니다. 이제 Datagridview에서 새 레코드의 값을 입력하면 레코드가 자동으로 데이터베이스에 다시 저장되지 않습니다. 새로운 레코드를 다시 데이터베이스에 저장하려면 어떤 이벤트를 처리해야하며 어떤 코드를 작성해야합니까?새 레코드를 Datagridview에서 데이터베이스에 저장합니다.

오전 C#을 사용 중입니다.

+0

gridview에 코드를 채우시겠습니까? (예 경우 게시하십시오) 또는 데이터 소스로 채우시겠습니까? – Carsten

+0

Windows Forms 또는 Web Forms? 데이터를 그리드에 바인딩하는 데 사용하는 코드를 표시 할 수 있습니까? –

+0

DataGridView를 TableAdapter 및 해당 Windows Forms 응용 프로그램으로 채 웁니다. – Gonzalez

답변

0

당신이 TableAdapterUpdate() 메소드를 호출을? 다비드 Piras 당신이 당신의 양식 또는 데이터가 될하려는 경우 저장 버튼을 가질 수 있듯이 각 행으로 저장 - 다음 코드는 로직의이 종류를 넣을 수있는 다양한 장소가 있습니다 MSDN's discussion on the TableAdapter:

try 
{ 
    this.Validate(); 
    this.customersBindingSource.EndEdit(); 

    this.customersTableAdapter.Update(this.northwindDataSet.Customers); 
    MessageBox.Show("Update successful"); 
} 
catch (System.Exception ex) 
{ 
    MessageBox.Show("Update failed"); 
} 

에서 온다 RowValidated 이벤트 핸들러로 논리를 저장할 수 있습니다.

1

당신이 어딘가에 저장 버튼을 넣을 때 사용자가이 같은 메서드를 호출하는 버튼을 클릭 할 수 있습니다 :

private void UpdateDataSet(DataSet dataSet) 
{ 
    // Check for changes with the HasChanges method first. 
    if(!dataSet.HasChanges(DataRowState.Modified)) return; 

    // Create temporary DataSet variable and 
    // GetChanges for modified rows only. 
    DataSet tempDataSet = 
     dataSet.GetChanges(DataRowState.Modified); 

    // Check the DataSet for errors. 
    if(tempDataSet.HasErrors) 
    { 
     // Insert code to resolve errors. 
    } 
    // After fixing errors, update the data source with 
    // the DataAdapter used to create the DataSet. 
    adapter.Update(tempDataSet); 
}