LINQ查询未检索已保存的数据

时间:2011-11-28 13:49:23

标签: c# .net linq datagridview

也许我需要重新开始。这是数据网格视图的有用链接,但我并不需要提起它。

问题的实质是:我将这种方法用于两件事: - 添加新记录时从数据库中获取必要的数据 - 在我更新记录后从数据库中获取必要的数据

public void refreshReservationDetail()
{
    _oReservationDetail = (from rd in _oReservation.tblReservationDetails
                           where rd.ReservationID == _oReservationObject.ID
                           select rd).ToList();
}

当我添加新记录时,新数据将被保存并加载到datagridview中。所以这很好。

当我更新现有记录(已经在datagridview中显示)时,更改的内容也正在正确保存,但上面的方法并没有给我保存的数据,但仍然是旧数据。怎么样?

这里我保存了我的数据

//frmReservationDetail
public event dRefresh SavedActivity;

private void btnOk_Click(object sender, EventArgs e)
{
    _oReservationDetail.ReservationDetailData.ReservationID = _oReservationDetail.ReservationObjectData.ID;
    _oReservationDetail.ReservationDetailData.StartTime = Convert.ToDateTime(_oReservationDetail.ReservationObjectData.StartTime.Value.ToString("dd/MM/yyyy") + " " + _oDateTime[cboStartTime.getSelectedID()]);
    _oReservationDetail.ReservationDetailData.EndTime = Convert.ToDateTime(_oReservationDetail.ReservationObjectData.StartTime.Value.ToString("dd/MM/yyyy") + " " + _oDateTime[cboEndTime.getSelectedID()]);
    if (this.cboLanguage.SelectedItem != null)
    {
        _oReservationDetail.ReservationDetailData.LanguageID = this.cboLanguage.getSelectedID();
    }
    else
        _oReservationDetail.ReservationDetailData.LanguageID = null;

    if (this.cboTargetgroup.SelectedItem != null)
    {
        _oReservationDetail.ReservationDetailData.TargetgroupID = this.cboTargetgroup.getSelectedID();
    }
    else
        _oReservationDetail.ReservationDetailData.TargetgroupID = null;

    if (this.cboExcursion.SelectedItem != null)
    {
        _oReservationDetail.ReservationDetailData.ExcursionID = this.cboExcursion.getSelectedID();
    }
    else
        _oReservationDetail.ReservationDetailData.ExcursionID = null;

    _oReservationDetail.ReservationDetailData.Participants = int.Parse(this.txtParticipants.Text);
    _oReservationDetail.ReservationDetailData.Leaders = int.Parse(this.txtLeaders.Text);
    _oReservationDetail.ReservationDetailData.Remarks = this.txtRemarks.Text;
    _oReservationDetail.save();

    if(SavedActivity != null)
        SavedActivity();

    this.Close();
    }

//clsReservationDetail
public void save()
{
    int i = _oReservationDetail.ID;
    if (_oReservationDetail.ID == 0)
    {
            _oReservation.tblReservationDetails.InsertOnSubmit(_oReservationDetail);
            _oReservation.SubmitChanges();
    }
    _oReservation.SubmitChanges();
}

2 个答案:

答案 0 :(得分:1)

听起来你没有重新绑定datagridview。保存/更新后,您应该再次调用refreshReservationDetail(),然后您需要再次将数据源设置为该变量并重新绑定它。如果您与更新面板或其他类似的异步执行此操作,则必须在为保存/更新而触发的事件结束时调用refreshReservationDetail()。如果页面正常回发,那么只要确保你的数据绑定发生在Gridview,无论它是否是回发。

答案 1 :(得分:0)

我通过调用这个循环成功了:

foreach (var oReservationDetail in
    _oReservationObject.ReservationData.tblReservationDetails)

我使用了数据库中的设置关系,而不是再次查询来获取数据。它现在有效!