【问题标题】:Remove or Refresh DataTable删除或刷新数据表
【发布时间】:2011-07-20 20:15:46
【问题描述】:

我编写的应用程序遇到了问题。基本上它是一个由每月每一天的面板/列表框组成的月历。我使用以下内容填充我的列表框:

private void PopulateEventListBoxes(int offsetNumber, int monthDays)
{
    string locationSelected = this.LocationComboBox.Text.ToLower();

    for (int i = 0; i <= (monthDays - 1); i++)
    {
        DateTime dateSelected = Convert.ToDateTime(m_ArrayDateLabel[offsetNumber + i].Text).Date;

        var tempCalendar = new Data.DataSet.vwGet_CalendarDetailsDataTable();

        switch (locationSelected)
        {
            case "all":
                Data.Manager.CalendarDetailsTableAdapter.FillByDate(tempCalendar, dateSelected);
                break;
            default:
                Data.Manager.CalendarDetailsTableAdapter.FillByDateCity(tempCalendar, dateSelected, locationSelected);
                break;
        }

        foreach (Data.DataSet.vwGet_CalendarDetailsRow row in tempCalendar)
        {
            ((ListBox)m_ArrayEventListBox[offsetNumber + i]).Items.Add(row.Name + " - " + row.StatusCode);
        }
    }
}

我将var tempCalendar = new Data.DataSet.vwGet_CalendarDetailsDataTable(); 用于DataTable,因为我需要将日事件加载到每个列表框中。此数据来自 SQL Server 的视图。

加载日历后,用户可以通过弹出对话框添加/编辑/删除项目,其中包含当天的日历详细信息。当对话框加载到添加/编辑时,我以这种方式填充它:

private void SearchForEventsByDate()
{
    switch (m_LocationSelected)
    {
        case "all":
            Data.Manager.CalendarDetailsTableAdapter.FillByDate(DataSetCalendar.vwGet_CalendarDetails, m_DateSelected);
            CalendarDetailsBindingSource.DataSource = DataSetCalendar.vwGet_CalendarDetails;
            break;
        default:
            Data.Manager.CalendarDetailsTableAdapter.FillByDateCity(DataSetCalendar.vwGet_CalendarDetails, m_DateSelected, m_LocationSelected);
            CalendarDetailsBindingSource.DataSource = DataSetCalendar.vwGet_CalendarDetails;
            break;
    }

    RefreshRepRecordCount();
}

这次加载数据时,我使用的是调用DataSetCalendar.vwGet_CalendarDetails,而不是数据表的临时内存版本。

如果我遵循以下程序,我的问题就会出现:

  1. 为员工添加新事件
  2. 删除我刚刚添加的同一事件
  3. 尝试在同一天为员工重新添加新事件 - 应用会抛出 GUID 已存在于表中的错误。

我的表的主键是 EventDate 和 EmployeeGuid。如果我在删除后查询 SQL 表,没有记录,但我的应用程序仍然认为该记录仍然存在。我知道记录在记忆中。

我尝试了以下方法将其从内存中删除,但它不起作用:

DataSetCalendar.vwGet_CalendarDetails.FindByEmployeeGUID(SelectedCalendarEvent.EmployeeGUID).Delete();
DataSetCalendar.vwGet_CalendarDetails.AcceptChanges();

我也尝试使用Remove 删除记录,但我的应用仍然认为记录存在。我不知道还能做什么或尝试。任何帮助都会很棒。

【问题讨论】:

    标签: c# winforms datatable


    【解决方案1】:

    您如何在客户端“存储”日历?知道这将引导你找到答案。听起来您在客户端有某种类型的缓存。

    如果在客户端缓存,在服务器端更改数据时需要刷新缓存。

    【讨论】:

    • 听起来不是很愚蠢,但我不是 100% 确定。我们正在使用由我部门中的某个人创建的 winforms 模板/框架。我对整个事情的运作方式仍然很陌生。但是我知道我必须在内存中刷新它但我一直无法成功。
    • 提问是不愚蠢的表现,因为愚蠢的人不会提问。不幸的是,至少在许多实现中,关于国产框架的抽象是没有经过深思熟虑的。如果cahcing方法被封装了,你将不得不深入框架并确定如何使缓存失效。
    【解决方案2】:

    我可以通过添加 Clear() 来解决此问题

    private void SearchForEventsByDate()
    {
        switch (m_LocationSelected)
        {
           case "all":
             Data.Manager.Data.SM_T_Employee_Status.Clear();
             DataSetCalendar.vwGet_CalendarDetails.Clear();
    
             Data.Manager.CalendarDetailsTableAdapter.FillByDate(DataSetCalendar.vwGet_CalendarDetails, m_DateSelected);
              CalendarDetailsBindingSource.DataSource = DataSetCalendar.vwGet_CalendarDetails;
              break;
           default:
              Data.Manager.Data.SM_T_Employee_Status.Clear();
              DataSetCalendar.vwGet_CalendarDetails.Clear();
              Data.Manager.CalendarDetailsTableAdapter.FillByDateCity(DataSetCalendar.vwGet_CalendarDetails, m_DateSelected, m_LocationSelected);
              CalendarDetailsBindingSource.DataSource = DataSetCalendar.vwGet_CalendarDetails;
              break;
         }
    
     RefreshRepRecordCount();
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 2017-06-28
      • 2012-08-03
      • 1970-01-01
      • 2020-05-07
      • 2017-05-22
      • 2015-04-08
      相关资源
      最近更新 更多