Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8108673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:11:15+00:00 2026-06-06T01:11:15+00:00

I am new to C#, Windows Forms, and datagridviews. I have a tabbed form:

  • 0

I am new to C#, Windows Forms, and datagridviews. I have a tabbed form: tab 1 displays a datagridview of the exercises table; tab 2 is for adding a new exercise to the table. The exercises table is bound to the datagrid view via test_ExercisesDataSet, vwexercisesBindingSource, vw_ExercisesTableAdapter.

I’m not sure what I need to do to rebind/refresh the binding source in order to get the datagridview to refresh when I switch back to tab 1. If I completely close the form and restart it, I can see the new row in the table.

I’ve seen many examples both on the Web and on StackOverflow but I still don’t understand what I’m doing wrong.

BTW, I’m using Visual Studio 2010.

Any help is appreciated!!

Thanks!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace testTabbedInterface
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string GetConnectionString()
        {
            return connString;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'test_ExercisesDataSet.vw_exercises' table. You can move, or remove it, as needed.
            this.vw_exercisesTableAdapter.Fill(this.test_ExercisesDataSet.vw_exercises);
            exerciseListDataGridView.DataSource = this.test_ExercisesDataSet.vw_exercises;
        }

        private void InsertExercise(string exerciseName, string exerciseDescription, string exerciseBegin)
        {
            var conn = new SqlConnection(GetConnectionString());
            const string InsertExerciseSql = @"INSERT INTO database.dbo.exercises
                (PK_exerciseUID,
                exerciseName,
                exerciseDescription,
                exerciseBegin,
                exerciseEnd) 
                VALUES 
                (@PK_exerciseUID,
                @exerciseName,
                @exerciseDescription,
                @exerciseBegin,
                NULL)";

            try
            {
                SqlCommand cmd = new SqlCommand(InsertExerciseSql, conn);
                var param = new SqlParameter[4];

                Guid exerciseGUID = Guid.NewGuid();
                param[0] = new SqlParameter("@PK_exerciseUID", exerciseGUID);
                param[1] = new SqlParameter("@exerciseName", exerciseName);
                param[2] = new SqlParameter("@exerciseDescription", exerciseDescription);

                //Convert date(s) to correct format
                DateTime exerciseBeginConverted = Convert.ToDateTime(exerciseBegin);
                param[3] = new SqlParameter("@exerciseBegin", exerciseBeginConverted);

                foreach (SqlParameter t in param)
                {
                    cmd.Parameters.Add(t);
                }

                conn.Open();
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                MessageBox.Show("Test/Exercise, " + exerciseName + ", successfully added!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (SqlException ex)
            {
                string msg = "Error inserting into 'exercises': ";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                conn.Close();
            }
        }

        private void saveExerciseButton_Click(object sender, EventArgs e)
        {
            InsertExercise(exerciseName.Text, exerciseDescription.Text, exerciseBegin.Text);

            this.exerciseListDataGridView.EndEdit();

            tabControl1.SelectTab("testExerciseTab");
        }

        private void addExButton_Click(object sender, EventArgs e)
        {
            tabControl1.SelectTab("exerciseTab");
        }

        private void reloadExListButton_Click(object sender, EventArgs e)
        {
            this.exerciseListDataGridView.Refresh();  
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:11:17+00:00Added an answer on June 6, 2026 at 1:11 am

    Create a LoadDataGridView method:

    private void LoadDataGridView() {
        // Fill a DataAdapter using the SelectCommand.
        DataAdapter da = null;
    
        // The Sql code here
    
        // In case something fails, bail out of the method.
        if (da == null) return;
    
        // Clear the DataSource or else you'll get double of everything.
        if (exerciseListDataGridView.DataSource != null) {
            exerciseListDataGridView.DataSource.Clear();
            exerciseListDataGridView.DataSource = null;
        }
    
        // I'm doing this off the top of my head, you may need to fill a DataSet here.
        exerciseListDataGridView.DataSource = da.DefaultView;
    
    }
    

    Now all you have to do is call that method in both your Form1_Load() and at the end of your InsertExcercise(). If you have to use a DataSet, don’t forget to dispose of the DataAdapter object at the end to conserve resources.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have datagridviews on windows form, i am adding records to datagridview using small
I work with windows forms and on the form I have a DataGridView. This
I have a windows forms DataGridView that contains some DataGridViewComboBoxCell s that are bound
I have a windows form where in I open new threads for server communication.
Windows form application. C# 4.0. Basically I have two datagridviews dgv1 and dgv2. One
I have a Windows Forms created with a dataGridView on it. I also have
I am developing a windows form application in C#. I have a datagridview on
I have this simple piece of code on a windows form containing said DataGridView
I am new to Windows forms and having an issue handling all the user
I am attempting to use a new settings file in my Windows Forms Application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.