Hello. How to set up and work with DataGridView correctly, if the structure of datarow and grid is different? At the moment, I am filling the grid with the Add() method. I will give an example code:

 dgv.Rows.Add(sName, sCount); 

The data to add is taken from datarow [] . Tell me, please, can I fill the grid faster?

  • 3
    In terms of performance ... If there is really a lot of data, then set the VirtualMode = true mode to VirtualMode = true and handle the corresponding events. Read more in MSDN . But, I suppose, it will be enough for you to simply use regular data binding. - Ev_Hyper
  • 3
    dgv.DataSource = datarows.CopyToDataTable().DefaultView; - Ev_Hyper
  • 2
    Do you have to have dgv columns for dgv other than datarows or what? - Ev_Hyper
  • 2
    yes really ... w3big.com/ru/sqlite/sqlite-alias.html - Ev_Hyper
  • 2
    Dear Alexander, here, IMHO, the wording of your question, in which there will not be a single objection, but it will be a universal pleasure: how to properly configure and work with DataGridView, if the structure of datarow and grid is different? :) - Alexander Muksimov

2 answers 2

In the comments flashed information that loading from the DataSource is not used due to the fact that the structure of the resulting query is different from the structure DataGridViev.

This is not a problem; you can control the bound columns by knobs, choosing which column from the source to display on a particular DataGridView column.

  dataGridView.AutoGenerateColumns = false;//Колонки генерироваться не будут=> где-то потребуется их сгенерировать перед привязкой dataGridView.DataSource = result; dataGridView.Columns["GridColumnName"].DataPropertyName = "ResultColumnName"; 

Data that has not been attached is not lost.

That is, if you get a Row from a DataGridView, you can bring it to the desired type and refer to fields that were not reflected.

  • What can I say ... Your answer decided not one question. I fill in through DataSource. Everything is cool, the advice is perfect. If it's not difficult, you can give advice on the issue of ru.stackoverflow.com/questions/614114/… - Alexander Puzanov

Ideally, DataGrid or DataGrid View should not be filled in principle.

Ideally, you need to switch to virtual mode (VirtualMode = true) and load data directly from any data resource. Be it dataset, arrey strings, a sheet of custom objects or something else.

That is, in dataGridView1_NewRowNeeded you write exactly what kind of macar you need to get and process the data. And whether it is necessary to insert everything from actually available data.

Including you can load data directly from SQL.

  • All of the available data do not need to stand. - Alexander Puzanov
  • Well then you do the necessary selection. Like DataGrid.Cells ['colColName'] = sql_item.ValueForThisColumn. Nothing particularly difficult, you will understand. - Andrew