简体   繁体   English

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

[英]Data Retrieval in Wrong Columns of DataGridView in C#

While retrieving the data from database it shows it in wrong grid view columns and even some are empty and also the data is shown in wrong columns.从数据库中检索数据时,它显示在错误的网格视图列中,甚至有些是空的,并且数据显示在错误的列中。

I tried checking everything and didn't find any solution, I pasted some of my code here:我尝试检查所有内容并没有找到任何解决方案,我在这里粘贴了一些代码:

Note: I am using xampp database for testing purposes.注意:我使用 xampp 数据库进行测试。

Here is a screen shot of the problem:这是问题的屏幕截图: 在此处输入图片说明

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");
        }
    }

Just a suggestion, in relation to:只是一个建议,关于:

string sql = "SELECT * FROM staff;";字符串 sql = "SELECT * FROM 员工;";

Your code is relying on the the result fields being in the index positions you are expecting, but SELECT * will return data as per the database table structure, which may not match your expectation.您的代码依赖于您期望的索引位置中的结果字段,但 SELECT * 将根据数据库表结构返回数据,这可能与您的期望不符。

It is probably better to explicitly define the list of field names you want in your SQL statement.最好在 SQL 语句中明确定义所需的字段名称列表。 In the following example I can be confident in knowing exactly which positions my result fields are in, even if a column is moved in the underlying table structure.在下面的示例中,即使列在基础表结构中移动,我也可以确信确切地知道我的结果字段位于哪个位置。

string sql = "SELECT Field1, Field2, Field3 FROM staff;"; string sql = "SELECT Field1, Field2, Field3 FROM Staff;";

Finally after lots of struggles, I found a solution.终于,经过一番折腾,终于找到了解决办法。 I added:我补充说:

dataGridView1.AutoGenerateColumns = true;
dataGridView2.AutoGenerateColumns = true; 
dataGridView3.AutoGenerateColumns = true;

to each retrieve() functions of my code and defined a new DataTable for each of the table in database, here is the code:我的代码的每个retrieve()函数并为数据库中的每个表定义了一个新的DataTable,这里是代码:

// NEW DataTables for each Table in Database
DataTable dt = new DataTable();
DataTable dt2 = new DataTable();
DataTable dt3 = new DataTable();

So this solved the problem for me :)所以这为我解决了这个问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM