title: Data Retrieval in Wrong Columns of DataGridView in C#
body: <p>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.</p>
  
  <p>I tried checking everything and didn't find any solution, I pasted some of my code here:</p>
  
  <p>Note: I am using xampp database for testing purposes. </p>
  
  <p>Here is a screen shot of the problem:
  <a href="https://i.stack.imgur.com/8A1lE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8A1lE.png" alt="enter image description here"></a></p>
  
  <pre><code>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");
          }
      }
  </code></pre>