Gridview color not changing when a button is clicked

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Gridview color not changing when a button is clicked



I am currently developing a webpage that retrieves the data from SQLdatasource on the click of a button. I also included a function to highlight specific rows.
It works on page load however it doesn't work on a button click.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Timers;
using System.Drawing;

namespace WebApplication1

public partial class WebForm1 : System.Web.UI.Page


protected void Page_Load(object sender, EventArgs e)

filterButton.Click += new EventHandler(this.filterButton_Click);
prodTab.Rows[1].Cells[7].BackColor = Color.Red;




protected void filterButton_Click(object sender, EventArgs e)


string newQuery = "SELECT top 10 A.[LogOn],A.[Tcode],B.[Name],A.[Item],A.[ToLocation] AS [BIN],A.[Quantity],A.[ToStorageLocation] AS [Production Line],A." +
"[Flag],A.[Remarks] FROM [IBusiness].[dbo].[SY_TransferOrderLog] A INNER JOIN" + " [IBusiness].[dbo].[ST_SY_User] B ON A.[UserId] = B.[UserId] WHERE " +
"(Tcode ='T32' OR Tcode ='B32') And [LogOn] BETWEEN GETDATE()-1 AND GETDATE() and B.[User] = '" + TextBox1.Text + "' Order By LogOn desc";

string command = SqlDataSource.SelectCommand; // added just for debug purpose
SqlDataSource.SelectCommand = newQuery;

prodTab.Rows[1].Cells[7].BackColor = Color.Red;







2 Answers
2



Make sure you change the cell color AFTER (re)binding data to the GridView.



This works for coloring the cell


prodTab.DataSource = source;
prodTab.DataBind();

prodTab.Rows[1].Cells[1].BackColor = Color.Red;



This does not because it is being overwritten by the defaults.


prodTab.Rows[1].Cells[1].BackColor = Color.Red;

prodTab.DataSource = source;
prodTab.DataBind();





Hi this works!. Thank you very much. I would cast a vote if I could
– user3308595
Aug 12 at 23:45






You can accept the answer as the correct one. Then other user will also know the solution works.
– VDWWD
Aug 13 at 6:28



add runat="server" attribute to button in your aspx page





the runat="server" is set by default in the HTML CODE of the ASPX page.
– user3308595
Aug 13 at 5:15






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS