C#中DataGridView的错误列中的数据检索

问题描述 投票:1回答:2

从数据库中检索数据时,它会在错误的网格视图列中显示它,甚至一些是空的,并且数据也显示在错误的列中。

我尝试检查所有内容但没有找到任何解决方案,我在这里粘贴了一些代码:

注意:我正在使用xampp数据库进行测试。

这是问题的屏幕截图:enter image description here

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");
        }
    }
c# database xampp
2个回答
1
投票

只是一个建议,关于:

string sql =“SELECT * FROM staff;”;

您的代码依赖于您期望的索引位置中的结果字段,但SELECT *将根据数据库表结构返回数据,这可能与您的预期不符。

最好在SQL语句中明确定义所需的字段名称列表。在下面的示例中,即使在基础表结构中移动了列,我也可以确切地知道结果字段的确切位置。

string sql =“SELECT Field1,Field2,Field3 FROM staff;”;


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();

所以这解决了我的问题:)

© www.soinside.com 2019 - 2024. All rights reserved.