Do not upload the table to the datagridview and give me a warning of invalid operation exception

1

I have the following problem. I try to upload a table to the datagridview, but when I try it, it gives me a warning with the message invalid operation exception .

I leave the exception details given.

System.InvalidOperationException was unhandled
  HResult=-2146233079
  Message=No row can be added to a DataGridView control that does not have columns. Columns must be added first.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.DataGridViewRowCollection.AddInternal(Boolean newRow, Object[] values)
       at System.Windows.Forms.DataGridViewRowCollection.Add(Object[] values)
       at Proyecto_final1.Worktable.llenar1() in D:\Manejador de items\Proyecto final1\Proyecto final1\Worktable.cs:line 88
       at Proyecto_final1.Worktable.Worktable_Load(Object sender, EventArgs e) in D:\Manejador de items\Proyecto final1\Proyecto final1\Worktable.cs:line 35
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  InnerException: 

And this is the line that VS 2015 points out to me, the table has 8 columns

 conx.Open(); MySqlDataReader leer;
            MySqlCommand cmd = new MySqlCommand("select * from usuario", conx);
            leer = cmd.ExecuteReader(); dgvUsuario.Rows.Clear();
            while (leer.Read())
            {
                this.dgvUsuario.Rows.Add(leer.GetValue(0), leer.GetValue(1), leer.GetValue(2), leer.GetValue(3), leer.GetValue(4), leer.GetValue(5), leer.GetValue(6), leer.GetValue(7));
            }
            conx.Close();

Any colleagues idea?

    
asked by Carlos Alvarez 12.04.2017 в 19:59
source

1 answer

1

You could implement something like this

MySqlCommand cmd = new MySqlCommand("select * from usuario", conx);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

dgvUsuario.DataSource = dt;

in this way the columns will be created automatically according to data datatable

If you want to use the Rows.Add () you should first create the columns

dataGridView1.ColumnCount = 8;
dataGridView1.Columns[0].Name = "NombreCol1";
dataGridView1.Columns[1].Name = "NombreCol2";

DataGridView.Columns

    
answered by 12.04.2017 в 20:07