简体   繁体   中英

Duplicating items in DataGridView c#

I have this code where I put data to the DataTable from where I show everything on DataGridView . But when I look it contains information which supposed to be in file but its repeated twice. 在此处输入图片说明

Code to retrieve data from mysql database:

MySqlDataAdapter mySqlDataAdapter;
    DataSet DS0 = new DataSet();
    DataTable DT0;        
    string gender;
private void Filter()
{
     ViewG.DataSource = null;
     ViewG.Rows.Clear();
     command.CommandText = "SELECT * FROM `table2` WHERE s1q2 = @gender";
     command.Parameters.Add("@gender", MySqlDbType.VarChar);
     command.Parameters["@gender"].Value = gender;
     DT0 = DS0.Tables.Add("1Filter");
     mySqlDataAdapter = new MySqlDataAdapter(command.CommandText, connection);

     connection.Open();
     mySqlDataAdapter.SelectCommand = command;
     mySqlDataAdapter.Fill(DS0.Tables["1Filter"]);
     ViewG.DataSource = DS0.Tables["1Filter"];
     connection.Close();   
}

Initially, on the start it retrieves all information from the database code ( SELECT * FROM table ) and displays on the DataGridView . And it works fine, but when I try to use filters to retrieve only for example "Females" problem occurs.

For full data I use:

mySqlDataAdapter.Fill(DS0.Tables["Full"]);
ViewG.DataSource = DS0.Tables["Full"];

For Filtered data:

mySqlDataAdapter.Fill(DS0.Tables["1Filter"]);
ViewG.DataSource = DS0.Tables["1Filter"];

If I run query used for filter on the application startup it does not duplicate and show correctly.

EDIT: SOLVED

From the code posted here, gender string is not assigned any value. So, your query be applying any filter that you want.

Thanks for you effort I found where the problem was. I used temp table on MySql and for some reason server did not dropped this table after connection is closed. So on the new query it added same items on the same table....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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