如何在DataGridView中从SQL数据库更改DataSource,以便更新

时间:2017-05-11 09:47:26

标签: c# sql datagridview datasource

我正在开发一个与我的SQL数据库交互的应用程序,它正遇到问题。

ATM问题表示在加载子表单之前无法更改DataGridView中的DataSource参数。 它也可以在主窗体中完成,但我不知道如何做到这一点。 表格2(儿童)的代码​​如下:

public partial class Form2 : Form
{
    string pass;   //this is used to pass what user chooses in the Form1 combobox
    public Form2(string choise)
    {
        InitializeComponent();
        pass = choise;
    }
    private void Form2_Load(object sender, EventArgs e)
    {
        if (pass == "studio")
        {
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = studioBindingSource;
            this.studioTableAdapter.Fill(this.videotekaDataSet.studio);
            MessageBox.Show(pass, "Notification", MessageBoxButtons.OK);
        }
        else if (pass == "star_sln")
        {
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = starslnBindingSource;
            this.star_slnTableAdapter.Fill(this.videotekaDataSet.star_sln);
            MessageBox.Show(pass, "Notification", MessageBoxButtons.OK);
        }
        else if (pass == "movie_star")
        {
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = moviestarBindingSource;
            this.movie_starTableAdapter.Fill(this.videotekaDataSet.movie_star);
            MessageBox.Show(pass, "Notification", MessageBoxButtons.OK);
        }
        else if (pass == "movie_exec")
        {
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = movieexecBindingSource;
            this.movie_execTableAdapter.Fill(this.videotekaDataSet.movie_exec);
            MessageBox.Show(pass, "Notification", MessageBoxButtons.OK);
        }
        else if (pass == "movie")
        {
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = movieBindingSource;
            this.movieTableAdapter.Fill(this.videotekaDataSet.movie);
            MessageBox.Show(pass, "Notification", MessageBoxButtons.OK);
        }
        else MessageBox.Show("Error showing a table", "Alert", MessageBoxButtons.OK);
        }
}

这是我使用的SQL查询

CREATE DATABASE videoteka
GO
USE videoteka
GO
CREATE TABLE movie_exec (
    certif int PRIMARY KEY,
    name_exec varchar(25),
    adres_exec varchar(50),
    networth decimal(14,0)
)
GO
CREATE TABLE movie_star (
    id_star int PRIMARY KEY,
    name_star varchar(35),
    adres_star varchar(50),
    gender char(1),
    bd_star date
)
GO
CREATE TABLE studio (
    id_studio int PRIMARY KEY,
    name_studio varchar(35),
    adres_studio varchar(50),
    certif int,
    FOREIGN KEY(certif) REFERENCES movie_exec(certif),
)
GO
CREATE TABLE movie (
    id_movie int PRIMARY KEY,
    title_m varchar(40),
    year_m int,
    length_m int,
    incolor char(1),
    id_studio int,
    certif int,
    FOREIGN KEY(certif) REFERENCES movie_exec(certif),
    FOREIGN KEY(id_studio) REFERENCES studio(id_studio)
)
GO
CREATE TABLE star_sln (
    id_movie int,
    id_star int,
    FOREIGN KEY(id_movie) REFERENCES movie(id_movie),
    FOREIGN KEY(id_star) REFERENCES movie_star(id_star)
)
GO

P.S。对不起我的笨蛋代码

2 个答案:

答案 0 :(得分:0)

这不是我构建程序的方式(我会为每种事物[电影/导演/明星]等设置数据网格视图,我会在设计时间内构建它们并显示/隐藏相关的部分,或者将它们放在带有每个网格选项卡的TabPage中,但是:

  • 确保您的datagridview1的AutoGenerateColumns属性设置为true
  • 更改datagridview1上的绑定后,在相关绑定源上调用xxxBindingSource.ResetBindings();尝试传递false,因为从技术上讲,bindingsource正在查看的模式没有改变。如果false不起作用(它对我有用),你可能需要传递true。
  • 如果您发现datagridview在旧版本的末尾生成新列而不是替换它们,则可能需要调用dataGridView1.Columns.Clear()
  • 您不需要将dgv1的数据源设置为空的调用

答案 1 :(得分:0)

所以我找到了出路 我们的想法是不使用主连接向导(对于datagridview),而是手动插入连接字符串并使用SqlCommand,SqlDataAdapter和Datatable变量和函数。
这就是它的样子

MessageBox.Show(comboBox1.Text, "Notification", MessageBoxButtons.OK);
string command = "select * from " + comboBox1.Text;
SqlCommand com = new SqlCommand(command, co);
adp = new SqlDataAdapter(com); //this is declared globally, so I can use it anywhere
dt = new DataTable();          // same as previous
adp.Fill(dt);
dataGridView1.DataSource = dt;