简体   繁体   English

Winforms如何绑定到列表 <long> DatagridView并正确显示

[英]Winforms How to Bind to a List<long> DatagridView and have it show correctly

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. 我有一个文本框,他们输入数字,当单击添加按钮时,将其添加到BindingList中,然后将其用作DataGridView的数据源。

So, with some help from this Stackoverflow Post I was able to get this halfway working. 因此,在此Stackoverflow帖子的帮助下,我得以中途完成了工作。 Unfortunately, even though it appears to add a row the Grid each time it does not correctly show the value. 不幸的是,即使每次它未正确显示该值时,它似乎都在Grid中添加了一行。 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. 所以这是加几个数字的结果。

添加项目

If you need any other code from me to help you answer the question, just ask. 如果您需要我提供任何其他代码来帮助您回答问题,请提出。

You haven't told the DataGridViewColumn what to bind to. 您尚未告诉DataGridViewColumn绑定到什么。

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. 在这种情况下,您的数据类型很long ,没有合适的属性绑定。

Wrap your long in a custom class and expose it as a public property. 将您的long包裹在一个自定义类中,并将其公开为公共属性。

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

Bind your column to the Value property. 将您的列绑定到Value属性。 You can do this in the designer, but here is the code: 您可以在设计器中执行此操作,但是代码如下:

Column1.DataPropertyName = "Value";

Now instead of long you use Data : 现在使用Data不再需要long

ProjectIDs = new BindingList<Data>();

... ...

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

The following post discusses this: 以下帖子对此进行了讨论:

DataGridView bound to BindingList does not refresh when value changed 更改值时绑定到BindingList的DataGridView不会刷新

It looks like your data type in the binding list needs to support the INotifyPropertyChanged interface: 绑定列表中的数据类型似乎需要支持INotifyPropertyChanged接口:

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

NOTE: Revised - my use of INotifyPropertyChanged was incorrect and also appears not to be needed. 注意:已修​​订-我对INotifyPropertyChanged的使用是不正确的,并且似乎也不需要。 Now this answer is basically just like Igby's - so I think his is the better one :) 现在,这个答案基本上和Igby的一样-所以我认为他是更好的:)

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;
      }    
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM