C#DataGridView与DataSet显示问题

时间:2011-06-06 18:37:57

标签: c# datagridview dataset

我有以下代码:

private void Timer1Tick(object sender, EventArgs e)
{
  timer_ScanTimer.Enabled = false;

  var psc = new ParseScannedCheckNumbers();
  if (psc.ParseCheck(_checkData))
  {
    label_Status.Text = @"Scan Next Check";

    var ct = checkTrans.IndividualCheck.NewIndividualCheckRow();
    ct.Date = DateTime.Now.ToShortDateString();
    var bracct = GetBranchAccountNumber(psc.BankAccountNumber);
    if (bracct.Trim().Length == 7)
    {
      ct.Branch = bracct.Substring(0, 2);
      ct.AccountNumber = bracct.Substring(2, 5);
      ct.NameOnCheck = GetAccountName(ct.Branch + ct.AccountNumber);
      ct.AccountBalance = GetAccountBalance(ct.Branch + ct.AccountNumber);
    }
    else
    {
      ct.Branch = Configuration.Branch;
      ct.AccountNumber = string.Empty;
      ct.NameOnCheck = string.Empty;
      ct.AccountBalance = 0;
    }

    ct.CheckAmount = 0;
    ct.BankRoutingNumber = psc.BankRoutingNumber;
    ct.BankAccountNumber = psc.BankAccountNumber;
    ct.CheckNumber = psc.CheckNumber;
    ct.Status = "Entered";
    checkTrans.IndividualCheck.Rows.Add(ct);
  }
  else
  {
    label_Status.Text = Resources.ScanCheck_ScanFailed;
  }
  _checkData = string.Empty;

  var rs = new RegistrySettings();
  if (!rs.ScanChecksContinuous)
  {
    StopScanning();
    label_Status.Text = Resources.ScanCheck_Success;
    EditLastRowEntered();
  }

  label_ChecksScanned.Text = (dgv_Checks.RowCount - 1).ToString();
}

当计时器熄灭时,我确认已收到所有数据,然后将其添加到数据集中。它被添加到数据集中没有问题,它只是每次都在datagridview上被看到。有时候它有效,大部分时间都没有。

如何在对数据集进行更改时更新datagridview?我在上面的代码中做错了吗?

谢谢! (再次)

1 个答案:

答案 0 :(得分:0)

如果您创建了数据集并使用Visual Studio数据源配置向导将其附加到DataGridView,那么您可能需要调用

this.somethingTableAdapter.Fill(this.yourDataSet.someDataTable);

代码中的某处。这实际上是将DataSet中的数据加载到您的DataGridView中。虽然再次调用此方法可能不是刷新DGV的“正确”方法,但它为我完成了这项工作。