2014-05-12 3 views
0

데이터 릿이 있는데 그 데이터 소스는 데이터베이스의 테이블에서 선택합니다.datarows가있는 배열

테이블을 읽고 표에 데이터를 표시하면 데이터베이스의 행이 삭제됩니다. 그러나 내 데이터는 내 표에 나타납니다. 몇 가지 변경 사항을 적용한 후 정보를 추가 및 삭제하면 이러한 새 행을 새 테이블에 저장해야합니다.

내 DataGrid를 읽으려고하는데 어떻게해야할지 모르겠다.

//fill my grid 
SqlCeConnection conn = new SqlCeConnection(); 
StringBuilder sbQuery = new StringBuilder(); 
SqlCeCommand cmd = new SqlCeCommand(); 
conn.ConnectionString = ("Data Source =" + 
    (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) + 
    "\\MOB.sdf;Password=acwef;Persist Security Info=False;"); 
conn.Open(); 
DataSet ds = new DataSet(); 
SqlCeDataAdapter da = new SqlCeDataAdapter("SELECT * FROM TABDATAREG ", conn); 
da.Fill(ds, "TABDATAREG "); 
DivDup.dtgGrid.DataSource = ds.Tables[0].DefaultView; 


//and here, I'm trying to read 
foreach (DataRow renglon in DivDup.dtgGrid.rows)//<--------- BUT IT DOESNT WORK 
{ 
    arregloGrid[i, 0] = DivDup.dtgGrid[i, 0].ToString();//num_cía 
    arregloGrid[i, 1] = DivDup.dtgGrid[i, 1].ToString();//num_tipo 
    arregloGrid[i, 2] = DivDup.dtgGrid[i, 2].ToString();//num_activo 
    arregloGrid[i, 3] = DivDup.dtgGrid[i, 3].ToString();//sub_num_act 
    //posicion i 
    arregloGrid[i, 4] = DivDup.dtgGrid[i, 4].ToString();//marca 
    arregloGrid[i, 5] = DivDup.dtgGrid[i, 5].ToString();//modelo 
    arregloGrid[i, 6] = DivDup.dtgGrid[i, 6].ToString();//serie 
    arregloGrid[i, 7] = DivDup.dtgGrid[i, 7].ToString();//descripción 
    arregloGrid[i, 8] = DivDup.dtgGrid[i, 8].ToString();//porcentaje 
    arregloGrid[i, 9] = DivDup.dtgGrid[i, 9].ToString();//costo/importe 
    i++; 
} 

어떤 생각 :

이 내 코드?

+0

'그렇지만 작동하지 않습니다.'라는 것은 무엇을 의미합니까? – gunr2171

+0

MVS는 "오류 'System.Windows.Forms.DataGrid'는 'System.Windows.Forms.DataGrid'가 'System.Windows.Forms.DataSource'행에 대해 '행을 확장 할 수 없습니다.'라는 메시지를 표시합니다. DataGrid '\t " – PETTA

+0

이 오류 메시지와 같은 자세한 정보를 포함하려면 게시물을 편집하십시오. 가능한 경우 영어 버전을 작성하십시오. – gunr2171

답변

2

문제 :

  1. 당신은 DataGrid에 DataRow를 찾고 있습니다.
  2. 루프 내에서 DataRow를 사용하고 있지 않습니다.

문제는이 루프에서 여기에서 볼 수있다 :

:

여기
foreach (DataRow renglon in ds.Tables[0].Rows) 
{ 
     //here you can use renglon 
} 

는 데이터 그리드을 통해 통과 할 수있는 방법입니다 : 이것은 다음과 같이 수행 할 수 있습니다

foreach (DataRow renglon in DivDup.dtgGrid.rows) 
{ 

} 

int rowCount = ds.Tables[0].Rows.Count; 
int colCount = ds.Tables[0].Columns.Count; 

for(int row = 0; row < rowCount; row++) 
{ 
    for(int col = 0; col < colCount; col++) 
    { 
     arregloGrid[row, col] = DivDup.dtgGrid[row, col].ToString();     
    } 
} 

P 위의 코드는 테스트되지 않았습니다.

+0

** 작품 ** @ 하산 니사르, ** 규칙! 정말 고맙습니다!** – PETTA