数据行的数组

时间:2014-05-12 17:19:22

标签: c# arrays datagrid pocketpc

我有一个datagrid,它的数据源是从数据库中的表中选择的。

读取表格并在网格上显示数据后,dababase中的行将被删除 (但是我的数据显示在我的网格上)。经过一些更改,添加和删除信息后,我需要将这些新行保存在新表中!

我试图阅读我的数据网格,但我不知道该怎么做。

这是我的代码:

//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++;
}

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

问题:

  1. 您正在寻找DataGrid中的DataRow。
  2. 您没有在循环中使用DataRow。
  3. 在这个循环中可以看到问题:

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

    这可以这样做:

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

    以下是遍历数据网格的方法

    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();                  
        } 
    } 
    

    请注意以上代码未经过测试。

相关问题