SoFunction
Updated on 2025-03-07

C# implements a method to change a row and cell color of DataGrid

The examples described in this article mainly implement the function of C# changing a certain row and cell color of DataGrid in WPF projects. Share it for your reference. The specific methods are as follows:

If you want to change the color and height of a row of DataGrid, as well as the color of a cell and the color of a cell font, you must take a row of datagrid and a row of cells, and summarize the following example code by searching for relevant information and testing. Now record it for your reference and use.

1. Add a DataGrid control on the front-end WPF interface and add two columns (easy to write, just achieve the goal)

<DataGrid AutoGenerateColumns="False" Height="642" HorizontalAlignment="Left" Margin="131,57,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="799" CanUserAddRows="True" LoadingRow="dataGrid1_LoadingRow" GridLinesVisibility="None">
  < >
 <Style TargetType="DataGridColumnHeader">
   <Setter Property="Height" Value="50"></Setter>
 </Style>
  </>
  <>
 <DataGridTextColumn Header="id" Binding="{Binding Path=id}" ElementStyle="{StaticResource dgCell}"></DataGridTextColumn>
 <DataGridTextColumn Header="name" Binding="{Binding Path=name}" ElementStyle="{StaticResource dgCell}"></DataGridTextColumn>
  </>
</DataGrid>

2. Create a data source and bind it, here is to create a datatable

DataTable dt = new DataTable();
(new DataColumn("id", typeof(int)));
(new DataColumn("name", typeof(string)));

for (int i = 0; i < 6; i++)
{
 DataRow dr = ();
 if (i == 3)
 {
   dr["id"] = ;
   dr["name"] = DBNull .Value ;
   (dr);
 }
 else
 {
   dr["id"] = i;
   dr["name"] = "tom" + ();
   (dr);
 }
}

this. = false;
this. = ;

3. Get a single line

for (int i = 0; i < this.; i++)
{
 DataRowView drv = [i] as DataRowView;
 DataGridRow row = (DataGridRow)this.(i);

 if (i == 2)
 {
    = 50;
    = new SolidColorBrush();
   drv["id"] = 333;
 }

 if (drv["id"] == )
 {
    = new SolidColorBrush();
    = 8;
 }
}

4. Get the cell

for (int i = 0; i < this.; i++)
{
 DataRowView drv = [i] as DataRowView;
 DataGridRow row = (DataGridRow)this.(i);
 if (i == 4)
 {
   DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
   DataGridCell cell = (DataGridCell)(1);
    = new SolidColorBrush();
 }
}

public static T GetVisualChild<T>(Visual parent) where T : Visual
{
  T childContent = default(T);
  int numVisuals = (parent);
  for (int i = 0; i < numVisuals; i++)
  {
 Visual v = (Visual)(parent, i);
 childContent = v as T;
 if (childContent == null)
 {
   childContent = GetVisualChild<T>(v);
 }
 if (childContent != null)
 {
   break;
 }
  }

  return childContent;
}

5. If you write in the project to create a data source, bind a data source, and operate on a datagrid (change the color and height of the row) in an event, an error occurs when taking the row of the datagrid: the object reference is not set to the instance of the object.

Solution:

//Create data source and bind data source    
if (!(dataGrid1).IsVisible)
{
 (dataGrid1).Show();
}
();

//Can get a certain row or a certain row of cells

I believe that this article will have a reference effect on everyone's C# programming.