Solved

DataGrid refresh programatically

Posted on 2003-10-21
4
1,591 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
  • 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 75 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now