如何更新/重装的DataGridView的BindingSource? [英] How do I Update/Reload DataGridView BindingSource?

查看:231
本文介绍了如何更新/重装的DataGridView的BindingSource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C#,Windows窗体和datagridviews。我有一个标签的形式:标签1显示的练习表的datagridview的;标签2是添加一个新的练习表。练习表绑定通过test_ExercisesDataSet,vwexercisesBindingSource,vw_ExercisesTableAdapter数据网格视图。



我不知道我需要做重新绑定/刷新绑定源,以得到的DataGridView,当我切换回选项卡1.如果我完全关闭窗体并重新启动它,我可以看到在表中的新行刷新。



我见过了Web上和StackOverflow的例子很多,但我还是不明白我在做什么错了。



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



任何帮助表示赞赏!



谢谢!

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Configuration;
使用System.Data这;
使用System.Data.SqlClient的;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;

命名空间testTabbedInterface
{
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent() ;
}

公共字符串GetConnectionString()
{
返回CONNSTRING;
}

私人无效Form1_Load的(对象发件人,EventArgs五)
{
// TODO:这行代码加载数据到test_ExercisesDataSet.vw_exercises'表。您可以移动,或删除它,根据需要。
this.vw_exercisesTableAdapter.Fill(this.test_ExercisesDataSet.vw_exercises);
exerciseListDataGridView.DataSource = this.test_ExercisesDataSet.vw_exercises;
}

私人无效InsertExercise(字符串exerciseName,串exerciseDescription,串exerciseBegin)
{
变种康恩=新的SqlConnection(GetConnectionString());
常量字符串InsertExerciseSql = @INSERT INTO database.dbo.exercises
(PK_exerciseUID,
exerciseName,
exerciseDescription,
exerciseBegin,
exerciseEnd)

(@PK_exerciseUID,
@exerciseName,
@exerciseDescription,
@exerciseBegin,
NULL);


{
的SqlCommand CMD =新的SqlCommand(InsertExerciseSql,康涅狄格州);
变种参数=新的SqlParameter [4];

的Guid = exerciseGUID Guid.NewGuid();
参数[0] =新的SqlParameter(@ PK_exerciseUID,exerciseGUID);
参数[1] =新的SqlParameter(@ exerciseName,exerciseName);
参数[2] =新的SqlParameter(@ exerciseDescription,exerciseDescription);

//将日期(S),以正确的格式
的DateTime exerciseBeginConverted = Convert.ToDateTime(exerciseBegin);
参数[3] =新的SqlParameter(@ exerciseBegin,exerciseBeginConverted);

的foreach(在参数的SqlParameter T)
{
cmd.Parameters.Add(T);
}

conn.Open();
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
MessageBox.Show(测试/运动,+ exerciseName +,添加成功!,成功了!,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
赶上(SQLEXCEPTION EX)
{
弦乐味精=错误插入'演习';
味精+ = ex.Message;
抛出新的异常(MSG);
}
终于
{
conn.Close();
}
}

私人无效saveExerciseButton_Click(对象发件人,EventArgs五)
{
InsertExercise(exerciseName.Text,exerciseDescription.Text,exerciseBegin.Text );

this.exerciseListDataGridView.EndEdit();

tabControl1.SelectTab(testExerciseTab);
}

私人无效addExButton_Click(对象发件人,EventArgs五)
{
tabControl1.SelectTab(exerciseTab);
}

私人无效reloadExListButton_Click(对象发件人,EventArgs五)
{
this.exerciseListDataGridView.Refresh();
}
}
}


解决方案

创建LoadDataGridView方法:

 私人无效LoadDataGridView(){
//填充使用一个DataAdapter SelectCommand中。
DataAdapter的DA = NULL;这里

// SQL代码

//万一出故障时,摆脱困境的方法。
如果(DA == NULL)回报;

//清除数据源,否则你会得到双倍的一切。
如果(exerciseListDataGridView.DataSource!= NULL){
exerciseListDataGridView.DataSource.Clear();
exerciseListDataGridView.DataSource = NULL;
}

//我这样做了我的头顶,你可能需要在这里填充DataSet。
exerciseListDataGridView.DataSource = da.DefaultView;

}

现在你需要做的就是调用都该方法你的Form1_Load的()中,在您的InsertExcercise的末尾()。如果你有使用DataSet,不要忘记在最后处置DataAdapter对象的节约资源。


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

解决方案

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.

这篇关于如何更新/重装的DataGridView的BindingSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆