Search code examples
c#asp.netdatagridlabelitemtemplate

Check the values of a ItemTamplate Label in Datagrid


I have a data Grid and there are ItemTamplate and in there are 2 Labels and they both contains some data in it.

I want to be able to loop through the whole GridView and see if any label contains the work Keyboarding and if it does change the value of the second label from Incomplete to To fails or Pass.

Here is the Code.

<asp:datagrid id="dgData" runat="server" Width="658px" CellPadding="2" PageSize="2" DataKeyField="ID"
            AutoGenerateColumns="False" ShowFooter="True" BorderColor="AliceBlue" OnItemDataBound="dgData_ItemDataBound">
  <SelectedItemStyle ForeColor="HighlightText" BackColor="Highlight"></SelectedItemStyle>
  <AlternatingItemStyle BackColor="WhiteSmoke"></AlternatingItemStyle>
  <HeaderStyle Font-Bold="True" BackColor="AliceBlue"></HeaderStyle>
  <FooterStyle Font-Bold="True" BackColor="AliceBlue"></FooterStyle>
  <Columns>
    <asp:BoundColumn DataField="ID" Visible="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="Name" HeaderText="" ItemStyle-VerticalAlign="Top"></asp:BoundColumn>
    <asp:TemplateColumn HeaderText="Term 1" ItemStyle-Wrap="True">
      <ItemTemplate>
        <asp:label BorderStyle=None Visible='<%# ReverseBool(Convert.ToBoolean(DataBinder.Eval(Container, "DataItem.IsCompleteOrNot"))) %>'   runat="server" ID="edit_Score" Text='<%# DataBinder.Eval(Container, "DataItem.Score" ) %>'>
        </asp:label>
        <asp:label BorderStyle=None Text='<%# GetCompleteIncomplete(Convert.ToInt32(DataBinder.Eval(Container, "DataItem.Score"))) %>' Visible='<%# DataBinder.Eval(Container, "DataItem.IsCompleteOrNot") %>' id="txtIsComplete" runat="server">
        </asp:label>
      </ItemTemplate>
    </asp:TemplateColumn>

  </Columns>
</asp:datagrid>

Now the c# the Call to the server GetCompleteIncomplete is a method in the c# that looks at it if its 1 its Complete if its 0 its in complete:

protected string GetCompleteIncomplete(int iScore)
{
    if (iScore == 0)
    {
      return "Incomplete";
    }

    return "Complete";
}

This return the Value to the Label and shows like this. This is how it looks like

But I want to change that InComplete/Compete to Fail/Pass only if the First Label is Keyboarding other lines could stay InComplete or Complete.


Solution

  • Try below code in your itemdatabound event:

    if ((e.Item.ItemType == ListItemType.Item) ||
                       (e.Item.ItemType == ListItemType.AlternatingItem))
    {
        Label edit_Score = (Label)e.Item.FindControl("edit_Score");
        Label txtIsComplete = (Label)e.Item.FindControl("txtIsComplete");
    
        if (edit_Score.Text == "Keyboarding")
        {
            if (txtIsComplete.Text == "Complete")
            {
                txtIsComplete.Text = "Pass";
            }
            else if(txtIsComplete.Text == "InComplete")
            {
                txtIsComplete.Text = "Fail";
            }
        }
    
    }