Link to home
Start Free TrialLog in
Avatar of Rama Tito
Rama TitoFlag 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.
Avatar of Rimvis
Rimvis
Flag of Lithuania image

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?
Avatar of 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
Avatar of Rimvis
Rimvis
Flag of Lithuania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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