Made a method (in class):

public static async Task LoadBase() { StringBuilder sb = new StringBuilder(); using (SQLiteConnection con = new SQLiteConnection(string.Format("Data Source={0};", CommonData.pathBase))) { await con.OpenAsync(); DataSet dt = new DataSet(); using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM `base`", con)) { using (SQLiteDataAdapter da = new SQLiteDataAdapter(cmd)) { da.Fill(dt); sb.Append(dt); } } } } 

I created a StringBuilder and now I want to add all the data to it in order to display it later in the DataGridView . The problem, in fact, is how to add all the rows somewhere to put them in the DataGridView later in the form?

  • one
    ... why do you need StringBuilder ? - Ev_Hyper
  • @Ev_Hyper, I tried to write strings. Tell me, please, the correct version of how to do it. - Maxim
  • @Ev_Hyper, in short, it seems to have done)) only when calling Func.LoadBase(); writes a Warning: since this call is not expected, the current method continues until the end of the call. Try applying the await operator to the result of the call. I did so Task.Run(async () => await Func.LoadBase()); the warning is gone, but the data is no longer loading .. - Maxim
  • one
    Now there is no time to respond in detail, but it will be much easier for you to work not with strings, but with normal data. And read about EF - there is enough information, including in Russian. - Ev_Hyper

2 answers 2

Why do you add strings to a StringBuilder?

You can tie a DataTable to a DataGridView.

You can also generate a DataGridView on the fly by creating row columns with adding data from the database.

But note that if you want to edit the data and send it back to the database, then you have to implement a lot of things with pens, and if you used DataTable, then all of this would be available out of the box.

  • Thanks, but I didn’t want to access the DataGridView through the class, so I tried to send the data to the form, which I actually did (without StringBuilder). Now the only problem is that I wrote in the comments above. - Maxim

The optimal solution to this problem I see the following implementation:

  1. Data must be switched to virtual mode.
  2. On the RowNeeded event, load the required row directly from the database

Accordingly, if you want to edit and send back to the database, you need to also store somewhere shown at this second objects and when editing a cell in the data grid, edit it in this object.

I would also advise you to replace the current approach to working with the base on some kind of micro-ORM like PetaPoco.