0

I have a winform app which works correctly but I want to choose the column names, so I created the new columns.

When I open the application now, I have the blank manually generated columns, and then the autogenerated columns containing the data.

so I did:

dataGridView1.AutoGenerateColumns = false;

(on Form1 load)

and then:

List<Movimenti> source = dbo.RecuperaMovimenti(contoSelezionato);
dataGridView1.DataSource = source;

but I get an error:

object reference not set to an instance of an object.

I post some code which should help you to locate the problem:

When I select an item from my listbox, I want the datagridview to load the data for the selected user:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    DboInfo dbo = new DboInfo();

    try
    {
        contoSelezionato = listBox1.SelectedItem.ToString();
    }
    catch
    {}

         lblConto.Text = contoSelezionato;
         lblConto.Visible = true;
         lblTotale.Text = totale;
         lblTotale.Visible = true;

        try
        {
            List<Movimenti> source = dbo.RecuperaMovimenti(contoSelezionato);
            dataGridView1.DataSource = source;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }

dbo method for getting the data:

public List<Movimenti> RecuperaMovimenti(string contoSelezionato)
{            
    List<Movimenti> risultato = new List<Movimenti>();

    SQLiteConnection m_dbConnection = new SQLiteConnection();
        try
        {
            m_dbConnection = OpenConnection();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message, ex);
        }

        SQLiteDataReader rdr;

        string sql = "SELECT id_movimento, data_movimento, descrizione_movimento, importo_movimento FROM movimenti WHERE nome_conto = '"+contoSelezionato+"'";
        SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
        rdr = command.ExecuteReader();

        while (rdr.Read())
        {
            Movimenti MOV = new Movimenti();

            MOV.id = (Int64)rdr["id_movimento"];
            //MOV.nome_conto = (string)rdr["nome_conto"];
            MOV.data = (string)rdr["data_movimento"];
            MOV.descrizione = (string)rdr["descrizione_movimento"];
            MOV.importo = Convert.ToInt32(rdr["importo_movimento"]);

            risultato.Add(MOV);
        }

        rdr.Close();
        m_dbConnection.Close();

        return risultato;
    }

edit:

enter image description here

that's a picture. My data doesn't go into my columns, it just creates auto generated columns

Gino Perla
  • 91
  • 1
  • 10
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Hamid Pourjam Mar 05 '15 at 10:23
  • On which line did you get the exception mentioned? – Steve Mar 05 '15 at 10:25
  • And by the way, if your problem is just how to change the header of each column you could change them using `dataGridView.Columns[index].HeaderText = "Nome della colonna";` – Steve Mar 05 '15 at 10:28
  • http://i.imgur.com/tiJHt5D.jpg that's a picture. My data doesn't go into my columns, it just creates auto generated columns – Gino Perla Mar 05 '15 at 10:30
  • dataGridView.Columns[index].HeaderText = "Nome della colonna"; seems to work only for manually generated columns and not for automatic generated ones – Gino Perla Mar 05 '15 at 10:34
  • 1
    That seems strange to me. I can set the HeaderText in any columns However, I would like to see the code that creates the columns manually and how do you bind these column to the datasource that you set in you RecuperaMovimenti method – Steve Mar 05 '15 at 10:44
  • Man... that was the problem. I didn't create the columns manually, I used the wizard and something weird happened.. it works now =) The name of the columns wasn't the same of the properties in my "movimenti" class. Thank you for your help =) `private void generaColonne() { DataGridViewColumn col = new DataGridViewTextBoxColumn(); col.DataPropertyName = "ID"; col.HeaderText = "Id Movimento"; col.Name = "foo"; dataGridView1.Columns.Add(col); }` – Gino Perla Mar 05 '15 at 10:48

2 Answers2

0

May be, I think errors exist here:

contoSelezionato = listBox1.SelectedItem.ToString();

Can't get value from listBox.

Or here:

while (rdr.Read())
        {
            Movimenti MOV = new Movimenti();

            MOV.id = (Int64)rdr["id_movimento"];
            //MOV.nome_conto = (string)rdr["nome_conto"];
            MOV.data = (string)rdr["data_movimento"];
            MOV.descrizione = (string)rdr["descrizione_movimento"];
            MOV.importo = Convert.ToInt32(rdr["importo_movimento"]);

            risultato.Add(MOV);
        }

One or more item doesn't contain anything value.

0

This or change

col.DataPropertyName = "ID"

by

col.DataPropertyName = "id"

Movimenti class has "id" property, not "ID"