title: Gridview on row data bound
body: <p>I am having some problem with grid view on row data bound. What I am trying to do is first I check if certain distribution is delivered or not, if not yet delivered, then I check if it is sufficient or insufficient. Here is the code:</p>
  
  <pre><code>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
              }
          }
      }
  </code></pre>
  
  <p>And this is the data access layer:</p>
  
  <pre><code>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;
      }
  </code></pre>
  
  <p>And this is how I set up my grid view:</p>
  
  <pre><code>&lt;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"&gt;
                      &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775" /&gt;
                      &lt;Columns&gt;
                          &lt;asp:BoundField DataField="name" HeaderText="Beneficiary Name"&gt;&lt;/asp:BoundField&gt;
                          &lt;asp:BoundField DataField="packingDate" HeaderText="Packing Date" DataFormatString="{0:dd/M/yyyy}"&gt;&lt;/asp:BoundField&gt;
                          &lt;asp:BoundField DataField="deliveryDate" HeaderText="Delivery Date" DataFormatString="{0:dd/M/yyyy}" /&gt;
                          &lt;asp:TemplateField HeaderText="Status" ItemStyle-Width="120px"&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:Label ID="lblStatus" runat="server"&gt;&lt;/asp:Label&gt;
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:ButtonField CommandName="View" Text="View Details" /&gt;
                      &lt;/Columns&gt;
                  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>However, it just keep returning me all delivered at the status column. It was supposed to check isDelivered first, then if not delivered yet, check if sufficient or insufficient and display the message.</p>
  
  <p>Any guides? Thanks in advance.</p>