Error list in gridview with foreach [duplicate]

0

I wanted to know how I can display the data in a gridview, I know that I have to return the list but I can not find how, it should be easy but I'm just starting with c #

            DataTable SBTable = new DataTable();

            OleDbConnection connection = new OleDbConnection();
            OleDbCommand command = new OleDbCommand();
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            DataSet dataset = new DataSet();

            try
            {
                connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data source= " + myConnectionString;
                bool ok = System.IO.File.Exists(myConnectionString);
                String qry = "SELECT * FROM information_tool_gen";
                command.Connection = connection;
                command.CommandText = qry;

                adapter.SelectCommand = command;

                command.Connection.Open();
                OleDbDataReader reader = command.ExecuteReader();

                List<tabla> Resultado = new List<tabla>();

                while (reader.Read())
                {
                    Resultado.Add(new tabla(
                        int.Parse(reader["id_global"].ToString()),
                        int.Parse(reader["torque"].ToString()),
                        int.Parse(reader["tol"].ToString()),
                        reader["um"].ToString(),
                        reader["module"].ToString(),
                        reader["batteries"].ToString(),
                        reader["torquemeter"].ToString(),
                        int.Parse(reader["frequency"].ToString()),
                        DateTime.Parse(reader["maint_date"].ToString()),
                        int.Parse(reader["status_id"].ToString()),
                        DateTime.Parse(reader["reg_date"].ToString())
                        ));
                }

                List<tabla> lista = Resultado();
                {
                    foreach (var dato in lista)
                    {
                        DataGridView row = (DataGridViewRow)GridView1.Rows[0].clone();
                        row.cells["id_global"].value = var.id_global;
                        row.cells["torque"].value = var.id_global;
                        row.cells["tol"].value = var.tol;
                        row.cells["um"].value = var.um;
                        row.cells["module"].value = var.module;
                        row.cells["batteries"].value = var.batteries;
                        row.cells["torquemeter"].value = var.torquemeter;
                        row.cells["frequency"].value = var.frequency;
                        row.cells["maint_date"].value = var.maint_date;
                        row.cells["status_id"].value = var.status_id;
                        row.cells["reg_date"].value = var.reg_date;

                        GridView1.Rows.Add(row);
                    }
                }
                if (!reader.IsClosed)
                {
                    reader.Close();
                }
            }
            catch (Exception)
            {
            }
            return SBTable;
        }
    }
}
    
asked by Cesar Gutierrez Davalos 04.05.2017 в 16:08
source

1 answer

0

Good morning, as I can see, you have a list of objects table and if I understand you, you want to return this list and then add it to GridView .

Here I leave the code that you should implement to achieve the task.

//viene el resto de tu código
List<tabla> lista = tuMetodoqueRetornaLista();
foreach(var dato in lista)
{
    DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
    row.Cells["Columna1"].Value = var.dato1;
    row.Cells["Columna1"].Value = var.dato2;
    //agregas todos los campos
    yourDataGridView.Rows.Add(row);
}

Now, if you want to do it in a simpler way, you can from the query pass the OleDbDataReader to a DataTable and then pass it to a GridView

        DataTable SBTable = new DataTable();

        OleDbConnection connection = new OleDbConnection();
        OleDbCommand command = new OleDbCommand();
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        DataSet dataset = new DataSet();

        try
        {
            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data source= " + myConnectionString;
            bool ok = System.IO.File.Exists(myConnectionString);
            String qry = "SELECT * FROM information_tool_gen";
            command.Connection = connection;
            command.CommandText = qry;

            adapter.SelectCommand = command;

            command.Connection.Open();
            OleDbDataReader reader = command.ExecuteReader();
            SBTable.Load(reader);
        }
        catch (Exception)
        {
        }
        return SBTable;
    }
}

}

Now, in the part of your GridView, what you should do is the following:

DataTable dataTable = FuncionDevuelveTabla();
if (dt.Rows.Count > 0)
{
   yourDataGridView.DataSource = dataTable;
   yourDataGridView.DataBind();
}
else
{
   yourDataGridView.DataBind();       
}
    
answered by 04.05.2017 / 16:33
source