如何从 DataGridView 中的 SQL 数据库更改 DataSource 以便更新
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
P.S。对不起我的笨蛋代码
这不是我构建程序的方式(我会为每种事物创建一个数据网格视图 [movie/director/star] 等,我会在设计时构建它们 Show/Hide相关的,或者将它们放在 TabPage 中,每个网格都有一个选项卡)但是:
- 确保您的 datagridview1 的
AutoGenerateColumns
属性 设置为 true
- 更改 datagridview1 上的绑定后,在相关绑定源上调用
xxxBindingSource.ResetBindings()
;尝试传递 false,因为从技术上讲,绑定源正在查看的模式没有改变。如果 false 不起作用,您可能需要传递 true(它对我有用)
- 如果您发现 datagridview 正在旧列的末尾生成新列而不是替换它们,您可能需要调用
dataGridView1.Columns.Clear()
- 您不需要将 dgv1 的数据源设置为空的调用
所以我找到了出路
这个想法是不使用主连接向导(对于 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;
我正在开发一个与遇到问题的 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
P.S。对不起我的笨蛋代码
这不是我构建程序的方式(我会为每种事物创建一个数据网格视图 [movie/director/star] 等,我会在设计时构建它们 Show/Hide相关的,或者将它们放在 TabPage 中,每个网格都有一个选项卡)但是:
- 确保您的 datagridview1 的
AutoGenerateColumns
属性 设置为true
- 更改 datagridview1 上的绑定后,在相关绑定源上调用
xxxBindingSource.ResetBindings()
;尝试传递 false,因为从技术上讲,绑定源正在查看的模式没有改变。如果 false 不起作用,您可能需要传递 true(它对我有用) - 如果您发现 datagridview 正在旧列的末尾生成新列而不是替换它们,您可能需要调用
dataGridView1.Columns.Clear()
- 您不需要将 dgv1 的数据源设置为空的调用
所以我找到了出路
这个想法是不使用主连接向导(对于 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;