将GridView绑定到列表

本文关键字:列表 绑定 GridView | 更新日期: 2023-09-27 18:01:19

我在弄清楚这一点时遇到了最糟糕的情况。我必须使用我还不完全理解的MVVM,并将一个装满SQL数据的列表绑定到radGridView。听起来很简单。。。但我想不通。数据没有显示,我认为这是因为我绑定错误。以下是我的相关代码。任何帮助都将不胜感激!

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>

将GridView绑定到列表

完整的公共属性可以帮助您,将其添加到ViewModel:的根级别

#region Private Fields
...
#region
#region Public Props
private List<String> _dataList;
public List<String> dataList 
{
get{ return _datalist;}
set{_datalist = value;}
}
#region

public void Load_Audit()
        {
dataList = new List<String>(); //then your code
//.... 
}
List<String> dataList = new List<String>();

它只是一个局部变量,只能在该方法中访问和有效。您必须将其移动到属性或字段:

public List<String> DataList { get; set; }

并且在方法中:

DataList= new List<String>();
while (reader.Read())
    for (int i = 0; i < reader.FieldCount; i++)
    {
        string rdr = reader[i].ToString();
        DataList.Add(rdr);
    }

MVVM绑定在ViewModel中使用公共属性。。。

此外,如果可能的话,最好绑定到Observable Collections。

为此,您需要使用以下代码;

private ObservableCollection<String> _DataList;
public ObservableCollection<String> DataList {
    get { return _DataList; }
    set {
        if (value.Equals(_Details) == false) {
                 _DataList= value;
                  OnPropertyChanged("DataList");
        }
    }
}

OnPropertyChanged Sub引发INotifiyPropertyChanged事件,因此对集合的任何更改都会自动反映在GridView中。

您需要创建此子,但是如果您愿意,可以直接在Setter中引发此事件。

您的代码将是;

_Details = new ObserservableCollection<string>();
while (reader.Read())
   for (int i = 0; i < reader.FieldCount; i++)
   {
       string rdr = reader[i].ToString();
       dataList.Add(rdr);
   }

您的XAML将与上面的相同。

初始化视图和viewModel 时尝试设置DataContext

MyView view = new MyView();
MyViewModel viewModelodel = new MyViewModel();
view.DataContext = viewModel;

另一个解决方案是在您的视图中设置DataContex。但是你必须告诉你的视图ViewModels的位置。

<Window x:Class="MyNamespace.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyNamespace"
    Title="MainWindow" Height="Auto" Width="Auto" >
<Window.DataContext>
    <local:MyViewModel/>
</Window.DataContext>
</Window>

当然,您必须在ViewModel中添加一个表示列表的公共属性。