[英]C#: DataGridView shows only one row
I have a problem with a datagridview in C#. 我在C#中的datagridview有问题。 I get the data via query from a mysql-database. 我通过查询从mysql数据库中获取数据。 But for some reason, only the first row from the result is displayed in the gridview. 但是由于某种原因,结果的第一行仅显示在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. 更新: while循环的内容仅执行一次。 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. 但是,如果我在MySQL客户端中执行查询,则它返回的行多于一行。
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. 更新2:我注意到,尽管注释了while循环的全部内容,但循环的执行时间与查询结果中的行完全相同。 Any ideas? 有任何想法吗?
UPDATE 3: It seems that everything after 更新3:似乎之后的一切
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. 当在dgv中输入一行时,我在代码的其余部分中都有一个事件,该事件执行了某些操作。 I suppose the loop breaked, when that happened. 我想当这种情况发生时,循环就中断了。 After removing that event, the dgv was properly filled. 删除该事件后,dgv已正确填充。
Check the RowCount property on the Grid. 检查网格上的RowCount属性。 It maybe set to 1 if so increase it to the desired amount of rows. 如果将其增加到所需的行数,则可以将其设置为1。
You should use a DataAdapter
and use it to populate a DataTable. 您应该使用DataAdapter
并使用它来填充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 :- 然后,您可以使用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. 您可以使用DataTable代替数据集,并将dataGridView的DataSource设置为该DataTable。 It will solve your problem and also using that you can set the column Headers easily. 它不仅可以解决您的问题,还可以使用它轻松设置列标题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.