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

  • 2020-09-16 07:45:54
  • OfStack

The example described in this article mainly implements the function of C# in WPF project to change the color of a certain line and cell of DataGrid. Share to everybody for everybody reference. The specific methods are as follows:

If you want to change the color and height of a certain line of DataGrid, as well as the color of a cell and the font color of a cell, you must take the cell of line 1 and line 1 of datagrid, and summarize the following example code by looking up relevant information and testing, which is now recorded for everyone's reference.

1. Add 1 DataGrid control to the foreground WPF interface and add two columns (easy to write and achieve the purpose)


<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">
  <DataGrid.ColumnHeaderStyle >
 <Style TargetType="DataGridColumnHeader">
   <Setter Property="Height" Value="50"></Setter>
 </Style>
  </DataGrid.ColumnHeaderStyle>
  <DataGrid.Columns>
 <DataGridTextColumn Header="id" Binding="{Binding Path=id}" ElementStyle="{StaticResource dgCell}"></DataGridTextColumn>
 <DataGridTextColumn Header="name" Binding="{Binding Path=name}" ElementStyle="{StaticResource dgCell}"></DataGridTextColumn>
  </DataGrid.Columns>
</DataGrid>

2. Create 1 data source and bind, in this case, create 1 datatable


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

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

this.dataGrid1.CanUserAddRows = false;
this.dataGrid1.ItemsSource = dt.DefaultView;

3. Get a single line


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

 if (i == 2)
 {
   row.Height = 50;
   row.Background = new SolidColorBrush(Colors.Blue);
   drv["id"] = 333;
 }

 if (drv["id"] == DBNull.Value)
 {
   row.Background = new SolidColorBrush(Colors.Green);
   row.Height = 8;
 }
}

4. Get the cell


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

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

  return childContent;
}

5. If the creation of data source, binding data source and operation on datagrid (changing the color and height of the row) are all written in one event in the project, there is an error when fetching row of datagrid: the object reference is not set to the instance of the object.

Solutions:


// Create and bind data sources 
    
if (!Window.GetWindow(dataGrid1).IsVisible)
{
 Window.GetWindow(dataGrid1).Show();
}
dataGrid1.UpdateLayout();

// You can get something 1 Row, 1 Row cell 

I believe that what I have described in this article can be used as a reference for your C# programming.


Related articles: