我目前正在使用Telerik的RadGridView
来显示数据库中的数据。我想要的数据加载到gridview中,我还添加了三个额外的列,供用户输入其他信息。
我遇到的问题是,当您在其中一个空单元格中输入信息并单击行/列时,我输入的信息就会消失。我已经搜索了与此相关的每个论坛,我认为我使用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类:
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; }
}
答案 0 :(得分:1)
您应该将每列的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);
}
您还需要确保T
IEnumerable<T>
的{{1}}类型ItemsSource
实际上包含RadGridView
DeductId
,CheckNo
和CheckDate
属性,并且每个属性都有一个公共设置器。
绑定到匿名类型的对象集合不会起作用。您需要创建一个包含所有属性的类(Customer_Name,Customer_Id,Job_Id,Claim_Id,DeductId,CheckNo和CheckDate),并为那些您希望能够在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);