我正在尝试将数据从数据库中拉到DataGridView3,您可以在下图的右列中看到它。当我进行调试时,我看到DataSet保持正确的值,并且当我第一次尝试对dataGrid1进行cellClick时它显示正确。但是在随后的尝试中,尽管在调试中DataSet中有正确的值,但DataGridView3不会显示DataSet。 DataGridView3仅显示表的标题,而不显示数据库中的记录。另外,当我单击此标题(从而对行进行排序)时,也会出现记录。您可以在图片2上看到它。因此问题出在 DataGridView 中,它无法显示存储在他中的数据。我尝试在DataGridView3上使用Update()
,Refresh()
,Show()
方法,但并没有帮助。
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Windows.Forms;
namespace Lab3
{
public partial class Form1 : Form
{
private MySqlConnection connection = null;
private DataSet dataSet = null;
private DataSet dataSetOld = null;
private MySqlDataAdapter doctorDataAdapter = null;
private MySqlDataAdapter patientDataAdapter = null;
private MySqlDataAdapter appointmentDataAdapter = null;
public Form1()
{
InitializeComponent();
}
public void setConnection(MySqlConnection connection)
{
this.connection = connection;
}
//creating DataSet
private DataSet getDataSet()
{
if (dataSet == null)
{
dataSet = new DataSet();
dataSet.Tables.Add("Doctor");
dataSet.Tables.Add("Patient");
dataSet.Tables.Add("Appointment");
}
return dataSet;
}
//set connection with database
public MySqlConnection Connect(string host, int port, string database,
string username, string password)
{
string connStr = "Server=" + host + ";Database=" + database + ";port=" + port + ";User Id=" + username + ";password=" + password;
MySqlConnection connection = new MySqlConnection(connStr);
connection.Open();
return connection;
}
public void FillDataGridView1ByDoctors()
{
getDataSet().Tables["Doctor"].Clear();
doctorDataAdapter = new MySqlDataAdapter(
"SELECT * FROM Doctor", connection);
new MySqlCommandBuilder(doctorDataAdapter);
doctorDataAdapter.Fill(getDataSet(), "Doctor");
dataGridView1.DataSource = getDataSet().Tables["Doctor"];
this.dataGridView1.Columns["id_doctor"].Visible = false;
}
public void FillDataGridView2ByPatients()
{
getDataSet().Tables["Patient"].Clear();
patientDataAdapter = new MySqlDataAdapter(
"SELECT * FROM Patient", connection);
new MySqlCommandBuilder(patientDataAdapter);
patientDataAdapter.Fill(dataSet, "Patient");
dataGridView2.DataSource = getDataSet().Tables["Patient"];
this.dataGridView2.Columns["id_patient"].Visible = false;
dataGridView2.ClearSelection();
}
public void FillDataGridView3ByAppointment(string table, int id)
{
getDataSet().Tables["Appointment"].Reset();
if (table == "Doctor")
{
appointmentDataAdapter = new MySqlDataAdapter(
"SELECT patient.name_patient AS `Имя`, patient.surname_patient, appointment.datetime " +
"FROM patient, appointment, doctor " +
"WHERE doctor.id_doctor = appointment.id_doctor AND patient.id_patient = appointment.id_patient AND doctor.id_doctor = " +
id, connection);
}
else
{
appointmentDataAdapter = new MySqlDataAdapter(
"SELECT doctor.name_doctor, doctor.surname_doctor, doctor.speciality, appointment.datetime " +
"FROM doctor, appointment, patient " +
"WHERE patient.id_patient = appointment.id_patient AND doctor.id_doctor = appointment.id_doctor AND patient.id_patient = " +
id, connection);
}
new MySqlCommandBuilder(appointmentDataAdapter);
appointmentDataAdapter.Fill(dataSet, "Appointment");
dataGridView3.DataSource = getDataSet().Tables["Appointment"];
}
private void dataGridView1_CellClick(object sender,
DataGridViewCellEventArgs e)
{
int selectedRow = dataGridView1.SelectedCells[0].RowIndex;
int key = (int)dataGridView1.Rows[selectedRow].Cells[0].Value;
dataGridView2.ClearSelection();
FillDataGridView3ByAppointment("Doctor", key);
}
private void dataGridView2_CellClick(object sender,
DataGridViewCellEventArgs e)
{
int selectedRow = dataGridView2.SelectedCells[0].RowIndex;
int key = (int)dataGridView2.Rows[selectedRow].Cells[0].Value;
dataGridView1.ClearSelection();
FillDataGridView3ByAppointment("Patient", key);
}
}
}