I have a DataGrid in WPF. DataGrid is tied to data in a DataTable. DataGrid has ItemsSource = "{Binding}". There are no DataGridTextColumn records. Columns / rows of a DataTable are dynamically defined through database tables.
In the example below, the database column names are placed in a list, and the DataTable is set so that the first column has a name and a series of integer values ββthat follow:
DataTable theDataTable= new DataTable(); theDataTable.Columns.Add("Name", typeof(string)); for(int i = 0; i < columnNameList.Count ; i++) theDataTable.Columns.Add(columnNameList[i], typeof(int));
Next, the DataGrid is connected (bound) to the DataTable
theDataGrid.DataContext = theDataTable;
Then the rows are set to a DataTable. Again, the number of rows is determined by users in the database. User names are put into the list after the database is called. The default string values ββare zero.
for (int i = 0; i < usersNames.Count ; i++) { DataRow newDataRow = theTradersDataTable.NewRow(); newDataRow["Name"] = usersNames[i]; for(int j = 0; i < columnNameList.Count ; j++) { newDataRow[columnNameList[j]] = 0; } theDataTable.Rows.Add(newDataRow); }
Following the setup of the DataGrid, I connect to an API that provides callbacks with user information updates. After the update, I define a row and column that should update the underlying DataTable, which in turn updates the DataGrid. All of this works great.
//inside some API callback theDataTable.Rows[rowNumber][columnNumber] = someDataSentInViaAPI;
Finally, the question is: how do you update the specific color of the DataGrid cell based on the value in that cell. When the API callbacks, where I get the data, the cells are constantly updated. Therefore, I would like to update the cell color when updates appear.
I am not married to my design. I do not want this model to match the code I already made. I am for most ideas, but I would prefer not to write down the column names of the hard code. I would also prefer a method that allows me to check the color of the cells, and then redraw if necessary (for speed / processor reasons). Thank you in advance for your help.