我有最糟糕的時間搞清楚了。我必須使用我還沒完全理解的MVVM,並將填充了SQL數據的列表綁定到radGridView。聽起來很容易......但我無法弄清楚。數據不顯示,我認爲這是因爲我綁定它是錯誤的。以下是我的相關代碼。任何幫助,將不勝感激!將GridView綁定到列表
C#(HistoryAuditLogViewModel.cs):
#region Private Fields
private DatabaseConnectionSetting dbSetting;
private string tableName = "Manufacturers";
private int primaryKeyID = 1;
private string entryID;
private string manufacturerID;
private string manufacturerName;
private string auditDate;
private string sqlLogin;
private string application;
private string dbConnectionKey = Alliance.Infrastructure.Common.DatabaseConnectionSetting.BACKFLOW_SCOPE_KEY;
#endregion
public void Load_Audit()
{
string strSQLconnection = (dbSetting.SqlConnectionString + "; User Id = " + dbSetting.SqlUserName + "; Password = " + dbSetting.SqlPassword + ";");
SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand("SELECT [EntryID], [AuditValue].value('(row/@ManufacturerID)[1]', 'int') as ManufacturerID, [AuditValue] .value('(row/@ManufacturerName)[1]', 'nvarchar(50)') as ManufacturerName, [AuditDate], [SqlLogin], [Application] from [Backflow].[dbo].[AuditLog] where (TableName = @tableName AND [EntryID] = @primarykey)", sqlConnection);
sqlCommand.Parameters.AddWithValue("@tablename", tableName);
sqlCommand.Parameters.AddWithValue("@primarykey", primaryKeyID);
SqlDataReader reader = sqlCommand.ExecuteReader();
List<String> dataList = new List<String>();
while (reader.Read())
for (int i = 0; i < reader.FieldCount; i++)
{
string rdr = reader[i].ToString();
dataList.Add(rdr);
}
}
XAML(HistoryAuditLogView.xaml):
<telerik:RadGridView Name="AuditGrid" ItemsSource="{Binding dataList}">
</telerik:RadGridView>
一定要將'View'的'DataContext'屬性設置爲'ViewModel'。 offtopic:不要忘記關閉'sqlConnection' – Nogard 2013-02-27 14:56:01
你'while'loop似乎很奇怪。你想把桌子弄平嗎? – 2013-02-27 14:56:36
這可能很奇怪哈哈,但是,不。基本上我不挑剔如何做到這一點。我只需要使用MVVM並將來自sql的數據放入數據網格中。 @Nogard感謝您提醒我關閉連接,並設置了DataContext屬性。 – JLott 2013-02-27 15:00:04