Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

How to implement virtual mode with a DataGridView, and DataTable for real time data?

$
0
0

I have a method which returns a dataTable with system information as below,

public DataTable SystemMonitor(ManagementScope connectionScope)
        {
            dt = new DataTable();
            SelectQuery systemMonitorQuery = new SelectQuery("SELECT Name,CreatingProcessID,PercentProcessorTime FROM win32_PerfFormattedData_PerfProc_Process");
            ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(connectionScope, systemMonitorQuery);
            if (!dt.Columns.Contains("Process ID"))
            {
                dt.Columns.Add("Process ID");
            }
            if (!dt.Columns.Contains("Process Name"))
            {
                dt.Columns.Add("Process Name");
            }
            if (!dt.Columns.Contains("CPU Usage"))
            {
                dt.Columns.Add("CPU Usage");
            }
            int counter = 0;
            foreach (ManagementObject item in searchProcedure.Get())
            {
                DataRow dr = dt.NewRow();
                foreach (PropertyData pd in item.Properties)
                {
                    dr[counter++] = pd.Value;
                }
                dt.Rows.Add(dr);
                counter = 0;
            }
            return dt;
        }

I want this method to run in one second intervals to retrieve system information continuously. Now I want to display this data in a gridview in my form. The data in the grid view also should update according to the data retrieved periodically. Since the data should load fast and the UI should stay responsive it seems that virtual mode is the way to go. Also I want to implement pagination in the gridview.

So far I have managed to display the data from the data table to the gridview using the following method,

private void CPU_Load(object sender, EventArgs e)
        {
            // Enable virtual mode. 
            this.dataGridView1.VirtualMode = true;

            // Connect the virtual-mode events to event handlers.  
            this.dataGridView1.CellValueNeeded += new
                DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
            this.dataGridView1.CellValuePushed += new
                DataGridViewCellValueEventHandler(dataGridView1_CellValuePushed);
            this.dataGridView1.NewRowNeeded += new
                DataGridViewRowEventHandler(dataGridView1_NewRowNeeded);

            // Add columns to the DataGridView.
            DataGridViewTextBoxColumn pid = new
                DataGridViewTextBoxColumn();
            pid.HeaderText = "PID";
            pid.Name = "PID";
            DataGridViewTextBoxColumn Name = new
                DataGridViewTextBoxColumn();
            Name.HeaderText = "Name";
            Name.Name = "Name";
            DataGridViewTextBoxColumn Usage = new
                DataGridViewTextBoxColumn();
            Usage.HeaderText = "CPU Usage";
            Usage.Name = "CPU Usage";
            this.dataGridView1.Columns.Add(pid);
            this.dataGridView1.Columns.Add(Name);
            this.dataGridView1.Columns.Add(Usage);
            this.dataGridView1.AutoSizeColumnsMode =
                DataGridViewAutoSizeColumnsMode.AllCells;

            SystemMonitor(connectionScope);
            // Set the row count, including the row for new records. 
            this.dataGridView1.RowCount = 50;
        }

        private void dataGridView1_CellValueNeeded(object sender,
    System.Windows.Forms.DataGridViewCellValueEventArgs e)
        {
            // If this is the row for new records, no values are needed. 
            if (e.RowIndex == this.dataGridView1.RowCount - 1) return;

            // Set the cell value to paint using the Customer object retrieved. 
            switch (this.dataGridView1.Columns[e.ColumnIndex].Name)
            {
                case "PID":
                    e.Value = dt.Rows[e.RowIndex]["Process ID"];
                    break;
                case "Name":
                    e.Value = dt.Rows[e.RowIndex]["Process Name"];
                    break;
                case "CPU Usage":
                    e.Value = dt.Rows[e.RowIndex]["CPU Usage"];
                    break;
            }
        }

Now I need to be able to continuously call the SystemMonitor() method and refresh the data in the gridview in realtime and also implement pagination.

Please advice.

Thanx in advance.


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>