2011-08-03 2 views
0

나는 누군가가 데이터를 변경하는 경우 만약 내가 다시 내 데이터를 업데이트 할 수있는 방법이업데이트 DataGridView에 내가 내의 WinForm 응용 프로그램 DataGridView를에있는

DataSet ds2 = DAL.Display_all(); 
dataGridView1.DataSource = ds2; 
dataGridView1.DataMember = "To_display"; 

같이와 DataSet으로 DAL 클래스에서 데이터를 표시 gridView (DAL 클래스로 다시 샌드위치해야하나요?)

+0

DAL.Display_all()에서 무엇을하고 있습니까? 데이터 집합이 DataGridView로 자동 업데이트됩니다. – Wowa

+0

업데이트 된 행을 가져 와서 데이터베이스를 업데이트하는 방법을 묻는 것 같습니까? – Jethro

+0

나는 연결이 열려있는 SQL 쿼리를 가지고 있으며 DAL에서 dataSet을 반환하면 위에서 언급 한 내용을 수행합니다. –

답변

0

이 시도 :

dataGridView1.ResetBindings(); 

이 목록의 모든 항목을 다시 읽고 표시된 값을 새로 고치 BindingSource에 바인딩 된 컨트롤을 일으킴. MSDN

희망이 도움이됩니다.

0

DataGridView의 각 행을 반복하는 경우 아래 메서드를 사용하여 특정 행의 지정된 Column 값을 가져올 수 있습니다.

private T GetDataGridViewTextBoxValue<T>(int rowIndex, string columnName) 
    { 
     try 
     { 
      return (T)Convert.ChangeType(dataGridViewImport.Rows[rowIndex].Cells[columnName].Value, typeof(T)); 
     } 
     catch (Exception e) 
     { 
      throw new InvalidOperationException(String.Format("Row : {0}, column : {1} could not be cast to {2}", rowIndex, columnName, typeof(T)), e); 
     } 
    } 

그리고 다음과 같은 방법을 사용할 수 있습니다.

for(int i=0;i< ds2.Tables[0].Rows.Count(); i++) 
{ 
    var column1 = GetDataGridViewTextBoxValue<string>(i, "column1"); 
    //Get the rest of the row values. 
    //In your DAL class you must have a Method that updates the row 
    //and then just pass in the values that you want. 
} 
1

DataAdapter.Update 메서드를 사용하십시오.

DataSet을 매개 변수로 전달하십시오.

DataSet.HasChanges을 사용하면 Update 메서드를 호출하기 전에 변경 사항이 있는지 확인할 수 있습니다.

관련 문제