网格没有在VB.NET中绑定

时间:2013-07-16 05:58:25

标签: asp.net .net vb.net visual-studio-2008 webforms

我正在使用vb.net并尝试在其中进行编辑/删除操作。

为此我编写了绑定网格的代码,但是当我运行页面时,

未显示网格。

aspx for grid:

<asp:GridView ID="gv" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None" AutoGenerateColumns="False">
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
                <asp:BoundField DataField="ExpenceDate" HeaderText="Expence Date" />
                <asp:BoundField DataField="sum(Amount)" HeaderText="Amount" />
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>

绑定网格的代码:

 Try
            da = New SqlDataAdapter("select expDate,sum(Amount) from expence_VB where expDate between '" + DateTime.Parse(txtFromDate.Text) + "' and '" + DateTime.Parse(txtToDate.Text) + "' group by ExpDate", con)
            ds = New DataSet()
            da.Fill(ds)
            gv.DataSource = ds.Tables(0)
            gv.DataBind()
        Catch ex As Exception

        End Try

注意:

我已完成编辑列并关闭了自动生成字段。

对于绑定字段,我已将DataField与查询中的列名相同,我已编写该绑定网格。

这有什么错误吗?

请帮帮我。

1 个答案:

答案 0 :(得分:1)

您的查询错误选择,查询总和(金额)不给列名。 试试这个: 从

更改您的选择查询
da = New SqlDataAdapter("select expDate,sum(Amount) from expence_VB where expDate between '" + DateTime.Parse(txtFromDate.Text) + "' and '" + DateTime.Parse(txtToDate.Text) + "' group by ExpDate", con)

成为

da = New SqlDataAdapter("select expDate,sum(Amount) as SumAmount from expence_VB where expDate between '" + DateTime.Parse(txtFromDate.Text) + "' and '" + DateTime.Parse(txtToDate.Text) + "' group by ExpDate", con)

然后在aspx网格中,您可以更改此代码:

<asp:BoundField DataField="sum(Amount)" HeaderText="Amount" />

成为:

<asp:BoundField DataField="SumAmount" HeaderText="Amount" />