tosorro
tosorro

Reputation: 115

C# Updating/refresh gridview

When I'm trying to update my gridview. The changes won't show up. But when I reopen the form it has changed. How can I make sure that it will update immediately (and show up). When I say:

            scb = new SqlCommandBuilder(da);
            da.Fill(dt);
            userInformation.DataSource = dt;

It wil obviously fill the table. But than I have the information twice.

This is my code currently:

            DataTable dt;
            SqlDataAdapter da;
            SqlCommand cmd;
            SqlCommandBuilder scb;

            //this is inside the constructor
            cmd = new SqlCommand("loadUserTable", conn);
            cmd.Parameters.AddWithValue("@username", this.username);
            cmd.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(cmd);
            da.SelectCommand = cmd;
            dt = new DataTable();
            da.Fill(dt);
            userInformation.DataSource = dt;

            //this is a button (called save)
            scb = new SqlCommandBuilder(da);
            da.Update(dt);
            userInformation.DataSource = dt;

Also I've tried refresh but that also doesn't do the job.

Upvotes: 0

Views: 88

Answers (1)

efischency
efischency

Reputation: 471

Each time you Fill an instance of DataTable, before you do so, you need to call the Clear() method on that DataTable. If not, the Fill method just keeps adding on to that DataTable.

Upvotes: 2

Related Questions