【问题标题】:Gridview on row data bound行数据绑定上的 Gridview
【发布时间】:2014-02-02 20:52:46
【问题描述】:

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

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,如果还没有传递,检查是否足够或不足并显示消息。

有什么指南吗?提前致谢。

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    您的 SQL 查询

    "SELECT delivered FROM dbo.Distributions "
    

    没有 where 条件,所以无论distribuitonID 的值是什么,checkIsDelivered 方法总是返回相同的值。尝试使用 distribuitonID 变量添加 where 条件。

    【讨论】:

    • 哦,对不起,我的粗心错误。刚刚也发现了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    相关资源
    最近更新 更多