Your DT is a reference type variable, which is pointing to a DataTable
instance. When we assign,

DataGridView1.DataSource = DT;

DataSource property is now also pointing to the same DataTable
instance, NOT the DT itself. Which means if we execute the following
statement,

DT = New DataTable();  // creating a new DataTable instance and now DT
is pointing to this new instance

then there will be 2 DataTable instances running. Now if we make
changes in DT, the DataGridView will NOT reflect those changes because
DataGridView is still pointing to the first instance. At this point
developers do a workaround that they again assign the DataSource
property which is a very bad practice.

So what should we do here? Simply don't create new instances. Do
updation in same DT and the DataGridView will always reflect changes.

All the above is true for single Threaded application. Things go weird
when we jump into multi-threading environments, Following is my
experience,

On other Thread:
1) Update DT rows  -> DataGridView will reflect changes immediately
2) Add rows in DT   -> DataGridView will NOT reflect changes BUT if
you scroll meanwhile it will reflect otherwise call
DataGridView.Refresh()
3) Delete Rows from DT -> CAUTION: Don't delete Rows on other thread
very strange errors will be poping which you can handle in
DataGridView.DataError event handler but this is NOT recommended

So if you are updating the Rows on different thread, no problemo. If
you are adding the rows and you want to reflect the changes
immediately then refresh the DataGridView after each/batch addition
(follow the multi-threading rules here). If you are deleting!!! I
suggest don't delete, if very necessary then delete from main thread.

Good luck,
Arsalan Tamiz

On Feb 20, 8:33 pm, Nacho108 <[email protected]> wrote:
> Hi, thanks for answering.
> I have to process the data from the database and place it in a
> datatable in order to do it.
> In brief I'm doing something like this:
>
> 1) Read the data from SQL and put it in a datatabel (dt_aux).
> 2) Process dt_aux and place it into dt with this line
>   dt=filtra_activas(dt_aux.Copy());
> 3) Assign the dt to the datagridview every time, since otherwise it
> doesn't notice wether the dt have changed.
>
> So I cannot bind directly the datagridview with the SQL.
>
> Any other idea? (or maybe go further on the explanation on this one if
> I've got it wrong)
>
> On Feb 20, 4:31 pm, Vinicius Quaiato <[email protected]>
> wrote:
>
> > Hi,
>
> > Try to bind a SqlDataSource in the Grid, then just call the DataBind()
> > method instead of set the DataSource;
>
> > 2009/2/20 Nacho108 <[email protected]>
>
> > > Hi Everyone !
>
> > > I'm having a problem with a Datagridview control, I hope somebody can
> > > help me since I tried many things but I cannot nail the solution.
> > > I have a Datagridview displaying the contents of a database that
> > > change continuously, but the changes are minimum (around 2 or 3 rows
> > > change over 180, every 10 seconds), and I'm reading this database
> > > through a separated thread and then refreshing the datagridview each
> > > 10 seconds. All the threading thing is already debugged and working
> > > fine. The problem is not connected with it.
> > > The user should be able to scroll the datagridview normally during the
> > > refresh process, but it happens that when the program comes to this
> > > line where I update the datagridview itself (after reading the
> > > database):
>
> > > dataGridView1.DataSource = dt;     (dt is a datatable which I populate
> > > with database values)
>
> > > If I were scrolling with the mouse, the mouse looses control and I
> > > have to take again the scroll bar, this is very anoying for the user
> > > since refresh is very often.
>
> > > I don't know if I'm doing things right, so I have a few questions:
>
> > > 1) If I don't assign the datasource in each database reading, the
> > > control doesn't refresh its content. Is this normal? The datagridview
> > > content shouldn't automatically follow the content of dt (datatable)?
>
> > > 2) In case this assign I describe before is necessary, is there a way
> > > to avoiding this loose of control I'm experimenting?
>
> > > Thanks in advance
> > > Nacho

Reply via email to