另一个问题是关于显示和获取数据库mySQL中其他表的值。我添加了新的DataGridViewComboboxColumn
,但是我无法从“ projekty”表中的值获取到“ modulate”表中的值。 “ projekty”表仅包含2列:ID type int
和NAZWA_PROJEKTU type VARCHAR
。有我的代码:
新tables.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Collections;
namespace KontrolaBazaDanych
{
public partial class New_Tables : Form
{
public New_Tables()
{
InitializeComponent();
}
MySqlConnection connection;
MySqlDataAdapter adapter, adapter2, adapter3, adapter4;
DataSet ds, ds2, ds3, ds4;
DataTable dt;
public void New_Tables_Load(object sender, EventArgs e)
{
try
{
connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
adapter = new MySqlDataAdapter("SELECT * FROM eltechnik_projekt1.pracownicy", connection);
adapter2 = new MySqlDataAdapter("SELECT moduly.ID_MODULU, moduly.NAZWA, projekty.NAZWA_PROJEKTU FROM projekt1.moduly INNER JOIN projekt1.projekty ON projekty.ID = moduly.ID_PROJEKTU;", connection);
adapter3 = new MySqlDataAdapter("SELECT zlecenia.ID_ZLECENIA, zlecenia.OPIS_ZLECENIA, zlecenia.NUMER_ZLECENIA, projekty.NAZWA_PROJEKTU FROM projekt1.zlecenia INNER JOIN projekt1.projekty ON projekty.ID = zlecenia.ID_PROJEKTU", connection);
adapter4 = new MySqlDataAdapter("SELECT * FROM projekt1.projekty", connection);
connection.Open();
ds = new DataSet();
adapter.Fill(ds, "pracownicy");
dataGridView1.DataSource = ds.Tables["pracownicy"];
ds2 = new DataSet();
adapter2.Fill(ds2, "moduly");
dataGridView2.DataSource = ds.Tables["moduly"];
ds3 = new DataSet();
adapter3.Fill(ds3, "zlecenia");
dataGridView3.DataSource = ds3.Tables["zlecenia"];
ds4 = new DataSet();
adapter4.Fill(ds4, "projekty");
dataGridView4.DataSource = ds4.Tables["projekty"];
dataGridView2.DataSource = loaddata();
dataGridView1.Columns[0].Visible = false;
dataGridView2.Columns[0].Visible = false;
dataGridView3.Columns[0].Visible = false;
dataGridView4.Columns[0].Visible = false;
fillcombo();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
try
{
MySqlCommandBuilder cmbl = new MySqlCommandBuilder(adapter);
adapter.Update(ds, "pracownicy");
MySqlCommandBuilder cmbl2 = new MySqlCommandBuilder(adapter2);
adapter2.Update(ds2, "moduly");
MySqlCommandBuilder cmbl3 = new MySqlCommandBuilder(adapter3);
adapter3.Update(ds3, "zlecenia");
MySqlCommandBuilder cmbl4 = new MySqlCommandBuilder(adapter4);
adapter4.Update(ds4, "projekty");
MessageBox.Show("Informacja została zauktualizowana", "Aktualizacja", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].Cells[0].Value = e.RowIndex + 1;
}
private void dataGridView2_RowEnter(object sender, DataGridViewCellEventArgs e)
{
dataGridView2.Rows[e.RowIndex].Cells[0].Value = e.RowIndex + 1;
}
private void dataGridView2_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void dataGridView3_RowEnter(object sender, DataGridViewCellEventArgs e)
{
dataGridView3.Rows[e.RowIndex].Cells[0].Value = e.RowIndex + 1;
}
private void dataGridView4_RowEnter(object sender, DataGridViewCellEventArgs e)
{
dataGridView4.Rows[e.RowIndex].Cells[0].Value = e.RowIndex + 1;
}
private void button3_Click(object sender, EventArgs e)
{
MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
DataTable dt = new DataTable();
if (textBox1.Text.Length >= 0)
{
MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM projekt1.pracownicy WHERE IMIE LIKE '%" + textBox1.Text + "%' OR NAZWISKO LIKE '%" + textBox1.Text + "%';", connection);
adapter.Fill(dt);
}
dataGridView1.DataSource = dt;
}
private void button4_Click(object sender, EventArgs e)
{
MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
DataTable dt2 = new DataTable();
if (textBox2.Text.Length >= 0)
{
MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM projekt1.moduly WHERE NAZWA LIKE '%" + textBox2.Text + "%' OR MODEL_MODULU LIKE '%" + textBox2.Text + "%';", connection);
adapter.Fill(dt2);
}
dataGridView2.DataSource = dt2;
}
private void button5_Click(object sender, EventArgs e)
{
MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
DataTable dt3 = new DataTable();
if (textBox3.Text.Length >= 0)
{
MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM projekt1.zlecenia WHERE OPIS_ZLECENIA LIKE '%" + textBox3.Text + "%' OR NUMER_ZLECENIA LIKE '%" + textBox3.Text + "%';", connection);
adapter.Fill(dt3);
}
dataGridView3.DataSource = dt3;
}
private void button6_Click(object sender, EventArgs e)
{
MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
DataTable dt4 = new DataTable();
if (textBox4.Text.Length >= 0)
{
MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM projekt1.projekty WHERE NAZWA_PROJEKTU LIKE '%" + textBox4.Text + "%';", connection);
adapter.Fill(dt4);
}
dataGridView4.DataSource = dt4;
}
private DataTable loaddata()
{
MySqlDataAdapter adapter = new MySqlDataAdapter();
MySqlCommand cmd;
DataSet ds = new DataSet();
string sql = "SELECT * FROM projekt1.moduly";
cmd = new MySqlCommand(sql, connection);
adapter.SelectCommand = cmd;
adapter.Fill(ds);
dt = ds.Tables[0];
return dt;
}
private void fillcombo()
{
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
MySqlDataAdapter sda1 = new MySqlDataAdapter("SELECT * FROM projekt1.projekty", connection);
combo.HeaderText = "NAZWA_PROJEKTU";
combo.Name = "combo";
ArrayList row = new ArrayList();
foreach (DataRow dr in dt.Rows)
{
combo.DisplayMember = "NAZWA_PROJEKTU";
combo.ValueMember = "ID";
combo.DataSource = ds.Tables["projekty"];
}
combo.Items.AddRange(row.ToArray());
dataGridView2.Columns.Add(combo);
}
}
}
有人可以解释如何解决吗?感谢您的帮助或建议。