简体   繁体   English

DataGridView缺少具有空数据源的新行

[英]DataGridView missing new-row with empty datasource

I'm creating an application which uses a DataGridView to create and modify a local List that will be imported into a system later. 我正在创建一个应用程序,它使用DataGridView创建和修改稍后将导入系统的本地List。 It should be editable by the user (clicking and writing in the DGV), and it should also support importing from csv which means I need 2-way sync between DGV and datasource. 它应该由用户编辑(在DGV中单击和写入),它还应该支持从csv导入,这意味着我需要DGV和数据源之间的双向同步。

I've set the DGV's DataSource to a BindingList<Client> like this: 我已经将DGV的DataSource设置为BindingList<Client>如下所示:

//In my seperate Client class-file
public class Client
{
    private string _name;
    private string _mac;
    private string _status;

    public Client(string pcnavn, string MAC)
    {
        _pcnavn = pcnavn;
        _mac = mac;
    }

    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }

    public string MAC
    {
        get
        {
            return _mac;
        }
        set
        {
            _mac = value;
        }
    }

    public string Status
    {
        get
        {
            return _status;
        }
        set
        {
            _status = value;
        }
    }
}

//In my mainform class

public BindingList<Client> clientDataSource = new BindingList<Client>();

public MainForm()
{
InitializeComponent();
//clienDataGridView.AutoGenerateColumns = false; //The duplicate columns where fixed by setting DataPropertyName for all columns.
clientDataGridView.DataSource = clientDataSource;
}

With this approach, the DGV is generated, but it is empty(only headers), so the user can't add a row by using the DGV. 使用这种方法,生成DGV,但它是空的(仅标题),因此用户无法使用DGV添加行。 Without DataSource, it displays a blank row like a SQL editor so I can create rows manually in the DGV. 没有DataSource,它会像SQL编辑器一样显示一个空白行,这样我就可以在DGV中手动创建行。 How can I show the "new item"-row when linked to an empty data source? 如何在链接到空数据源时显示“新项目”? All examples I've found uses non-empty datasources. 我发现的所有示例都使用非空数据源。

AllowUserToAddRows and AllowUserToDeleteRows are set to true . AllowUserToAddRowsAllowUserToDeleteRows设置为true

This picture shows my problem. 这张照片显示了我的问题。 The "first row" is missing when using datasource. 使用数据源时缺少“第一行”。 It's not possible to add data by typing in the DGV. 输入DGV无法添加数据。 DGV

When looking at MSDN , I found this: 在查看MSDN时 ,我发现了这个:

If the DataGridView is bound to data, the user is allowed to add rows if both this property and the data source's IBindingList.AllowNew property are set to true. 如果DataGridView绑定到数据,则如果此属性和数据源的IBindingList.AllowNew属性都设置为true,则允许用户添加行。

It seems BindingList has this property as false by default, so this little trick fixed it: 看来BindingList默认将此属性设置为false ,所以这个小技巧修复了它:

public BindingList<Client> clientDataSource = new BindingList<Client>() { AllowNew = true };

I guess you are missing the DataPropertyName setting. 我猜你错过了DataPropertyName设置。

  1. First you have to add columns to your data grid. 首先,您必须向数据网格添加列。 Go to add columns option and add the required columns using the wizard. 转到添加列选项并使用向导添加所需的列。

  2. Secondly you have to edit each column in the wizard mode and set the DataPropertyName . 其次,您必须在向导模式下编辑每一列并设置DataPropertyName Basically for each grid column you have created you need to give the exact bound property name of Client class. 基本上,对于您创建的每个网格列,您需要提供Client类的精确绑定属性名称。

在此输入图像描述


Displaying Empty Row

When you bind a empty list to a DGV default row will be removed. 将空列表绑定到DGV时,将删除默认行。 If you need to display an empty row then do like this, 如果你需要显示一个空行,那么这样做,

//Adding a client object to the collection so that an empty row will be inserted to DGV
clientDataSource.Add(new Client());
clientDataGridView.DataSource = clientDataSource;

See config here 请参阅此处的配置 请参阅此处的配置

try
{

  int id =Convert.ToInt32( textBox1.Text);
    string query = "SELECT * FROM ngo.inser WHERE ID LIKE '%" + id + "%'";
    command = new MySqlCommand(query, connection);
    adapter = new MySqlDataAdapter(command);
    table = new DataTable();
    adapter.Fill(table);

    dataGridView1.DataSource = table;
}
catch(Exception ex)
{
    MessageBox.Show(ex.Message);
}

And go to the datagridview edit column setting. 然后转到datagridview编辑列设置。 In that DATA menu having DataPropertyName change non property into actual database column name. 在具有DataPropertyName的DATA菜单中,将非属性更改为实际的数据库列名。 Then it will show you that column information . 然后它会显示该列信息。 If you have multiple column then set all information like that. 如果您有多列,则设置所有类似的信息。

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

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