2013-12-15 96 views
0

我希望你能幫助我解決這個小問題。我這樣做是爲了將所有用戶帳戶的詳細信息添加到datagridview,現在colums都是空的?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"]); 
    } 

我執行的查詢是

public DataRow ExecuteQueryRow(string CommandText) 
    { 
     DataTable DataTable = ExecuteQueryTable(CommandText); 
     return DataTable.Rows.Count > 0 ? DataTable.Rows[0] : null; 
    } 
+0

你可以更新你的'ExecuteQueryRow'方法代碼的問題? – Damith

+0

是的,我已經做到了。 – Fernando

回答

0
public DataRow GetResultsTable(string Username) 
{ 
using (SqlDatabaseClient client = SqlDatabaseManager.GetClient()) 
{ 
    DataRow row = client.ExecuteQueryRow("SELECT * FROM users WHERE username = '" + Username + "'"); 

    DataRow dr = new DataRow; 
    dr.Columns.Add("Username".ToString()); 
    dr.Columns.Add("Motto".ToString()); 
    dr.Columns.Add("Email".ToString()); 
    dr.Columns.Add("Homeroom".ToString()); 
    dr.Columns.Add("Health".ToString()); 
    dr.Columns.Add("Energy".ToString()); 
    dr.Columns.Add("Age".ToString()); 
    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"] + ""; 

    return dr; 
} 
} 
    SqlDatabaseManager.Initialize(); 
    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()); 
    using (SqlDatabaseClient client = SqlDatabaseManager.GetClient()) 
    foreach (DataRow row2 in client.ExecuteQueryTable("SELECT * FROM users").Rows) 
    { 
    table .Rows.Add(GetResultsTable((string)row2["username"])); 
    } 
    DataGridView.DataSourse=table ;