DataGrid refresh programatically

Hi,

I insert a row into a table. I show the content of table trough DataGrid.
If I execute an insert query, the DataGrid is not refresh, I not see the newly inserted row.
How can I refresh the DataGrid programatically.

Thanks.
stonecastleAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stone123Commented:
If the table is in a database, then the simpliest way is to call function OleDbDataAdapter.Fill().

For simplicity, I assume that you are using OleDbDataAdapter object to load the table into a DataSet object and assign it to the DataSource property of the DataGrid object .

Then call
oleDbDataAdapter1.Fill(yourDataSet, strTableName);
0
TheAvengerCommented:
If the table is correctly bound to the data grid, the new rows should automatically appear. Can you show your code for adding a row? You should create a new row and add it then
0
stonecastleAuthor Commented:
Hi,

Here is any addendum for my question.

Declarations in my class:

            private SqlConnection MSPIConnection;
            private SqlDataAdapter MSPIDataAdapter;
            private DataSet MSPIDataSet;
            private DataRow MSPIDataRow;

            private DataSet MSPIDs;
            private SqlDataAdapter MSPIDa;
            private SqlCommand MSPICmd;
            private DataGrid MSPI_Dg;

I display a table in datagrid:
(if  I call this after execute the query the datas is refresh, but not this a perfect way, as I think)

            public void RefreshDataGrid()
            {
                  MSPIDs.Clear();
                  MSPIDa.Fill(MSPIDs, "SO");
                  MSPI_Dg.DataSource = MSPIDs.Tables["SO"].DefaultView;
            }


I call this in the form_load event:

public void FillDataGrid(DataGrid MSPIDg)
            {
                  MSPIDs = new DataSet();

                  string sqlCmd;
                  MSPI_Dg = MSPIDg;

                  sqlCmd = "SELECT CT.CTName AS Name, SO.SP AS Period, SOK.OKId AS Id,";
                  sqlCmd = sqlCmd + "  SO.OK AS Operation, OT.OT AS Type, SO.Descr AS Description";
                  sqlCmd = sqlCmd + " FROM SO";
                  sqlCmd = sqlCmd + " INNER JOIN OT ON SO.OTId = OT.OTId";
                  sqlCmd = sqlCmd + " INNER JOIN CT ON SO.CTId = CT.CTId";
                  sqlCmd = sqlCmd + " INNER JOIN SOK ON SO.OKd = SOK.OKId";
                  sqlCmd = sqlCmd + " ORDER BY SO.CTId, SO.SP, SO.OKId";
                  
                  MSPICmd = new SqlCommand(sqlCmd, MSPIConnection);
                  MSPIDa = new SqlDataAdapter(MSPICmd);
                  MSPIDa.MissingSchemaAction = MissingSchemaAction.AddWithKey;

                  RefreshDataGrid();
            }

Here is my code fragment for insert a row:

            public static bool InsertAnOperation(int aP1, string aP2, int aP3, int aP4, string aP5)
            {
                  int p_1, p_2, p_3, p_4;

                  SqlConnection MSPIConnection = new SqlConnection(connectionStr);
                  SqlCommand MSPICommand = new SqlCommand();
                  SqlTransaction MSPITrans;

                  // Assign the connection property.
                  MSPICommand.Connection  = MSPIConnection;
                  // Open the connection.
                  MSPIConnection.Open();

                  p-1 = Convert.ToInt16(aP1) + 1;
                  p_2 = Convert.ToInt16(aP2);
                  p_3 = aP3 + 1;
                  p_4 = aP4 + 1;

                  // Insert command
                  MSPICommand.CommandText = "Insert into SO (CTId, SP, OKId, OTId, Descr) VALUES (@P1, @P2, @P3, @P4, @P5)";
                  
                  MSPICommand.Parameters.Add(new SqlParameter("@P1", SqlDbType.Int, 4));
                  MSPICommand.Parameters["@P1"].Value = p_1;
      
                  MSPICommand.Parameters.Add(new SqlParameter("@P2", SqlDbType.Int, 4));
                  MSPICommand.Parameters["@ServicePeriod"].Value = p_2;

                  MSPICommand.Parameters.Add(new SqlParameter("@P3", SqlDbType.Int, 4));
                  MSPICommand.Parameters["@OperationKindId"].Value = p_3;

                  MSPICommand.Parameters.Add(new SqlParameter("@P4", SqlDbType.Int, 4));
                  MSPICommand.Parameters["@OperationTypeId"].Value = p_4;

                  MSPICommand.Parameters.Add(new SqlParameter("@P5", SqlDbType.VarChar, 100));
                  MSPICommand.Parameters["@P5"].Value = aP5;

                  // Begin the transaction.
                  MSPITrans = MSPIConnection.BeginTransaction();
                  // Assign transaction object for a pending local transaction
                  MSPICommand.Transaction = MSPITrans;
                  try
                  {
                        // insert a record
                        MSPICommand.ExecuteNonQuery();

                        MSPITrans.Commit();

                        return true;
                  }
                  catch(Exception e)
                  {
                        MSPITrans.Rollback();
                        return false;
                  }
                  finally
                  {
                        MSPIConnection.Close();
                  }
            }
I call the FillDataGrid with my datagrid, when the form is load. -> Show me the datas of my table.
I enter the inpud data, set the query parameters with input data, and execute the query.
When I execute the query the data rows in the datagrid are not refresh.
I would like to call any command (after execute insert script) for refresh the datagrid. What command is refresh a datagrid?

Thank you.

0
TheAvengerCommented:
First, you don't need to set the data source of the DataGrid every time you make a change if you don't change the dataset itself. So the last line in the refresh method is not needed, but you have to put it once somewhere. Note that this is connected with the second point I mention.

Second, you don't call any code to refresh your data. You change the data in the database but notice that DataSet is a separate data store. If you want to refresh the data from the database, you need to call the RefreshDataGrid method at the end of your update operation. This will however reread all the data from the database again which can be time consuming. The other chance is that you go and change the values manually in the underlying dataset which needs some more programming.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.