无法在 RadGridView Telerik WPF 中编辑单元格
Cannot Edit Cells in RadGridView Telerik WPF
我目前正在使用 Telerik 的 RadGridView
来显示数据库中的数据。我想要的数据确实加载到 gridview 中,我还添加了三个额外的列,供用户输入其他信息。
我遇到的问题是,当您在其中一个空白单元格中输入信息并单击 row/column 之外时,我输入的信息就会消失。我搜索了与此相关的每个论坛,我认为我使用 gridView.Items.CommitEdit
的代码是正确的,但是我输入到空单元格中的信息仍然消失了。这是创建额外列的代码:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Loads queries from each of the designated data tables in BSI_Test
var customerQuery =
(from customer in testEntity.Customers
join job in testEntity.Jobs
on customer.CID equals job.CID
join claim in testEntity.Claims
on job.JID equals claim.JID
select new
{
Customer_Name = customer.CName,
Customer_Id = customer.CID,
Job_Id = job.JID,
Claim_Id = claim.CLAIMID,
DID = DeductId,
Check_No = CheckNo,
Check_Date = CheckDate
})
.OrderBy(c => c.Customer_Name);
//Populates the Telerik data grid with data.
gridView.ItemsSource = customerQuery.ToList();
GridViewDataColumn deductId = new GridViewDataColumn();
deductId.UniqueName = "DeductId";
deductId.Header = "DID";
deductId.DataMemberBinding = new Binding("DeductId");
gridView.Columns.Add(deductId);
GridViewDataColumn checkNo = new GridViewDataColumn();
checkNo.UniqueName = "CheckNo";
checkNo.Header = "Check No";
checkNo.DataMemberBinding = new Binding("CheckNo");
gridView.Columns.Add(checkNo);
GridViewDataColumn checkDate = new GridViewDataColumn();
checkDate.UniqueName = "CheckDate";
checkDate.Header = "Check Date";
checkDate.DataMemberBinding = new Binding("CheckDate");
gridView.Columns.Add(checkDate);
}
这是我的 gridView_CellEditEnded
事件,它尝试提交对列所做的任何编辑。注意:我确实使用断点测试了这个事件,当我在单元格中键入信息并单击它时,它确实通过了 if 语句。但是,我输入的数据还是消失了,所以CommitEdit
似乎没有正常工作。
bool handle = true;
private void gridView_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
{
if (e.EditAction == GridViewEditAction.Commit && handle)
{
handle = false;
gridView.Items.EditItem(this.gridView.CurrentItem);
gridView.Items.CommitEdit();
handle = true;
}
}
如果有人能帮助我解决这个问题,将不胜感激。老实说,我对我的代码有什么问题感到很困惑。
编辑:
我已经更新了 Window_Loaded
事件的代码,以显示将信息从数据库查询到数据网格的 linq 查询。 ItemsSource
然后作为列表设置到此查询。下面是我的 MainWindow()
方法之前的所有内容,我在其中设置 DeductId、CheckNo 和 CheckDate 的属性。
public string DeductId { get; set; }
public int CheckNo { get; set; }
public string CheckDate { get; set; }
public MainWindow()
{
InitializeComponent();
}
编辑:
现在,我已将 DeductId、CheckNo 和 CheckDate 属性添加到 linq 查询中。
编辑:
这是 DataProperties class:
public partial class DataProperties
{
public string CName { get; set; }
public int CID { get; set; }
public int JID { get; set; }
public int CLAIMID { get; set; }
public string DeductId { get; set; }
public string CheckNo { get; set; }
public string CheckDate { get; set; }
}
您应该将每列的 DataMemberBinding
属性 设置为一个 Binding
对象:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
GridViewDataColumn deductId = new GridViewDataColumn();
deductId.UniqueName = "DeductId";
deductId.Header = "DID";
deductId.DataMemberBinding = new System.Windows.Data.Binding("DeductId");
gridView.Columns.Add(deductId);
GridViewDataColumn checkNo = new GridViewDataColumn();
checkNo.UniqueName = "CheckNo";
checkNo.Header = "Check No";
deductId.DataMemberBinding = new System.Windows.Data.Binding("CheckNo");
gridView.Columns.Add(checkNo);
GridViewDataColumn checkDate = new GridViewDataColumn();
checkDate.UniqueName = "CheckDate";
checkDate.Header = "Check Date";
deductId.DataMemberBinding = new System.Windows.Data.Binding("CheckDate");
gridView.Columns.Add(checkDate);
}
您还需要确保您为 RadGridView
设置为 ItemsSource
的 IEnumerable<T>
的类型 T
实际上包含 DeductId
、CheckNo
和 CheckDate
属性,并且每个属性都有一个 public setter.
绑定到匿名类型的对象集合将不起作用。您需要创建一个包含所有属性(Customer_Name、Customer_Id、Job_Id、Claim_Id、DeductId、CheckNo 和 CheckDate 的 class 并具有 public setters 用于您希望能够在 RadGridView
中编辑的内容:
var customerQuery =
(from customer in testEntity.Customers
join job in testEntity.Jobs
on customer.CID equals job.CID
join claim in testEntity.Claims
on job.JID equals claim.JID
select new YourClass //<--
{
Customer_Name = customer.CName,
Customer_Id = customer.CID,
Job_Id = job.JID,
Claim_Id = claim.CLAIMID,
DID = DeductId,
Check_No = CheckNo,
Check_Date = CheckDate
})
.OrderBy(c => c.Customer_Name);
我目前正在使用 Telerik 的 RadGridView
来显示数据库中的数据。我想要的数据确实加载到 gridview 中,我还添加了三个额外的列,供用户输入其他信息。
我遇到的问题是,当您在其中一个空白单元格中输入信息并单击 row/column 之外时,我输入的信息就会消失。我搜索了与此相关的每个论坛,我认为我使用 gridView.Items.CommitEdit
的代码是正确的,但是我输入到空单元格中的信息仍然消失了。这是创建额外列的代码:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Loads queries from each of the designated data tables in BSI_Test
var customerQuery =
(from customer in testEntity.Customers
join job in testEntity.Jobs
on customer.CID equals job.CID
join claim in testEntity.Claims
on job.JID equals claim.JID
select new
{
Customer_Name = customer.CName,
Customer_Id = customer.CID,
Job_Id = job.JID,
Claim_Id = claim.CLAIMID,
DID = DeductId,
Check_No = CheckNo,
Check_Date = CheckDate
})
.OrderBy(c => c.Customer_Name);
//Populates the Telerik data grid with data.
gridView.ItemsSource = customerQuery.ToList();
GridViewDataColumn deductId = new GridViewDataColumn();
deductId.UniqueName = "DeductId";
deductId.Header = "DID";
deductId.DataMemberBinding = new Binding("DeductId");
gridView.Columns.Add(deductId);
GridViewDataColumn checkNo = new GridViewDataColumn();
checkNo.UniqueName = "CheckNo";
checkNo.Header = "Check No";
checkNo.DataMemberBinding = new Binding("CheckNo");
gridView.Columns.Add(checkNo);
GridViewDataColumn checkDate = new GridViewDataColumn();
checkDate.UniqueName = "CheckDate";
checkDate.Header = "Check Date";
checkDate.DataMemberBinding = new Binding("CheckDate");
gridView.Columns.Add(checkDate);
}
这是我的 gridView_CellEditEnded
事件,它尝试提交对列所做的任何编辑。注意:我确实使用断点测试了这个事件,当我在单元格中键入信息并单击它时,它确实通过了 if 语句。但是,我输入的数据还是消失了,所以CommitEdit
似乎没有正常工作。
bool handle = true;
private void gridView_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
{
if (e.EditAction == GridViewEditAction.Commit && handle)
{
handle = false;
gridView.Items.EditItem(this.gridView.CurrentItem);
gridView.Items.CommitEdit();
handle = true;
}
}
如果有人能帮助我解决这个问题,将不胜感激。老实说,我对我的代码有什么问题感到很困惑。
编辑:
我已经更新了 Window_Loaded
事件的代码,以显示将信息从数据库查询到数据网格的 linq 查询。 ItemsSource
然后作为列表设置到此查询。下面是我的 MainWindow()
方法之前的所有内容,我在其中设置 DeductId、CheckNo 和 CheckDate 的属性。
public string DeductId { get; set; }
public int CheckNo { get; set; }
public string CheckDate { get; set; }
public MainWindow()
{
InitializeComponent();
}
编辑: 现在,我已将 DeductId、CheckNo 和 CheckDate 属性添加到 linq 查询中。
编辑: 这是 DataProperties class:
public partial class DataProperties
{
public string CName { get; set; }
public int CID { get; set; }
public int JID { get; set; }
public int CLAIMID { get; set; }
public string DeductId { get; set; }
public string CheckNo { get; set; }
public string CheckDate { get; set; }
}
您应该将每列的 DataMemberBinding
属性 设置为一个 Binding
对象:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
GridViewDataColumn deductId = new GridViewDataColumn();
deductId.UniqueName = "DeductId";
deductId.Header = "DID";
deductId.DataMemberBinding = new System.Windows.Data.Binding("DeductId");
gridView.Columns.Add(deductId);
GridViewDataColumn checkNo = new GridViewDataColumn();
checkNo.UniqueName = "CheckNo";
checkNo.Header = "Check No";
deductId.DataMemberBinding = new System.Windows.Data.Binding("CheckNo");
gridView.Columns.Add(checkNo);
GridViewDataColumn checkDate = new GridViewDataColumn();
checkDate.UniqueName = "CheckDate";
checkDate.Header = "Check Date";
deductId.DataMemberBinding = new System.Windows.Data.Binding("CheckDate");
gridView.Columns.Add(checkDate);
}
您还需要确保您为 RadGridView
设置为 ItemsSource
的 IEnumerable<T>
的类型 T
实际上包含 DeductId
、CheckNo
和 CheckDate
属性,并且每个属性都有一个 public setter.
绑定到匿名类型的对象集合将不起作用。您需要创建一个包含所有属性(Customer_Name、Customer_Id、Job_Id、Claim_Id、DeductId、CheckNo 和 CheckDate 的 class 并具有 public setters 用于您希望能够在 RadGridView
中编辑的内容:
var customerQuery =
(from customer in testEntity.Customers
join job in testEntity.Jobs
on customer.CID equals job.CID
join claim in testEntity.Claims
on job.JID equals claim.JID
select new YourClass //<--
{
Customer_Name = customer.CName,
Customer_Id = customer.CID,
Job_Id = job.JID,
Claim_Id = claim.CLAIMID,
DID = DeductId,
Check_No = CheckNo,
Check_Date = CheckDate
})
.OrderBy(c => c.Customer_Name);