Avatar of Rama Tito
Rama Tito
Flag for Malaysia asked on

Update DataGridView

Hi, I am receiving data from hardware and continuously update my Database MS SQL. In the same time i am trying to retrieve  these data from MS SQL to my datagridview. The data update from hardware to Database no problem but updating from Database to my datagirdview, I do face problem. The new incoming data is just be written in first row only. Its just keep on updating the first row instead of create a new row to update incoming data.
.NET ProgrammingVisual C++.NET

Avatar of undefined
Last Comment
Rama Tito

8/22/2022 - Mon
Rimvis

Hi TITO2009,

In what manner data is written to DB? Is it like multiple records with time stamp and values? Or are you constantly updating one row in the table? Could you post you table structure?
Rama Tito

ASKER
private delegate void UpdateGridDG1(DataTable table);

private void UpdateDG1()
{
            string cmd;
            
            cmd = "Select No as [No],Sender as [Sender],TimeRec as [Time],MsgData as [RawData],ActArea as [Area],ActData as [Actual Data] From InComing_Data order by No desc";
            
            da.SelectCommand = new SqlCommand(cmd, Con);
            Con.Open();
            ds.Clear();
            da.Fill(ds, "InComing_Data");

            UpdateGrid(ds.Tables["InComing_Data"]);
            //DG1.DataSource = ds.Tables["InComing_Data"];
            bs.DataSource = ds.Tables["InComing_Data"];
            
            
            Con.Close();
        }

        private void UpdateGrid(DataTable table)
        {
            if (DG1.InvokeRequired)
            {
                UpdateGridDG1 UP = new UpdateGridDG1(UpdateGrid);
                this.Invoke(UP,new object[]{table});
            }
            else
            {
                DG1.DataSource = table;
            }
        }
                                  

Open in new window

ASKER CERTIFIED SOLUTION
Rimvis

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Rama Tito

ASKER
Hi,

InComing_Data in ms sql are updated accordingly, When i run the program first time it will update all the data form ms sql database accordingly into my datagridview, then for subsequently  incoming data will keep on writing on top row instead keep on create new row for update new incoming data.

I am surprise, when i check my ms sql database the data are insert into database accordingly, the problem is to extract the data from there to my datagridview.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Rama Tito

ASKER
Hi, I do manage to solve by using the code below
private void UpdateDG1()
        {
            if (DG1.InvokeRequired)
            {
                DG1.Invoke(new MethodInvoker(this.UpdateDG1 ));
            }
            else
            {
                string cmd;

                cmd = "Select No as [No],Sender as [Sender],TimeRec as [Time],MsgDataIn as [InComing SMS],MsgDataOut as [OutGoing SMS],AreaRecSend as [Area]From InOutGoingSMS order by No desc";

                da.SelectCommand = new SqlCommand(cmd, Con);
                Con.Open();
                ds.Clear();
                da.Fill(ds, "InComing_Data");

                //UpdateGrid(ds.Tables["InComing_Data"]);
                DG1.DataSource = ds.Tables["InComing_Data"];
                bs.DataSource = ds.Tables["InComing_Data"];
                //UpdateDataBindingGrid(ds.Tables["InComing_Data"]);

                Con.Close();
            }
        }

Open in new window