Change ASP.Net GridView Cell Background Color based on condition using C# and VB.Net

indradeo
 
on Nov 23, 2021 03:10 AM
406 Views

Dear Sir,

If Message Off then gridview column is in red how to do this.

namespace Network_auto_ping
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable pingResults = new DataTable();
            pingResults.Columns.AddRange(new DataColumn[] {
                    new DataColumn("Date", typeof(DateTime)),
                    new DataColumn("IP", typeof(string)),
                    new DataColumn("MacAddress",typeof(string)),
                    new DataColumn("MachineName",typeof(string)),
                    new DataColumn("Message",typeof(string)),
                    new DataColumn("Emp_Name",typeof(string))});
            try
            {
                pingResults.Clear();

                List<string> ipAddress = new List<string>();
                ipAddress.Add("10.1.250.192");
                ipAddress.Add("10.1.246.213");
                ipAddress.Add("10.1.150.119");
                ipAddress.Add("10.1.250.122");
                ipAddress.Add("10.1.246.233");
                ipAddress.Add("10.1.246.62");
                ipAddress.Add("10.1.246.4");
                ipAddress.Add("10.1.246.8");
                ipAddress.Add("10.1.250.11");
                ipAddress.Add("192.168.200.181");

                List<string> Emp_Name = new List<string>();
                Emp_Name.Add("Rakesh Bajaj");
                Emp_Name.Add("Navneet Goel");
                Emp_Name.Add("R K Bhanote");
                Emp_Name.Add("Kamlesh Soni");
                Emp_Name.Add("NOT IN NETWORK");
                Emp_Name.Add("Amrita PC");
                Emp_Name.Add("MTTP Intranet BACK-UP PC");
                Emp_Name.Add("PIMEJA PC");
                Emp_Name.Add("MAHADEV PC");
                Emp_Name.Add("Engineer(IT)");

                for (int i = 0; i < ipAddress.Count; i++)
                {
                    Ping ping = new Ping();
                    PingReply pingReply = ping.Send(ipAddress[i].ToString());
                    string message = (pingReply.Status == IPStatus.Success) ? "On" : "Off";
                    lock (pingResults.Rows.SyncRoot)
                    {
                        pingResults.Rows.Add(DateTime.Now, ipAddress[i], GetMacAddress(ipAddress[i]), GetMachineName(ipAddress[i]), message, Emp_Name[i]);
                    }
                    Thread.Sleep(1000);
                }
                GridView1.DataSource = pingResults;
                GridView1.DataBind();

            }
            catch (Exception ex)
            {

            }
        }

        public string GetMacAddress(string ipAddress)
        {
            string macAddress = string.Empty;
            System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
            pProcess.StartInfo.FileName = "arp";
            pProcess.StartInfo.Arguments = "-a " + ipAddress;
            pProcess.StartInfo.UseShellExecute = false;
            pProcess.StartInfo.RedirectStandardOutput = true;
            pProcess.StartInfo.CreateNoWindow = true;
            pProcess.Start();
            string strOutput = pProcess.StandardOutput.ReadToEnd();
            string[] substrings = strOutput.Split('-');
            if (substrings.Length >= 8)
            {
                macAddress = substrings[3].Substring(Math.Max(0, substrings[3].Length - 2))
                            + "-" + substrings[4] + "-" + substrings[5] + "-" + substrings[6]
                            + "-" + substrings[7] + "-"
                            + substrings[8].Substring(0, 2);
                return macAddress;
            }
            else
            {
                return "not found";
            }
        }

        private string GetMachineName(string ipAdress)
        {
            string machineName = string.Empty;
            try
            {
                IPHostEntry hostEntry = Dns.GetHostEntry(ipAdress);
                machineName = hostEntry.HostName;
            }
            catch (Exception ex)
            {

            }
            return machineName;
        }
    }
}

 

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download