4
To popular my grid the values are not aligned correctly, as the image below.
This is my Load Form code:
DatabaseRepository db = new DatabaseRepository();
var colunas = db.GetColumnsName();
var resultado = db.Get();
DataTable dataTable = new DataTable();
foreach (IDictionary<string, object> columns in colunas)
{
var coluna = columns.Values.FirstOrDefault().ToString();
dataTable.Columns.Add(coluna);
foreach (IDictionary<string, object> rows in resultado)
{
foreach (var pair in rows)
{
if (pair.Key == coluna)
{
DataRow dataRow = dataTable.NewRow();
dataRow[coluna] = pair.Value;
dataTable.Rows.Add(dataRow);
}
}
}
}
dataGridView1.DataSource = dataTable;
And that access code to my database:
public class DatabaseRepository
{
private Connection _Connection = new Connection();
public DatabaseRepository()
{
}
public List<dynamic> Get()
{
using(var Sql = _Connection.GetConnection())
{
Sql.Open();
var resultado = Sql.Query<dynamic>("SELECT * FROM [seguranca].[tb_acao]").ToList();
return resultado;
}
}
public List<dynamic> GetColumnsName()
{
using(var Sql = _Connection.GetConnection())
{
Sql.Open();
string query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + "tb_acao" +"'";
var resultado = Sql.Query<dynamic>(query).ToList();
return resultado;
}
}
}
The idea is to take this data and columns directly from the database, and when listing concatenate the columns with the values to mount the grid.
Your code is extremely confusing, there’s something there that doesn’t even make sense. Why don’t you populate a list and play on the grid? Datatable is really necessary?
– Jéf Bueno
@LINQ I think Datatable is the only way to popular this Grid, as for why not the list, the idea was to take this data dynamically without having to create a model and type in a list
– William
Your idea is to make any select in the bank and be able to play it for the same Datagrid. This?
– Jéf Bueno
@LINQ that’s right
– William