C# Datagridview行复制
每次向datagridview添加新行时,都会从当前行之前添加的行中获得重复的行。我正在使用一个外部mysql数据库C# Datagridview行复制,c#,mysql,datagridview,C#,Mysql,Datagridview,每次向datagridview添加新行时,都会从当前行之前添加的行中获得重复的行。我正在使用一个外部mysql数据库 private void FillTable() { try { MySqlConnection DBConn = new MySqlConnection(MysqlConnectionString); MySqlCommand Command = new MySqlCommand("SELE
private void FillTable()
{
try
{
MySqlConnection DBConn = new MySqlConnection(MysqlConnectionString);
MySqlCommand Command = new MySqlCommand("SELECT DISTINCT Voornaam, Achternaam, Leeftijd, GBDag, GBMaand, GBJaar, Adres, Woonplaats, Postcode FROM Libra_Klanten ORDER BY Voornaam", DBConn);
MySqlDataAdapter Adapter = new MySqlDataAdapter(Command.CommandText, DBConn);
MySqlCommandBuilder Builder = new MySqlCommandBuilder(Adapter);
DBConn.Open();
Command.ExecuteNonQuery();
Adapter.Fill(CustomersTable);
CustomersDataViewer.DataSource = CustomersTable;
CustomersDataViewer.DataMember = CustomersTable.TableName;
CustomersDataViewer.AutoResizeColumns();
DBConn.Close();
DBConn.Dispose();
Command.Dispose();
Adapter.Dispose();
Builder.Dispose();
}
catch (Exception Ex)
{
Debug.WriteLine(Ex.Message);
}
}
这是insert语句的代码:
private void ButtonInsertCustomer_Click(object sender, EventArgs e)
{
if (TextBoxVoornaam.TextLength > 0 && TextBoxAchternaam.TextLength > 0 && NumLeeftijd.Value > 0 && DateGeboorteDatum.Value != null && TextBoxAdres.TextLength > 0 && TextBoxWoonplaats.TextLength > 0 && TextBoxPostcode.TextLength > 0)
{
try
{
MySqlConnection DBConn = new MySqlConnection(MysqlConnectionString);
MySqlCommand Command = new MySqlCommand("INSERT INTO Libra_Klanten (Voornaam, Achternaam, Leeftijd, GBDag, GBMaand, GBJaar, Adres, Woonplaats, Postcode) values ('" + TextBoxVoornaam.Text + "','" + TextBoxAchternaam.Text + "','" + NumLeeftijd.Value + "','" + DateGeboorteDatum.Value.Day + "','" + DateGeboorteDatum.Value.Month + "','" + DateGeboorteDatum.Value.Year + "','" + TextBoxAdres.Text + "','" + TextBoxWoonplaats.Text + "','" + TextBoxPostcode.Text + "')", DBConn);
DBConn.Open();
Command.ExecuteNonQuery();
DBConn.Close();
DBConn.Dispose();
Command.Dispose();
FillTable();
}
catch (MySqlException ex)
{
Debug.WriteLine(ex.Message);
}
}
else
{
MessageBox.Show("Vul alle velden in.");
}
}
每次我向数据库中插入新客户时,都会显示我添加的前几行的副本。我已经尝试在datagridview上使用
.Clear()
,但没有成功 由于datagridview绑定到数据源,您需要清除数据源本身
运行
CustomersTable.Clear()
在FillTable
方法的开头,以确保数据源中没有数据;datagridview将反映这一点。您是否尝试调用CustomersTable.Clear()
在FillTable
方法的开头?清除datagridview不会删除以前的数据,您需要清除其数据源。请尝试在FillTable()方法中声明CustomerTable。这两种方法都不起作用。我试图在开头添加这个,但没有任何效果:如果(CustomersDataViewer.DataSource!=null){CustomersDataViewer.DataSource=null;}如果调用CustomersDataViewer.DataSource=null
,请确保调用CustomersDataView.Clear()
或CustomerDataView.RowCount=0
删除所有行。