0

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

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

网格未显示。

用于网格的 aspx:

<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 设置为与我为绑定网格而编写的查询中的列名相同。

这有什么错误吗?

请帮我。

4

1 回答 1

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" />
于 2013-07-16T08:30:06.227 回答