Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Datagridview仅添加1行_C#_Datagridview - Fatal编程技术网

C# Datagridview仅添加1行

C# Datagridview仅添加1行,c#,datagridview,C#,Datagridview,我试图将我的所有用户帐户添加到gridview中,但在使用foreach代码时,它只是添加datagridview的最后一个值。有没有办法做到所有这些 public DataTable GetResultsTable(string Username) { using (SqlDatabaseClient client = SqlDatabaseManager.GetClient()) { DataRow row = client.ExecuteQueryRow("

我试图将我的所有用户帐户添加到gridview中,但在使用foreach代码时,它只是添加datagridview的最后一个值。有没有办法做到所有这些

public DataTable GetResultsTable(string Username)
{
    using (SqlDatabaseClient client = SqlDatabaseManager.GetClient())
    {
        DataRow row = client.ExecuteQueryRow("SELECT * FROM users WHERE username = '" + Username + "'");
        DataTable table = new DataTable();
        table.Columns.Add("Username".ToString());
        table.Columns.Add("Motto".ToString());
        table.Columns.Add("Email".ToString());
        table.Columns.Add("Homeroom".ToString());
        table.Columns.Add("Health".ToString());
        table.Columns.Add("Energy".ToString());
        table.Columns.Add("Age".ToString());
        DataRow dr = table.NewRow();
        dr["Username"] = "" + row["username"] + "";
        dr["Motto"] = "" + row["motto"] + "";
        dr["Email"] = "" + row["mail"] + "";
        dr["Homeroom"] = "" + row["home_room"] + "";
        dr["Health"] = "" + row["health"] + "";
        dr["Energy"] = "" + row["energy"] + "";
        dr["Age"] = "" + row["age"] + "";
        table.Rows.Add(dr);
        return table;
    }
}
SqlDatabaseManager.Initialize();
  using (SqlDatabaseClient client  = SqlDatabaseManager.GetClient())
  foreach (DataRow row2 in client.ExecuteQueryTable("SELECT * FROM users").Rows)
  {
      dataGridView1.DataSource = GetResultsTable((string)row2["username"]);
  }

DataGridView对象的DataSource属性不应该是一行,而应该是多行。 因此,您可以更改代码以首先创建列表并将其应用为数据源,或者使用以下方法:

dataGridView1.Rows.Add(GetResultsTable(row2["username"].ToString());
在foreach循环中


希望它能帮助您完成以下操作:)。

您可以使用一种方法加载数据

public DataTable fillDataTable()
{
    // select all the columns with expected column name, here I only added 3 columns 
    string query = "SELECT username as Username, motto as Motto, mail as Email FROM users;

    using(SqlConnection sqlConn = new SqlConnection(conSTR))
    using(SqlCommand cmd = new SqlCommand(query, sqlConn))
    {
        sqlConn.Open();
        DataTable dt = new DataTable();
        dt.Load(cmd.ExecuteReader());
        return dt;
    }
}
如下所示设置网格
数据源

dataGridView1.DataSource =fillDataTable();

参数化您的查询。这是一个很大的禁忌:
“从username='“+username+'”
的用户中选择*。请参见此处了解原因:我在使用代码时出错。有什么简单的解决办法吗?System.Windows.Forms.dll中发生“System.InvalidOperationException”类型的未处理异常。其他信息:无法将任何行添加到没有列的DataGridView控件中。必须首先添加列。