Pergunta

So I need to show in a Grid a list of numbers that people enter to help them double check their work. It almost works except it doesn't show the numbers. My setup is simple. I have a textbox, they enter the number, when the add button is clicked its added to a BindingList and then that is used as the datasource for the DataGridView.

So, with some help from this Stackoverflow Post I was able to get this halfway working. Unfortunately, even though it appears to add a row the Grid each time it does not correctly show the value. It shows the new rows as empty.

Here is my code.

   public partial class ManualEntry : Form
   {

    BindingList<long> ProjectIDs;
    public ManualEntry()
    {
        InitializeComponent();
        ProjectIDs = new BindingList<long>();
    }

When the add button is clicked, this gets executed.

        private void AddButton_Click(object sender, EventArgs e)
        {
            try
            {
               long temp = long.Parse(textBox1.Text);
               ProjectIDs.Add(temp);
               ProjectsGrid.DataSource = ProjectIDs;
               textBox1.Text = "";//clear the textbox so they can add a new one.
            }
            catch//bring up the badinput form
            {
                BadInput b = new BadInput();
                b.Show();
            }

        }

And so here is the result of adding a few numbers.

add project

If you need any other code from me to help you answer the question, just ask.

Foi útil?

Solução

You haven't told the DataGridViewColumn what to bind to.

Normally you bind to a public property of the bound data type. In this case your data type is long which does not have an appropriate property to bind to.

Wrap your long in a custom class and expose it as a public property.

public class Data
{
    public long Value { get; set; }
}

Bind your column to the Value property. You can do this in the designer, but here is the code:

Column1.DataPropertyName = "Value";

Now instead of long you use Data:

ProjectIDs = new BindingList<Data>();

...

long temp = long.Parse(textBox1.Text);
ProjectIDs.Add(new Data { Value = temp });

Outras dicas

The following post discusses this:

DataGridView bound to BindingList does not refresh when value changed

It looks like your data type in the binding list needs to support the INotifyPropertyChanged interface:

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

NOTE: Revised - my use of INotifyPropertyChanged was incorrect and also appears not to be needed. Now this answer is basically just like Igby's - so I think his is the better one :)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
         ProjectIDs = new BindingList<AwesomeLong>();
         var source = new BindingSource( ProjectIDs, null );
         dataGridView1.DataSource = source;
         dataGridView1.Columns.Add( new DataGridViewTextBoxColumn() );
      }

      BindingList<AwesomeLong> ProjectIDs;
      private int i = 0;
      private void button1_Click( object sender, EventArgs e )
      {
         i++;
         ProjectIDs.Add(new AwesomeLong(i));
      }

   }

   public class AwesomeLong
   {
      public long LongProperty { get; set; }

      public AwesomeLong( long longProperty )
      {
         LongProperty = longProperty;
      }    
   }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top