DataGridView missing new row with empty data source

I am creating an application that uses a DataGridView to create and modify a local list that will later be imported into the system. It must be editable by the user (click and write to DGV), and it must also support import from csv, which means that I need two-way synchronization between DGV and the data source.

I installed the DGV DataSource in the BindingList<Client>following way:

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

DGV, ( ), DGV. DataSource , ​​ SQL, DGV. " ", ? , , .

AllowUserToAddRows AllowUserToDeleteRows true.

. " " . , DGV. Dgv

+4
3

MSDN :

DataGridView , , IBindingList.AllowNew true.

, BindingList false , :

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

, DataPropertyName.

  • . .

  • -, DataPropertyName. Client.

enter image description here


Displaying Empty Row

DGV . , ,

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

See configuration here 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 setting up the datagridview edit column. In this DATA menu with the name DataPropertyName, you can change the non-property to the actual database column name. He will then show you this column information. If you have multiple columns, then set up all the information like this.

0
source

All Articles