Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DataGrid refresh programatically

Posted on 2003-10-21
4
Medium Priority
?
1,611 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:stonecastle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Expert Comment

by:stone123
ID: 9591420
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
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9596832
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
 

Author Comment

by:stonecastle
ID: 9597383
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
 
LVL 20

Accepted Solution

by:
TheAvenger earned 150 total points
ID: 9597414
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question