locked
Highlight a record or field in gridview RRS feed

  • Question

  • User1028962535 posted

    Hello, 

    The previous version of my grid view contained a checkbox field called query, the user would tick this to indicate there was a query with this record, and the background color would change to highlight that fact. I recently had to change this field to a button, so the user can click to open a new form to capture details about the query. This is all working. What I need to do is change the background color of the query field when the record has a query associated with it, but I do not have a checkbox anymore, so my old code does not work

    If Query = True

    e.row.cells(1).backcolor = system.drawing.color.red

    Is there a way to do this?. The query data is stored in a different table. 

    Friday, September 16, 2016 10:19 AM

Answers

  • User283571144 posted

    Hi Dan5,

    What I need to do is change the background color of the query field when the record has a query associated with it, but I do not have a checkbox anymore, so my old code does not work

    According to your description, I suggest you could foreach the gridviwe's row value in the button click event.

    If the row's cell value is equals with click value, then highlight it.

    More details, you could refer to follow codes:

    Note: Since I don't know how you get the query string, I get current row value.

    I have two gridview in a page.

    If I click the button, will highlight the row in the second gridview.

    Aspx:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" >
                <Columns>
                    <asp:TemplateField HeaderText="ID" SortExpression="ID">
                        <EditItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="url" SortExpression="url">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("url") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                                  <asp:Label ID="Label2" runat="server" Text='<%# Bind("url") %>'></asp:Label>
                            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <br />
            <asp:GridView ID="GridView2" runat="server"  ></asp:GridView>

    Code-behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Databind1();
                    Databind2();
                }
            }
    
            private void Databind1()
            {
                DataTable d1 = new DataTable();
                d1.Columns.Add("ID");
                d1.Columns.Add("url");
                d1.Rows.Add("1", "http://www.google.com");
                d1.Rows.Add("2", "http://www.facebook.com");
                GridView1.DataSource = d1;
                GridView1.DataBind();
            }
    
            private void Databind2()
            {
                DataTable d1 = new DataTable();
                d1.Columns.Add("ID");
                d1.Columns.Add("url");
                d1.Rows.Add("1", "http://www.google.com");
                d1.Rows.Add("2", "http://www.facebook.com");
                GridView2.DataSource = d1;
                GridView2.DataBind();
            }
     protected void Button1_Click(object sender, EventArgs e)
            {
                string click = string.Empty;
                GridViewRow row  = (GridViewRow)((Button)sender).Parent.Parent;
                click = ((Label)row.Cells[1].FindControl("Label2")).Text;
                foreach (GridViewRow item in GridView2.Rows)
                {
                    item.BackColor = Color.White;
                    if (item.Cells[1].Text == click)
                    {
                        item.BackColor = Color.Red;
                    }
                }    
            }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 16, 2016 1:52 PM

All replies

  • User283571144 posted

    Hi Dan5,

    What I need to do is change the background color of the query field when the record has a query associated with it, but I do not have a checkbox anymore, so my old code does not work

    According to your description, I suggest you could foreach the gridviwe's row value in the button click event.

    If the row's cell value is equals with click value, then highlight it.

    More details, you could refer to follow codes:

    Note: Since I don't know how you get the query string, I get current row value.

    I have two gridview in a page.

    If I click the button, will highlight the row in the second gridview.

    Aspx:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" >
                <Columns>
                    <asp:TemplateField HeaderText="ID" SortExpression="ID">
                        <EditItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="url" SortExpression="url">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("url") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                                  <asp:Label ID="Label2" runat="server" Text='<%# Bind("url") %>'></asp:Label>
                            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <br />
            <asp:GridView ID="GridView2" runat="server"  ></asp:GridView>

    Code-behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Databind1();
                    Databind2();
                }
            }
    
            private void Databind1()
            {
                DataTable d1 = new DataTable();
                d1.Columns.Add("ID");
                d1.Columns.Add("url");
                d1.Rows.Add("1", "http://www.google.com");
                d1.Rows.Add("2", "http://www.facebook.com");
                GridView1.DataSource = d1;
                GridView1.DataBind();
            }
    
            private void Databind2()
            {
                DataTable d1 = new DataTable();
                d1.Columns.Add("ID");
                d1.Columns.Add("url");
                d1.Rows.Add("1", "http://www.google.com");
                d1.Rows.Add("2", "http://www.facebook.com");
                GridView2.DataSource = d1;
                GridView2.DataBind();
            }
     protected void Button1_Click(object sender, EventArgs e)
            {
                string click = string.Empty;
                GridViewRow row  = (GridViewRow)((Button)sender).Parent.Parent;
                click = ((Label)row.Cells[1].FindControl("Label2")).Text;
                foreach (GridViewRow item in GridView2.Rows)
                {
                    item.BackColor = Color.White;
                    if (item.Cells[1].Text == click)
                    {
                        item.BackColor = Color.Red;
                    }
                }    
            }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 16, 2016 1:52 PM
  • User1028962535 posted

    Thanks for your help

    Friday, September 16, 2016 4:49 PM