Kelly Anaya
Kelly Anaya

Reputation: 21

How to prevent my gridview from creating additional rows in c#?

I try researching this everywhere but it appears when I run my windows form application and display my database. Each it keeps automatically skipping and adding rows.
Here is a screen shot of my gridview enter image description here

Here is also my code where it displays the database when I click a button:

    try
    {
        conn = new MySqlConnection();
        conn.ConnectionString = connstring;

        query = "INSERT INTO schedule(name) VALUES(@namevalue)";
        MySqlCommand cmd = new MySqlCommand(query, conn);
        cmd.Parameters.AddWithValue("@namevalue", this.nameEmp.Text);

        conn.Open();

        cmd.ExecuteNonQuery();


        MessageBox.Show("Connection Success");
        myadapt = new MySqlDataAdapter();
        string sq = "SELECT * FROM schedule";
        myadapt.SelectCommand = new MySqlCommand(sq, conn);
        tb = new DataTable();
        myadapt.Fill(tb);

        BindingSource src = new BindingSource();
        src.DataSource = tb;

        dataGridView1.DataSource = src;

    }
    catch (MySqlException ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Upvotes: 0

Views: 64

Answers (2)

Kelly Anaya
Kelly Anaya

Reputation: 21

actually, I figured it out. I had in that code above the statement INSERT twice in my program. I removed it and redid my database on my server and fixed the problem. So sorry for the trouble.

Upvotes: 0

Hailin FU
Hailin FU

Reputation: 492

Do you mean you want to show those rows which name column is not empty? Gridview can just show the table you query from the DB,so you can change the SQL code to get the right table, for example:change SELECT * FROM schedule to SELECT * FROM schedule WHERE schedule(name) IS NOT NULL.

Upvotes: 1

Related Questions