Hello everyone, I recently started using C # and came across a few problems, for example, to update datagridview so that everything would remain in its place (for example, it wouldn’t scour it up, the line with which we work and so on would remain highlighted). And they began to worry. After various actions, for example, add a new record \ edit the current one, I call update_orders (); so that after adding I see the change in datagridview right away, am I doing the right thing?

public void update_orders() { cn.Open(); BindingSource bs = new BindingSource(); DataSet ds = new DataSet(); DataTable dt = new DataTable(); ds.Tables.Add(dt); SqlDataAdapter da = new SqlDataAdapter(); da = new SqlDataAdapter("тут запрос", cn); da.Fill(dt); bs.DataSource = dt; gridOrder.DataSource = bs; cn.Close(); 

}

  • Add gridOrder.DataBind () to the very end and there will be happiness - nick_n_a
  • @nick_n_a my project is Win Forms, DataBind () is this for the web, or am I mistaken? Thanks for your reply. - Winteriscoming

1 answer 1

Whether this is true or not depends on the business logic of the application.

If changes in the database can be done simultaneously by different users, then it is logical to get new data from the database.

If only one user works with the database, then it makes no sense to get the same data from there. However, even in this case, it may be necessary: ​​when updating, triggers (if any) may work, you may need to get the values ​​of keys generated by the base, default values ​​of columns, values ​​of calculated columns.


To make the DataGridView scroll remain in the same place, you can use the following:

 int index = gridOrder.FirstDisplayedScrollingRowIndex; gridOrder.DataSource = bs; gridOrder.FirstDisplayedScrollingRowIndex = index; 
  • Thank you very much for the help on scrolling !!! Regarding business logic, I make an update only when there are changes in the tables associated with this datagridview. My update just happens like this update_orders (); for example after insert. Why I asked such a question, I can’t configure DgvFilterPopup or DataGridViewAutoFilter, they are approximately the same in functionality, the problem is that after updating the datagrid, the exposed filters are lost using the above described libraries. Maybe you know how to do that would not stray? - Winteriscoming pm
  • @Winteriscoming - Um, is this some kind of third-party datagrid, not a native DataGridView? If so, then it is probably worth asking a new question, indicating which component it is. Update: yeah, google. But I cannot suggest anything at once. - Alexander Petrov
  • No, this is standard, just the library is connected and movies on the columns become available. I created a question not in one forum, no one knows anything. I understand that I need to catch the name of the column and the values ​​selected on the film and shove them into the query during the update and thus keep the datagridview state. Is that right, or is there another way? - Winteriscoming
  • @Winteriscoming judging from the source code of DataGridViewAutoFilter, you can’t get the filter values, there are no methods for reading and writing for them. But since the source code is available, you can easily add what you need yourself, this library is provided for visual demonstration or a basis for modifications. Quote from the description - it The sample is not a full-featured, general-purpose, bug-free, nor Microsoft-supported solution, but it does provide a groundwork, which you can modify or use to guide your own implementation. - rdorn
  • @rdorn, thanks. So it’s not my destiny to implement it, opened the source code and did not understand anything. Need to write an additional class, with the help of which it would be possible to determine this? - Winteriscoming