Gridview上的行数据绑定

时间:2014-02-02 12:30:06

标签: c# asp.net gridview

我在行数据绑定的网格视图上遇到了一些问题。我想要做的是首先检查某些分发是否已经交付,如果尚未交付,那么我检查它是否足够或不足。这是代码:

protected void gvDistribution_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            distributionID = gvDistribution.DataKeys[e.Row.RowIndex].Values[0].ToString();

            string isDelivered = packBLL.checkIsDelivered(distributionID);

            if (isDelivered == "Y")
            {
                Label lblStatus = (Label)e.Row.FindControl("lblStatus");
                lblStatus.Text = "Delivered";
            }
            else
            {
                //Check sufficient or insufficient which worked well already
            }
        }
    }

这是数据访问层:

public string checkIsDelivered(string distribuitonID)
    {
        string result = "";
        using (var connection = new SqlConnection(FoodBankDB.connectionString))
        {
            SqlCommand command = new SqlCommand("SELECT delivered FROM dbo.Distributions ", connection);
            connection.Open();
            using (var dr = command.ExecuteReader())
            {
                if (dr.Read())
                {
                    result = dr["delivered"].ToString();
                }
            }
        }
        return result;
    }

这就是我设置网格视图的方式:

<asp:GridView ID="gvDistribution" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Width="1000px" DataKeyNames="id" AllowPaging="True" OnPageIndexChanging="gvDistribution_PageIndexChanging" PageSize="5" OnRowCommand="gvDistribution_OnRowCommand" OnRowDataBound="gvDistribution_RowDataBound">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <Columns>
                        <asp:BoundField DataField="name" HeaderText="Beneficiary Name"></asp:BoundField>
                        <asp:BoundField DataField="packingDate" HeaderText="Packing Date" DataFormatString="{0:dd/M/yyyy}"></asp:BoundField>
                        <asp:BoundField DataField="deliveryDate" HeaderText="Delivery Date" DataFormatString="{0:dd/M/yyyy}" />
                        <asp:TemplateField HeaderText="Status" ItemStyle-Width="120px">
                            <ItemTemplate>
                                <asp:Label ID="lblStatus" runat="server"></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:ButtonField CommandName="View" Text="View Details" />
                    </Columns>
                </asp:GridView>

然而,它只是让我回复状态栏中的所有内容。它应该首先检查isDelivered,然后如果还没有交付,检查是否足够或不足并显示消息。

任何指南?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的SQL查询

"SELECT delivered FROM dbo.Distributions "

没有where条件,因此无论checkIsDelivered的值是什么,distribuitonID方法都将始终返回相同的值。尝试使用distribuitonID变量添加where条件。