【问题标题】:Handling empty cells in DataGridView处理 DataGridView 中的空单元格
【发布时间】:2012-09-28 22:28:24
【问题描述】:

如何处理 DataGridView 中的空单元格值,其中我有 3 列(帐户标题、借方、贷方),用户选择帐户的第一列,然后输入借方或贷方列,从而留下一个空单元格在每一行。我有一个条目涉及 3 个表。

create table Debit (debit_id, amount, **acct_id, entry_id);
create table Credit (credit_id, amount, **acct_id, entry_id);
create table Entry (entry_id, date);

基本上我希望它看起来像this。到目前为止,我所做的是加入了这 3 个表

SELECT  Entry.entry_id, Entry.Date, Debit.acct_id, Debit.amount, 
Credit.acct_id, Credit.amount FROM Entry FULL OUTER JOIN Debit
ON Entry.entry_id = Debit.entry_id FULL OUTER JOIN Credit
ON Entry.entry_id = Credit.entry_id

DataGridView 中有 6 列。我还没有想到一个查询只显示借方和贷方账户在一列中的 3 列以及相应列中的金额,以及它如何处理借方和贷方列上的空单元格我所做的工作有效,但在可用性方面并不好。

你能建议我这样做的方法吗?

** 账户表

【问题讨论】:

  • 我忘了在回答中提到这一点,但根据我的经验,DataGridViews 对于绑定到他们从变量中获得的对象类型有点挑剔。我假设您正在使用某种 SqlReader 或 DataTable 来获取此 SQL 查询的结果。问题是他们会返回Decimal 的类型(希望是钱)。而数据库实际上将其存储为Nullable(of Decimal)。如果您将网格绑定到符合您的意图的数据类型,您将看到更好的结果。

标签: c# .net sql-server-2008 accounting


【解决方案1】:

我认为建立工会可以解决您的问题。以下查询会将两组结果合并为一个结果集:

select e.entry_id, e.Date, d.acct_id, d.amount from Entry as e join Debit as d on e.entry_id=d.entry_id

联合所有

select e.entry_id, e.Date, c.acct_id, c.amount from Entry as e join Credit as c on e.entry_id=c.entry_id

【讨论】:

    【解决方案2】:

    我推断 (a) 您正在使用数据绑定,并且 (b) 这是供用户输入数据的。 如果它 是 用于输入,那么我将放弃数据绑定并在代码中从网格中填充/读取。

    【讨论】:

      【解决方案3】:

      如果您使用的是DataGridView,我建议您使用更加面向对象的方法,而不是直接传递 SQL 查询的结果。无论如何,您如何使用这种方法将更新推回?

      这是一个例子。我知道它在 VB.Net 中,但它应该仍然有意义。

      Public Class Form1
      
          Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
              Dim entries As List(Of AccountEntry) = RetrieveEntries()
      
              DataGridView1.DataSource = entries
      
          End Sub
      
          Public Class AccountEntry
              Public Property Id As Integer
              Public Property Debit As Nullable(Of Decimal)
              Public Property Credit As Nullable(Of Decimal)
              Public Property EntryDate As DateTime
          End Class
      
          Public Function RetrieveEntries() As List(Of AccountEntry)
              Dim returnEntries As List(Of AccountEntry) = Nothing
              ' Do your database stuff here to retrieve a list of AccountEntry'
              Return returnEntries
          End Function
      
      End Class
      

      【讨论】:

        猜你喜欢
        • 2018-02-18
        • 1970-01-01
        • 1970-01-01
        • 2011-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多