Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有什么解决办法吗;Gridview在更新新条目后仅显示一个数据;?_C#_Gridview - Fatal编程技术网

C# 有什么解决办法吗;Gridview在更新新条目后仅显示一个数据;?

C# 有什么解决办法吗;Gridview在更新新条目后仅显示一个数据;?,c#,gridview,C#,Gridview,每次单击按钮时,只会显示一行。但它应该显示多行。我在构造函数调用后声明列表。我尝试了gridview.update()和gridview.refresh(),但都不起作用。我找不到这个问题 using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using JournalEntryApp.Model; namespace JournalEntryApp {

每次单击按钮时,只会显示一行。但它应该显示多行。我在构造函数调用后声明列表。我尝试了gridview.update()和gridview.refresh(),但都不起作用。我找不到这个问题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using JournalEntryApp.Model;

namespace JournalEntryApp
{
    public partial class NewDocument : Form
    {
        public NewDocument()
        {
            InitializeComponent();
        }

        List<JEFrom> JEFromsList = new List<JEFrom>();
        List<JETo> JETosList = new List<JETo>();
        JEFrom _jef = null;

        private void NewDocument_Load(object sender, EventArgs e)
        {
            label4.Text = DateTime.Now.ToString("dd-MMM-yyyy");
            using (var db =new JournalContext())
            {
                unitComboBox.DataSource = db.Units.ToList();
                unitComboBox.ValueMember = "Id";
                unitComboBox.DisplayMember = "UnitName";

            }
        }

        private void addToListButton_Click(object sender, EventArgs e)
        {

            if (string.Empty== fromAccountTextBox.Text)
            {
                MessageBox.Show("From Account can not be empty!!!");
            }
            else if (string.Empty == toAccountTextBox.Text)
            {
                MessageBox.Show("To Account can not be empty!!!");
            }
            else
            {
                _jef = new JEFrom{ FromEntryName= fromAccountTextBox.Text , FromEntryDate= DateTime.Now };
                JEFromsList.Add(_jef);
                temporaryDataGridView.DataSource = JEFromsList;

                fromAccountTextBox.Text = string.Empty;
                toAccountTextBox.Text = string.Empty;



            }

        }
    }
}

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows.Forms;
使用JournalEntryApp.Model;
名称空间JournalEntryApp
{
公共部分类NewDocument:Form
{
公共文件()
{
初始化组件();
}
List JEFromsList=新列表();
List JETosList=新列表();
JEFrom _jef=null;
私有void NewDocument_加载(对象发送方,事件参数e)
{
label4.Text=DateTime.Now.ToString(“dd-MMM-yyyy”);
使用(var db=new JournalContext())
{
unitComboBox.DataSource=db.Units.ToList();
unitComboBox.ValueMember=“Id”;
unitComboBox.DisplayMember=“UnitName”;
}
}
私有void addToListButton_单击(对象发送者,事件参数e)
{
if(string.Empty==fromAccountTextBox.Text)
{
MessageBox.Show(“来自帐户不能为空!!!”;
}
else if(string.Empty==toAccountTextBox.Text)
{
MessageBox.Show(“收件人帐户不能为空!!!”;
}
其他的
{
_jef=newjefrom{FromEntryName=fromAccountTextBox.Text,FromEntryDate=DateTime.Now};
JEFromsList.Add(_-jef);
temporaryDataGridView.DataSource=JEFromsList;
fromAccountTextBox.Text=string.Empty;
toAccountTextBox.Text=string.Empty;
}
}
}
}

临时DataGridView无法检测到您已更改了数据源。它仅在数据源发生更改时刷新

temporaryDataGridView.DataSource = null;
temporaryDataGridView.DataSource = JEFromsList;
因此,首先更改数据源null

或者您可以使用bindingSource

private void NewDocument_Load(object sender, EventArgs e)
{
    this.bindingSource1.DataSource = JEFromsList;
    temporaryDataGridView.DataSource = this.bindingSource1;

    label4.Text = DateTime.Now.ToString("dd-MMM-yyyy");
    using (var db =new JournalContext())
    {
        unitComboBox.DataSource = db.Units.ToList();
        unitComboBox.ValueMember = "Id";
        unitComboBox.DisplayMember = "UnitName";
    }
}
在按钮中单击

JEFromsList.Add(_jef);
bindingSource1.ResetBindings(true);

在类构造函数调用之后,初始化JEFromsList的位置是什么;List JETosList=新列表();JEFrom _jef=null你可以粘贴完整的代码,除了ui设计代码。我已经更新了代码。请检查一下。