I am trying to add values from Win32_SystemDriver into a DataGridView, I it will load until it finishes. When it ends up loading all the data it will change all the rows for the last value from the query. This is the code:
ObjectQuery query8 = new ObjectQuery("SELECT * FROM Win32_SystemDriver");
ManagementObjectSearcher searcher8 =
new ManagementObjectSearcher(scope, query8);
foreach (ManagementObject queryObj in searcher8.Get())
{
DataTable dt = new DataTable();
dt.Columns.Add("Descrição");
foreach (DataGridViewRow oItem in dataGridView1.Rows)
{
dt.Rows.Add(new object[] { queryObj["Description"] });
}
dataGridView1.DataSource = dt;
}
What it's wrong about this code? Why after loading every data it will change every row to the last value from the query?
This is how I solve it by myself:
ObjectQuery query8 = new ObjectQuery("SELECT * FROM Win32_SystemDriver");
ManagementObjectSearcher searcher8 =
new ManagementObjectSearcher(scope, query8);
DataTable dt = new DataTable();
dt.Columns.Add("Descrição");
foreach (ManagementObject queryObj in searcher8.Get())
{
dt.Rows.Add(new object[] { queryObj["Description"] });
}
dataGridView1.DataSource = dt;