Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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# DataGridView未以编程方式加载数据_C#_Winforms_Datagridview - Fatal编程技术网

C# DataGridView未以编程方式加载数据

C# DataGridView未以编程方式加载数据,c#,winforms,datagridview,C#,Winforms,Datagridview,我正在使用一个Windows应用程序,在该应用程序中,当用户单击按钮(add)时,我从一个文本框获取一个输入,并将其添加到DataGridView 我的问题是,当我第一次添加文本时,它工作正常,并将其添加到网格中。 当我添加新文本时,它不会添加到DataGridView。一旦用同一个对象关闭并重新打开表单,我就可以看到它了 代码: private void btnAddInput_单击(对象发送者,事件参数e) { 如果(数据==null) 数据=新列表(); 如果(!string.IsNul

我正在使用一个Windows应用程序,在该应用程序中,当用户单击按钮(
add
)时,我从一个文本框获取一个输入,并将其添加到DataGridView

我的问题是,当我第一次添加文本时,它工作正常,并将其添加到网格中。
当我添加新文本时,它不会添加到DataGridView。一旦用同一个对象关闭并重新打开表单,我就可以看到它了

代码:

private void btnAddInput_单击(对象发送者,事件参数e)
{
如果(数据==null)
数据=新列表();
如果(!string.IsNullOrWhiteSpace(txtInput.Text))
{
Data.Insert(Data.Count,新输入()
{
Name=txtInput.Text,
Value=string.Empty
});
}
其他的
{
MessageBox.Show(“请输入参数值”,“信息”,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
txtInput.Text=“”;
gridViewInputs.DataSource=数据;
}

我不确定是什么原因导致第二次单击“添加”按钮时记录未添加到网格中

在设置新的数据源之前,可以将
DataGridView.DataSource
设置为
null

这将导致DataGridView使用源
列表中的新数据刷新其内容

只有当
数据源
引用不同于当前引用或设置为空时,才会重置基础

请注意,当您重置
数据源
时,
行删除
事件会引发多次(每删除一行一次)

我建议将
列表
更改为,因为对列表的任何更改都将自动反映出来,并且如果需要,它将允许从DataGridView中删除行:使用
列表
作为数据源将不允许删除行

BindingList<Inputs> InputData = new BindingList<Inputs>();
如果要继续使用
列表
,请添加重置
DataGridView.DataSource所需的代码

private void btnAddInput_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(textValue))
    {
        //(...)
        dataGridView1.DataSource = null;
        dataGridView1.DataSource = InputData;
        txtInput.Text = "";
    }
    //(...)

您是否收到任何错误?请尝试使用
gridViewInputs.DisplayMember=“Name”
。当您不需要跟踪索引时,为什么要使用
Data.Insert()
?我会使用simple
Data.Add()
——当有一个非常填充的列表和删除元素时,它也会有性能优势。
public class Inputs
{
    public string Name { get; set; }
    public string Value { get; set; }
}

internal BindingList<Inputs> InputData = new BindingList<Inputs>();

private void Form1_Load(object sender, EventArgs e)
{
    dataGridView1.DataSource = InputData;
}

private void btnAddInput_Click(object sender, EventArgs e)
{
    string textValue = txtInput.Text.Trim();
    if (!string.IsNullOrEmpty(textValue))
    {
        InputData.Add(new Inputs() {
            Name = textValue,
            Value = "[Whatever this is]"
        });
        txtInput.Text = "";
    }
    else
    {
        MessageBox.Show("Not a valid value");
    }
}
private void btnAddInput_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(textValue))
    {
        //(...)
        dataGridView1.DataSource = null;
        dataGridView1.DataSource = InputData;
        txtInput.Text = "";
    }
    //(...)