I have a datagrid and its datasource is a select from a table on a database.
Once I read the table and show the data on my grid, the rows in dababase are deleted (but aaall my data is shown on my grid). After some changes, and add and delete information, I need to save these new rows in a new table!
I'm trying to read my datagrid but I don't know how to do it.
This is my code:
//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++;
}
Any idea?
Issue:
Problem can be seen here in this loop:
foreach (DataRow renglon in DivDup.dtgGrid.rows)
{
}
This can be done like this:
foreach (DataRow renglon in ds.Tables[0].Rows)
{
//here you can use renglon
}
Here is how you could traverse through datagrid:
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();
}
}
Please note above code is not tested.