Update DataGridView

Rama Tito
Rama Tito used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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 TitoProgrammer

Author

Commented:
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

Commented:
Hi,

But InComing_Data contains multiple records, right?

If you execute same statement in Management Studio, how many rows do you get?
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

Open in new window

Rama TitoProgrammer

Author

Commented:
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.
Rama TitoProgrammer

Author

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial