2012-09-28 53 views
0

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

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列,其中借方和贷方帐户在一列中,并在适当的列上显示金额,如果它是如何处理借方和贷方列的空单元格我所做的工作,但可用性明智,它不好。

你可以建议我这样做的方法。

**账户表

+0

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

回答

0

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

从条目中选择e.entry_id,e.Date,d.acct_id,d.amount作为e连接借方为e.entry_id = d.entry_id

UNION ALL

选择e.entry_id,e.Date,c.acct_id,从入门c.amount为e加入信用为C上e.entry_id = c.entry_id

0

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

0

如果您使用的是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