Winforms如何绑定到列表< long>DatagridView并正确显示 [英] Winforms How to Bind to a List<long> DatagridView and have it show correctly

查看:55
本文介绍了Winforms如何绑定到列表< long>DatagridView并正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我需要在网格中显示人们输入的数字列表,以帮助他们仔细检查工作.除了不显示数字外,它几乎可以正常工作.我的设置很简单.我有一个文本框,他们输入数字,当单击添加按钮时,将其添加到BindingList中,然后将其用作DataGridView的数据源.

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.

因此,在此 Stackoverflow帖子的帮助下,我能够得到这个半途而废.不幸的是,即使每次它未正确显示该值时,它似乎都在Grid中添加了一行.它将新行显示为空.

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.

这是我的代码.

   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.

推荐答案

您尚未告诉DataGridViewColumn要绑定的内容.

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

通常,您绑定到绑定数据类型的公共属性.在这种情况下,您的数据类型为 long ,该数据类型没有要绑定的适当属性.

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.

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

将列绑定到 Value 属性.您可以在设计器中执行此操作,但这是代码:

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

Column1.DataPropertyName = "Value";

现在使用 Data 代替 long :

ProjectIDs = new BindingList<Data>();

...

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

这篇关于Winforms如何绑定到列表&lt; long&gt;DatagridView并正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆