2016-12-04 3 views
0

이 양식을 사용하여 ID를 지정하고 강조 표시된 버튼을 클릭하면 데이터베이스에서 가져온 DataGridview에 일부 정보가 표시됩니다. First Formdatagridview의 변경 사항이 데이터베이스에 저장되지 않았습니다.

버튼의 코드는 여기에 있습니다 : 우리가 데이터베이스에서 무언가를 얻을

private void kerkoButton_Click(object sender, EventArgs e) 
    { 
     try 
     {    
      Konektimi(); 
      string query = "SELECT * FROM Vizita WHERE [email protected]"; 

       using (SqlCommand command = new SqlCommand(query, conn)) 
       { 

        command.Parameters.AddWithValue("@pacientiId",pacientiTextBox.Text);            
        conn.Open(); 
        using (SqlDataReader reader = command.ExecuteReader()) 
        { 
         if (reader.HasRows) 
         { 
          DataTable dt = new DataTable(); 
          dt.Load(reader); 
          forma.vizitaDataGridView.DataSource = dt; 
          forma.ShowDialog(); 
         } 
         else 
         { 
          MessageBox.Show("Nuk ka të dhëna për këtë pacient"); 
         } 
        } 
        conn.Close(); 
       } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 

Suposing 우리가이 얻을 : Second Form,with the results

이 클릭 한 후 열 수있는 새로운 형태의 kerkoButton.

지금 문제는 다음과 같습니다. 행 (또는 일부 행)을 편집 할 때 저장을 클릭하면 변경 사항이 데이터베이스에 저장되지 않습니다. 어떻게해야합니까? 이 DataGridform에 SaveButton을 만들 수 있습니까? 그렇다면 어떻게됩니까?

DataRowView dataRowView = yourDataGridView.CurrentRow.DataBoundItem as DataRowView; 
DataRow[] rowsToUpdate = new DataRow[] { dataRowView.Row }; 

SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Vizita", con); 
SqlCommandBuilder builder = new SqlCommandBuilder(adapter); 
adapter.Update(rowsToUpdate); 

또는, 당신은 모든 편집 행을 업데이트 할 경우 :

답변

0

는 "how to update row in DataGridView?"

당신은 다음처럼 DataGridView에의 재산 CurrentRow를 사용하여 편집 된 행에 액세스 할 수 있습니다에서보세요 한 번 플래그를 만들고 PropertyChanged 이벤트를 사용하여 속성을 변경할 때마다 true으로 설정해야합니다.

"저장"을 클릭하면 모든 행을 반복하고 편집 된 내용을 rowsToUpdate에 추가합니다.

+0

데이터 바인딩을 올바르게 사용하면 DataTable에서 자체 변경 내용을 추적하므로 필요한 경우 어댑터에서 Update를 호출하면됩니다. 격자 행을 반복하는 경우 잘못 처리하고 있습니다. – Crowcoder

관련 문제