简体   繁体   中英

C#: DataGridView shows only one row

I have a problem with a datagridview in C#. I get the data via query from a mysql-database. But for some reason, only the first row from the result is displayed in the gridview. I use the following code to do this stuff:

MySqlCommand command = new MySqlCommand(query, Globals.Connection);
        reader = command.ExecuteReader();
        while (reader.Read())
        {
            object[] dataset = new object[7];
            dataset[0] = reader["sto_name"];
            dataset[1] = reader["esss"];
            dataset[2] = reader["onl_name"];
            dataset[3] = reader["rpc_id"];
            if (reader["datum_aufstellung"].ToString() != "0")
            {
                dataset[4] = getDate(reader["datum_aufstellung"]);
            }
            else
            { 
                dataset[4] = "Kein Datum gesetzt";
            }
            if (reader["datum_abbau"].ToString() != "0")
            {
                dataset[5] = getDate(reader["datum_abbau"]);
            }
            else
            {
                dataset[5] = "Kein Datum gesetzt";
            }
            dataset[6] = reader["id"];
            dataGridView1.Rows.Add(dataset);
        }

It worked, a few lines of code earlier. ^^ Do you have an idea, what the problem is?

UPDATE: The content of the while loop is executed only one time. I was also wondering about that fact, before I asked my question here. But if I execute the Query in an MySQL-Client, it returns more rows than one.

UPDATE 2: I've noticed, that while the whole content of the while-loop is commented, the loop is executed exactly the same times as there are rows in the query-result. Any ideas?

UPDATE 3: It seems that everything after

dataGridView1.Rows.Add(dataset);

is not executed. Not only in the loop, but in the whole function. But why?

PROBLEM SOLVED: There was nothing wrong with the code posted here. I had an event in the rest of the code, which executed something, when a row in the dgv is entered. I suppose the loop breaked, when that happened. After removing that event, the dgv was properly filled.

Check the RowCount property on the Grid. It maybe set to 1 if so increase it to the desired amount of rows.

You should use a DataAdapter and use it to populate a DataTable. Create a method for grabbing the data like this :-

public DataTable GetDataTable()
        {
            DataTable dt = new DataTable();

            using (SqlConnection con = new SqlConnection (@"YourCOnnectionString"))
            {
                using (SqlCommand cmd = new SqlCommand (query, con))
                {
                    var adaptor = new SqlDataAdapter ();
                    adaptor.SelectCommand = cmd;

                    con.Open();
                    adaptor.Fill(dt);

                    return dt;

                }
            }
        }

Then You can reference it with your DataGrid :-

DataTable Result = GetDataTable();

DatagridView1.DataSource = Result;

There is another easy way. You can use DataTable instead of dataset and set the dataGridView's DataSource to that DataTable. It will solve your problem and also using that you can set the column Headers easily.

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