[英]How to change DataSource from SQL Database in DataGridView so it will be updated
我正在开发与我的SQL数据库进行交互的应用程序,该应用程序遇到了问题。
在ATM上,此问题表示自身无法在加载子窗体之前更改DataGridView中的DataSource参数。 也可以以主要形式进行操作,但是我不知道如何进行操作。 form2(child)的代码如下:
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
PS对不起,我的笨蛋代码
这不是我构建程序的方式(我对每种事物(电影/导演/明星)等都有一个datagridview,我会在设计时构建它们并显示/隐藏相关的东西,或者放进去在带有每个网格选项卡的TabPage中),但:
AutoGenerateColumns
属性设置为true
xxxBindingSource.ResetBindings()
。 尝试将其传递为false,因为从技术上讲,绑定源正在查看的架构没有更改。 如果无法解决,您可能需要通过true(对我而言确实如此) dataGridView1.Columns.Clear()
所以我找到了出路
这样做的想法是不使用主连接向导(对于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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.