问题 Datagridview没有填充

尿el

新成员
已加入
2016年7月29日
留言内容
1
编程经验
Beginner
你好, 我有一个方法私有void FillLevyRoll(),我将该方法放在click事件中的按钮上以填充datagridview dgvLevyRoll我的问题是当我运行时我没有收到错误并且dgv保持为空。徒劳无功。该查询在SQL Management Studio中有效

C#:
C#:
[COLOR=#101094]private[/COLOR][COLOR=#101094]void[/COLOR][COLOR=#2B91AF]FillLevyRoll[/COLOR][COLOR=#303336]()[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#303336]{[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#101094]try[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#303336]{[/COLOR][COLOR=#303336]
            [/COLOR][COLOR=#2B91AF]DataTable[/COLOR][COLOR=#303336] dataTableSource [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#101094]new[/COLOR][COLOR=#2B91AF]DataTable[/COLOR][COLOR=#303336]();[/COLOR][COLOR=#303336]


            [/COLOR][COLOR=#2B91AF]SqlCommand[/COLOR][COLOR=#303336] command [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#101094]new[/COLOR][COLOR=#2B91AF]SqlCommand[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"SELECT  Refrence"[/COLOR][COLOR=#303336]+[/COLOR][COLOR=#303336]
                [/COLOR][COLOR=#7D2727]", MAX(CASE WHEN AccNumber = '1010000' THEN amount END) AS Opening"[/COLOR][COLOR=#303336]+[/COLOR][COLOR=#303336]
                [/COLOR][COLOR=#7D2727]", MAX(CASE WHEN AccNumber = '1010000' THEN amount END) AS electricity"[/COLOR][COLOR=#303336]+[/COLOR][COLOR=#303336]
                [/COLOR][COLOR=#7D2727]", MAX(CASE WHEN AccNumber = '1045000' THEN amount END) AS water"[/COLOR][COLOR=#303336]+[/COLOR][COLOR=#303336]
                [/COLOR][COLOR=#7D2727]", MAX(CASE WHEN AccNumber = '1000000' THEN amount END) AS levy"[/COLOR][COLOR=#303336]+[/COLOR][COLOR=#303336]
                [/COLOR][COLOR=#7D2727]", MAX(CASE WHEN AccNumber = '2750000' THEN amount END) AS Interest"[/COLOR][COLOR=#303336]+[/COLOR][COLOR=#303336]
                [/COLOR][COLOR=#7D2727]", MAX(CASE WHEN AccNumber = '4000000' THEN amount END) AS Legal"[/COLOR][COLOR=#303336]+[/COLOR][COLOR=#303336]
                [/COLOR][COLOR=#7D2727]", MAX(CASE WHEN AccNumber = '4350003' THEN amount END) AS Other"[/COLOR][COLOR=#303336]+[/COLOR][COLOR=#303336]
                [/COLOR][COLOR=#7D2727]", MAX(CASE WHEN AccNumber = '8400000' THEN amount END) AS Payments"[/COLOR][COLOR=#303336]+[/COLOR][COLOR=#303336]
                [/COLOR][COLOR=#7D2727]", MAX(CASE WHEN AccNumber = '1000000' THEN amount END) AS Due"[/COLOR][COLOR=#303336]+[/COLOR][COLOR=#303336]
                [/COLOR][COLOR=#7D2727]" FROM dbo.vw_LedgerTransactions"[/COLOR][COLOR=#303336]+[/COLOR][COLOR=#303336]
                [/COLOR][COLOR=#7D2727]" GROUP BY Refrence "[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] conn[/COLOR][COLOR=#303336]);[/COLOR][COLOR=#303336]

            [/COLOR][COLOR=#2B91AF]SqlDataAdapter[/COLOR][COLOR=#303336] adapter [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#101094]new[/COLOR][COLOR=#2B91AF]SqlDataAdapter[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]command[/COLOR][COLOR=#303336]);[/COLOR][COLOR=#303336]
            conn[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Open[/COLOR][COLOR=#303336]();[/COLOR][COLOR=#303336]

            adapter[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Fill[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]dataTableSource[/COLOR][COLOR=#303336]);[/COLOR][COLOR=#303336]
            dgvLevyRoll[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]DataSource[/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] dataTableSource[/COLOR][COLOR=#303336];[/COLOR][COLOR=#303336]

        [/COLOR][COLOR=#303336]}[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#101094]catch[/COLOR][COLOR=#303336]([/COLOR][COLOR=#2B91AF]Exception[/COLOR][COLOR=#303336] ex[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#303336]{[/COLOR][COLOR=#303336]
            [/COLOR][COLOR=#2B91AF]MessageBox[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Show[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Application Error. err:"[/COLOR][COLOR=#303336]+[/COLOR][COLOR=#303336] ex[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]ToString[/COLOR][COLOR=#303336]());[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#303336]}[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#101094]finally[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#303336]{[/COLOR][COLOR=#303336]
            conn[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Close[/COLOR][COLOR=#303336]();[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#303336]}[/COLOR][COLOR=#303336]

    [/COLOR][COLOR=#303336]}[/COLOR]

 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,525
地点
悉尼,澳大利亚
编程经验
10+
顺便说一下,打开和关闭连接毫无意义,因为Fill和Update都会为您做到这一点。如果要执行多个操作,则仅在使用数据适配器时才需要显式打开和关闭连接。
 
最佳 底部