改变DataGrid某一行和单元格的颜色

前段时间做WPF项目,需要改变DataGrid某一行的颜色、高度,以及某个单元格的颜色、单元格字体的颜色,自然就必需取到datagrid的一行和一行的单元格,网上也是搜索了好久才找到,记录下来便于使用。

1、前台WPF界面添加一个DataGrid控件,并添加两列(便于编写,达到目的即可)

<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、创建一个数据源并绑定,此处是创建一个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、获取单行

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、获取单元格

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、如果在项目中把创建数据源、绑定数据源、对datagrid进行操作(改变行的颜色、高度)都写在一个事件中,其中在取datagrid的row时出现错误:未将对象引用设置到对象的实例。

解决的方法:

   //创建数据源、绑定数据源

 if (!Window.GetWindow(dataGrid1).IsVisible)
            {
                Window.GetWindow(dataGrid1).Show();
            }
            dataGrid1.UpdateLayout();

//可以获取某一行、某一行的单元格
时间: 2024-08-29 01:45:26

改变DataGrid某一行和单元格的颜色的相关文章

Flex4 DataGrid实现可复制单元格,同时解决自定义GridItemRenderer出现1009错误的方法

原创内容,如需转载,请注明出处,谢谢 最近在项目中发现Flex的DataGrid不支持内容复制,在涉及到保护敏感数据时倒是很有用处,但大部分情况下,我们还是希望客户能够直接复制DataGrid单元格中的内容进行快速操作,这个与用户体验背道而驰的问题一定要解决,因此想到直接自定义DataGrid的项呈现器来实现. 呆毛如下: 本来以为可以非常顺利,哪知居然给我碰到了一个BUG,网上查了查,也有不少童鞋们碰到了这个问题,不过似乎都没有有效的解决方案,这个主要是思维固化了,总认为官方的就一定是正确的.

javascript如何便利表格中的行和单元格

javascript如何便利表格中的行和单元格:尽管现在普遍使用div布局来替代表格,这并不说明table是一无是处的,在制作表格类型的结构的时候还是使用table更为有效.在实际应用中有时候需要便利表格中的行和单元格,下面就通过实例简单介绍一下如何实现此功能.代码如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author&quo

ExtJS4.2 Grid知识点三:改变表格Grid单元格背景颜色

在ExtJS4.2 Grid知识点一:改变表格Grid单元格文字颜色一文中讲解了如何改变单元格中文字颜色,接下来在本章学习如何改变Grid中单元格的背景颜色,显示结果如图片: 在线演示  /  示例代码 实现方式同样是为Grid中该列自定义renderer函数,查询ExtJS 4.2 API得知,Ext.grid.column.Column的renderer属性可以是一个函数也可以是字符串,这个知识点是通过函数来实现的.函数参数列表如下: value : 当前待渲染的单元格值,即表格中某行某列的

Easyui datagrid 设置内容超过单元格宽度时自动换行显示

datagrid 设置内容超过单元格宽度时自动换行显示 by:授客 QQ:1033553122 测试环境 jquery-easyui-1.5.3 问题描述 单元格内容超过单元格宽度不会自动化换行.如下: 图1: 图2: 解决方法 定义表格时,设置nowrap属性为false. <table id='tt' class="easyui-datagrid" title="Basic DataGrid" style="width:700px;height:

c#winform中如何修改ListView控件每个单元格的颜色

ListView在View属性为Details的时候它呈现的方式和DataGirdView差不多,它的每一行都是一个ListViewItem,然而每个ListViewItem都有一个BackColor的属性,但是设置了这个属性却没有任何作用.因为是ListView的每一行的样式都继承的父控件的样式所以无法改变. 解决方案: ListViewItem item; item = new ListViewItem(new string[] { "NAME","" });

NPOI Excel 单元格背景颜色对照表

NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillPattern 为单元格背景色的填充样式. NPOI Excel 单元格背景颜色设置方法以及颜色对照表: 1 2 3 4 5 6 ICellStyle style = workbook.CreateCellStyle(); style.FillForegroundColor = NPOI.HSSF.U

怎样将Excel包含某字符的单元格填充颜色

在处理数据的时候,xmyanke想将Excel中包含某字符的单元格填充蓝色,比较容易看清,弄了好一阵子都没完成,最后试用条件格式处理了一下,终于实现了. 比如要将A1到A12区间包含数字1的单元格填充成蓝色,点击A1按shift键再点击A12选中A1:A12区间所有单元格,在菜单栏中选“格式”-“条件格式” 在弹出的条件格式对话框中选“单元格数值”“等于”“1”,点击“格式”按钮,弹出的单元格格式对话框中的“图案”选项,选择蓝色单元格底色,确定 这样设置以后,Excel包含某字符的单元格填充颜色

C#对gridView的列和行以及单元格进行颜色设置

①字体 其中gridView是gridView的Name(下同) gridView[column, row].Style.ForeColor = Color.Red;//设置第row行column列的单元格字体颜色 gridView.Rows[row].DefaultCellStyle.ForeColor = Color.Red;//设置第row行的行字体颜色gridView.Columns[column].DefaultCellStyle.ForeColor = Color.Yellow;//

DevGridControl单元格背景色和单元格文字颜色设置

1.拖一个gridControl控件在 窗体上 2.添加三列 分别是 BgColor,BgColor2 , FontColor  分别显示单元格颜色 单元格渐变颜色 单元格字体颜色 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { List<object> list = new Li