how to fetch all rows inserted in datagirdview in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Firas2014
    New Member
    • Sep 2014
    • 3

    how to fetch all rows inserted in datagirdview in c#

    I fetch maxid row to view in datagridview using the following code
    Code:
     public static DataTable GetMaximpID()
            {
                string strconn = AlShehabi.Properties.Settings.Default.NewSalariesDBConnectionString;
                SqlConnection conn = new SqlConnection(strconn);
    
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                SqlCommand command = new SqlCommand("select * from Imprests where Imprest_ID= (Select MAX(imprest_ID) FROM Imprests)", conn);
    
                SqlDataAdapter adpater = new SqlDataAdapter(command);
                DataTable dt = new DataTable();
                try
                {
                    adpater.Fill(dt);
                }
    
    
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    
                conn.Close();
                return dt;
            }
    but I want to fetch all rows inserted and view in datagridview
    for example if user insert 4 rows I want to view them in datagridview after insert them
    using insertbutton_cl ick
    Last edited by Rabbit; Sep 14 '14, 08:05 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Gobindap
    New Member
    • Feb 2013
    • 25

    #2
    Database itself does not identify which is new or which is old. We need to shape data table in database in such a way which help to reach our motto. For this create one new column named 'status' as integer in the table where you insert data. Now shape you code to return zero if those records are views else 1. Now when user click on the button select all rows where status row value = 1 i.e. "Select * from dataTable where status=1"

    I hope your problem is solved.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Use the DataTable.GetCh anges Method to get the changes made to the DataTable.

      If there were changes this will return you a DataTable and you can check the RowState of the Rows in that table for Added rows.


      OR you could add a listener for the DataTable RowChanged Event and keep a running list of items that were added.

      Or you could add an unnecessary column to keep track of the state as suggested.


      There are a lot of ways to accomplish this.




      -Frinny
      Last edited by Frinavale; Sep 16 '14, 04:36 PM.

      Comment

      Working...