带有“Convert()”的DataTable DefaultView过滤字符串

时间:2013-05-28 06:37:14

标签: c# winforms datagridview

您好我使用DataTableDevaultView.RowFilter来展示DataGridView

我有一个日期(dd/mm/yyyy)列。

String filter = String.Format(@"account_name LIKE '{0}*' AND type_name='{2}' AND Convert( transaction_date, System.DateTime ) < '{1}' OR is_edited ='true' ", this.accountName, this.dateTimePickerTransactionFrom.Value.ToString(), this.radioButtonDebit.Checked ? "Debit" : "Credit");
transactionsAll.DefaultView.RowFilter = filter;
DefaultView_ListChanged();

最终出现错误:

  

字符串未被识别为有效的DateTime。

1 个答案:

答案 0 :(得分:0)

谢谢伙计们,
我解决了这个问题 我从

更改了数据库查询
SELECT     transactions.transaction_id, transactions.transaction_amound, CONVERT(char(10), transactions.transaction_date, 103) AS transaction_date, 
                  transaction_type.type_name, transaction_type.description, accounts.account_name, accounts.user_id
FROM         transactions INNER JOIN
                  transaction_type ON transactions.type_id = transaction_type.type_id INNER JOIN
                  accounts ON transactions.account_id = accounts.account_id
WHERE     (accounts.user_id = @UserId)


SELECT     transactions.transaction_id, transactions.transaction_amound, transactions.transaction_date, transaction_type.type_name, 
                  transaction_type.description, accounts.account_name, accounts.user_id
FROM         transactions INNER JOIN
                  transaction_type ON transactions.type_id = transaction_type.type_id INNER JOIN
                  accounts ON transactions.account_id = accounts.account_id
WHERE     (accounts.user_id = @UserId)


    private void ConstraintChanged()
    {
        String filter = String.Format(@"account_name LIKE '{0}*' AND type_name='{2}' " +
                @"AND Convert( transaction_date, System.DateTime ) > '{1}' OR is_edited ='true' ",
            this.accountName,
            this.dateTimePickerTransactionFrom.Value,
            this.radioButtonDebit.Checked ? "Debit" : "Credit");
        transactionsAll.DefaultView.RowFilter = filter;
        DefaultView_ListChanged();
    }