Define DataGridView column width when loading from a DataSet? [duplicate]

0

This works well for me

 
List mascota = new List();

dataGridView1.DataSource = mascota;
                    getEncabezado();


  private void getEncabezado()
    {
      dataGridView1.Columns["idmascota"].Visible = false;
      dataGridView1.Columns["nombremascota"].Width = 150;
      dataGridView1.Columns["nombremascota"].HeaderText = "Codigo";
    }

and I want to use it in the same way


    OleDbConnection connection = Connection.getConnection();
               string sql = "SELECT idmascota,nombremascota,nombremascota  FROM Mascota";

               OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
               DataSet ds = new DataSet();

               dataadapter.Fill(ds, "DTMascota");
               dataGridView1.DataSource = ds;
               dataGridView1.DataMember = "DTMascota";

               getEncabezado(); /// aqui hago el llamado a mi metodo para definir las caracteristicas del encabezado de mi dvgrid

               dataGridView1.Refresh();



    private void getEncabezado()
                {
                  dataGridView1.Columns["idmascota"].Visible = false;
                  dataGridView1.Columns["nombremascota"].Width = 150;    //**Error: Excepción no controlada del tipo 'System.NullReferenceException' en System.Windows.Forms.dll //  Información adicional: Referencia a objeto no establecida como instancia de un objeto.  *//
                  dataGridView1.Columns["nombremascota"].HeaderText = "Codigo";
                }

I get this error and try these two methods and it does not work method1 , method2

    
asked by Rodrigo Rodriguez 12.10.2017 в 15:20
source

2 answers

-1

In my experience the only time I've seen a situation like this is if you try to set the ".DataSource" before initializing the DataGrid.

I recommend that you set your properties from the .OnLoad event of the form.

You can create a method to load the data, assign the DataSource of the grid and modify the properties.

    private void Form1_Load(object sender, EventArgs e)
    {

        GetData();
    }

    private void GetData() {
        OleDbConnection connection = Connection.getConnection();
        string sql = "SELECT * FROM Mascota";

        OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
        DataSet ds = new DataSet();

        dataadapter.Fill(ds, "DTMascota");
        dataGridView1.DataSource = ds;
        dataGridView1.DataMember = "DTMascota";

        getEncabezado();

        dataGridView1.Refresh();

        //Setting properties

        dataGridView1.Columns["idmascota"].Visible = false;
        dataGridView1.Columns["nombremascota"].Width = 150;    
        dataGridView1.Columns["nombrecota"].HeaderText = "Codigo";
    }
    
answered by 12.10.2017 в 16:01
-1

From what I see, the data obtained does not have the column "nombremascota" or it does not come with that name, so when you want to refer to the column using the column name as an index, it marks you the error.

Do a debug and place a break right on the line where the exception marks you and inspect the content of. Columns to see how many columns and names were created.

    
answered by 16.10.2017 в 05:38