从数据库中检索数据时,它会在错误的网格视图列中显示它,甚至有些是空的,并且数据也会在错误的列中显示。
我尝试检查所有内容,但未找到任何解决方案,我在此处粘贴了一些代码:
注意:我正在使用xampp数据库进行测试。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
gradeView3Intializer();
}
//ADD TO GDVIEW:
private void populate3(string staffNo, string fName, string lName, string position, string sex, string dob, string salary, string branchNo)
{
dataGridView3.Rows.Add(staffNo, fName, lName, position, sex, dob, salary, branchNo);
}
//Retrive Function
private void retrieve3()
{
dataGridView3.Rows.Clear();
string sql = "SELECT * FROM staff;";
cmd = new MySqlCommand(sql, con);
try
{
con.Open();
adapter = new MySqlDataAdapter(cmd);
adapter.Fill(dt);
//Loop through dt
foreach (DataRow row in dt.Rows)
{
populate3(row[0].ToString(), row[1].ToString(), row[2].ToString(), row[3].ToString(), row[4].ToString(), row[5].ToString(), row[6].ToString(), row[7].ToString());
}
con.Close();
//Clear DT
dt.Rows.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Notice");
con.Close();
}
}
//Intializing GradView with Rows
private void gradeView3Intializer()
{
dataGridView3.ColumnCount = 8;
dataGridView3.Columns[0].Name = "Staff No";
dataGridView3.Columns[1].Name = "First Name";
dataGridView3.Columns[2].Name = "Last Name";
dataGridView3.Columns[3].Name = "Position";
dataGridView3.Columns[4].Name = "SEX";
dataGridView3.Columns[5].Name = "DOB";
dataGridView3.Columns[6].Name = "Salary";
dataGridView3.Columns[7].Name = "Branch No";
dataGridView3.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
// Selection Mode:
dataGridView3.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView3.MultiSelect = false;
}
private void btnUpdate3_Click(object sender, EventArgs e)
{
try
{
string selected = dataGridView3.SelectedRows[0].Cells[0].Value.ToString();
update3(selected, textBox8.Text, textBox7.Text, textBox6.Text, textBox5.Text, textBox25.Text, textBox26.Text, textBox27.Text, textBox28.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Notice");
}
}
private void btnRetrive3_Click(object sender, EventArgs e)
{
retrieve3();
}
private void dataGridView3_MouseClick(object sender, MouseEventArgs e)
{
try
{
textBox8.Text = dataGridView3.SelectedRows[0].Cells[0].Value.ToString();
textBox7.Text = dataGridView3.SelectedRows[0].Cells[1].Value.ToString();
textBox6.Text = dataGridView3.SelectedRows[0].Cells[2].Value.ToString();
textBox5.Text = dataGridView3.SelectedRows[0].Cells[3].Value.ToString();
textBox25.Text = dataGridView3.SelectedRows[0].Cells[4].Value.ToString();
textBox26.Text = dataGridView3.SelectedRows[0].Cells[5].Value.ToString();
textBox27.Text = dataGridView3.SelectedRows[0].Cells[6].Value.ToString();
textBox28.Text = dataGridView3.SelectedRows[0].Cells[7].Value.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Notice");
}
}
答案 0 :(得分:1)
关于以下方面的建议:
string sql =“ SELECT * FROM staff;”;
您的代码依赖于结果字段位于您期望的索引位置,但是SELECT *将根据数据库表结构返回数据,该数据可能与您的期望不符。
最好在SQL语句中显式定义所需的字段名称列表。在下面的示例中,即使在基础表结构中移动了列,我也可以确信确切知道我的结果字段位于哪个位置。
string sql =“ SELECT字段1,字段2,字段3 FROM人员;”;
答案 1 :(得分:0)
最后,经过很多努力,我找到了解决方案。我加了:
dataGridView1.AutoGenerateColumns = true;
dataGridView2.AutoGenerateColumns = true;
dataGridView3.AutoGenerateColumns = true;
对于我的代码的每个retrieve()函数,并为数据库中的每个表定义一个新的DataTable,代码如下:
// NEW DataTables for each Table in Database
DataTable dt = new DataTable();
DataTable dt2 = new DataTable();
DataTable dt3 = new DataTable();
所以这为我解决了问题:)