【问题标题】:How do I Update/Reload DataGridView BindingSource?如何更新/重新加载 DataGridView BindingSource?
【发布时间】:2012-06-16 17:39:26
【问题描述】:

我是 C#、Windows 窗体和数据网格视图的新手。我有一个选项卡式表单:选项卡 1 显示练习表的数据网格视图;选项卡 2 用于向表中添加新练习。练习表通过 test_ExercisesDataSet、vwexercisesBindingSource、vw_ExercisesTableAdapter 绑定到数据网格视图。

我不确定我需要做什么来重新绑定/刷新绑定源,以便在我切换回选项卡 1 时刷新 datagridview。如果我完全关闭表单并重新启动它,我可以看到表格中的新行。

我在 Web 和 StackOverflow 上都看到了很多示例,但我仍然不明白我做错了什么。

顺便说一句,我使用的是 Visual Studio 2010。

感谢任何帮助!

谢谢!

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();  
        }
    }
}

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    创建LoadDataGridView方法:

    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;
    
    }
    
    现在,您所要做的就是在lead1_load()和InsertExcercise()的末尾呼叫该方法。如果您必须使用数据集,请不要忘记在最后处理DataAdapter对象以节省资源。

    【讨论】:

      【解决方案2】:

      您必须在插入后使用数据库中的信息重新加载您的数据集。这不是自动的!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-24
        • 1970-01-01
        • 2010-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多