C#實(shí)現(xiàn)改變DataGrid某一行和單元格顏色的方法
本文所述實(shí)例主要實(shí)現(xiàn)WPF項(xiàng)目中C#改變DataGrid某一行和單元格顏色的功能。分享給大家供大家參考。具體方法如下:
如果要改變DataGrid某一行的顏色、高度,以及某個(gè)單元格的顏色、單元格字體的顏色,就必需取到datagrid的一行和一行的單元格,通過查找相關(guān)資料及測試總結(jié)出如下實(shí)例代碼,現(xiàn)記錄下來便于大家參考使用。
1、前臺(tái)WPF界面添加一個(gè)DataGrid控件,并添加兩列(便于編寫,達(dá)到目的即可)
<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、創(chuàng)建一個(gè)數(shù)據(jù)源并綁定,此處是創(chuàng)建一個(gè)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、如果在項(xiàng)目中把創(chuàng)建數(shù)據(jù)源、綁定數(shù)據(jù)源、對(duì)datagrid進(jìn)行操作(改變行的顏色、高度)都寫在一個(gè)事件中,其中在取datagrid的row時(shí)出現(xiàn)錯(cuò)誤:未將對(duì)象引用設(shè)置到對(duì)象的實(shí)例。
解決的方法:
//創(chuàng)建數(shù)據(jù)源、綁定數(shù)據(jù)源 if (!Window.GetWindow(dataGrid1).IsVisible) { Window.GetWindow(dataGrid1).Show(); } dataGrid1.UpdateLayout(); //可以獲取某一行、某一行的單元格
相信本文所述對(duì)大家的C#程序設(shè)計(jì)有一定的借鑒作用。
- c#重寫TabControl控件實(shí)現(xiàn)關(guān)閉按鈕的方法
- C#實(shí)現(xiàn)更改MDI窗體背景顏色的方法
- C#通過重寫Panel改變邊框顏色與寬度的方法
- C#在RichTextBox中顯示不同顏色文字的方法
- C# Winform使用擴(kuò)展方法實(shí)現(xiàn)自定義富文本框(RichTextBox)字體顏色
- C#取得隨機(jī)顏色的方法
- C#中改變DataGridView控件邊框顏色的方法
- c#構(gòu)造ColorComboBox(顏色下拉框)
- c#遍歷System.drawing.Color下面的所有顏色以及名稱以查看
- C#簡單獲取屏幕鼠標(biāo)坐標(biāo)點(diǎn)顏色方法介紹
- C#更改tabControl選項(xiàng)卡顏色的方法
相關(guān)文章
C#實(shí)現(xiàn)Excel導(dǎo)入sqlite的方法
這篇文章主要介紹了C#實(shí)現(xiàn)Excel導(dǎo)入sqlite的方法,是C#程序設(shè)計(jì)中非常重要的一個(gè)實(shí)用技巧,需要的朋友可以參考下2014-09-09在Framework 4.0中:找出新增的方法與新增的類(二)
為什么動(dòng)態(tài)加載程序集無法找出Framework 4.0 和Framwork2.0 新增的方法和類2013-05-05