0

I try to color row if he have a certain value so when a index of my row is different of zero, I get an error

<asp:GridView ID="Gridview1" class="mx-auto" runat="server" AutoGenerateColumns="False" OnRowDataBound="Gridview1_RowDataBound"
     KeyNames="Id_Task"  AllowPaging="True" PageSize="6" DataSourceID="SqlDataSource1" CellPadding="6" ForeColor="#333333" GridLines="Vertical">
    <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="Id_Task" HeaderText="Id_Task" SortExpression="Id_Task" Visible="false" />
            <asp:BoundField DataField="Taches a faire" HeaderText="Taches a faire" SortExpression="Taches a faire" />
            <asp:BoundField DataField="Username" HeaderText="Username" SortExpression="Username" />
            <asp:BoundField DataField="date de requete" HeaderText="date de requete" SortExpression="date de requete" />
            <asp:BoundField DataField="deadline" HeaderText="deadline" SortExpression="deadline" />
            <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
            <asp:HyperLinkField Text="Details" DataNavigateUrlFields="Id_Task,Taches a faire,Username,date de requete" DataNavigateUrlFormatString="~/Admin/NewPage.aspx?Id_Task={0}&Taches a faire={1}&Username={2}&date de requete={3}" />


        </Columns>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <SortedAscendingCellStyle BackColor="#FDF5AC" />
        <SortedAscendingHeaderStyle BackColor="#4D0000" />
        <SortedDescendingCellStyle BackColor="#FCF6C0" />
        <SortedDescendingHeaderStyle BackColor="#820000" />
    </asp:GridView>

C# code:

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.Cells[0].Text.Equals("Finished"))
    {
        e.Row.BackColor = System.Drawing.Color.DarkGreen;
        e.Row.ForeColor = System.Drawing.Color.White;
    }
    else if (e.Row.Cells[0].Text.Equals("In progress"))
    {
        e.Row.BackColor = System.Drawing.Color.DarkOrange;
        e.Row.ForeColor = System.Drawing.Color.White;
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • What section is not working? What is the error message? Please be more specific in order to help more efficiently. – Péter Szilvási Nov 19 '22 at 08:43
  • The specified argument was not within the range of valid values. Parameter name: index . i receive this message when index is not equal zero – David Kuepo Nov 19 '22 at 08:45

1 Answers1

1

This is the correct syntax:

if (e.Row.RowType == DataControlRowType.DataRow)
{
  if (e.Row.Cells[5].Text.Equals("Finished"))
  {
    e.Row.BackColor = System.Drawing.Color.DarkGreen;
    e.Row.ForeColor = System.Drawing.Color.White;
    e.Row.Visible = false;
  }
  else if (e.Row.Cells[5].Text.Equals("In progress"))
  {
    e.Row.BackColor = System.Drawing.Color.DarkOrange;
    e.Row.ForeColor = System.Drawing.Color.White;
  } 
}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • 1
    It is not advisable to use magic numbers as indexes. If you change the order of the grid, you will need to adapt the backend code again. Checkout this answer to improve your answer: https://stackoverflow.com/questions/9715983/how-to-get-the-cell-value-by-column-name-not-by-index-in-gridview-in-asp-net – Péter Szilvási Nov 19 '22 at 09:55