ここのサンプルはうまく動作していないので参考程度にして頂ければと思います。
以下では、ToolkitのDataGridのDataGridCellにコードからアクセスする方法を示しています。
Visual Studio 2010のDataGridの場合については、次を参考にしてください。
VisualStudio2010のDataGridのセルにアクセスする方法
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Windows.Controls;
using System.IO;
namespace DataGridProject
{
/// <summary>
/// Window1.xaml の相互作用ロジック
/// </summary>
public partial class Window1 : Window
{
DataTable dt = new DataTable();
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SqlConnectionStringBuilder bldr = new SqlConnectionStringBuilder();
bldr.DataSource = ".";
bldr.IntegratedSecurity = true;
bldr.InitialCatalog = "Northwind";
using (SqlConnection conn = new SqlConnection(bldr.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT CustomerID,City FROM Customers";
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
this.dataGrid1.DataContext = dt;
}
private void dataGrid1_SelectedCellsChanged(object sender, Microsoft.Windows.Controls.SelectedCellsChangedEventArgs e)
{
DataGrid dg = sender as DataGrid;
//クリックしたセルを含む行を取得
DataRowView cell = e.AddedCells[0].Item as DataRowView;
//クリックしたセルの列のインデックスを取得
int columnIndex = dg.Columns.IndexOf(e.AddedCells[0].Column);
int index = -1;
//選択されたセルのある行に入っているオブジェクトを取得。
//データベースからLINQで取得した時に無名クラスで一行を表していたので、
//そのクラスがここに現れる。
object o = e.AddedCells[0].Item;
//選択された行のクラスと同じものであればそこが行番号になる。
for (int i = 0; i < dg.Items.Count; i++)
{
if (dg.Items[i] == o)
{
index = i;
break;
}
}
//DataGridの行オブジェクトを取得
DataGridRow dgr = dg.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow;
DataGridCell dgc = null;
//クリックしたセルのデータを取得
string data = cell.Row[columnIndex].ToString();
DataGridRow temp;
string strTemp;
for (int i = 0; i < dg.Items.Count; i++)
{
//行のインデックスから行を取得する
temp = dg.ItemContainerGenerator.ContainerFromIndex(i) as DataGridRow;
//nullの時があるのでチェック
if (temp != null)
{
//今の行のデータを取得
strTemp = ((TextBlock)dg.Columns[columnIndex].GetCellContent(temp)).Text;
//コンテナがDataGridCellなのでこのようにして取得
dgc = this.dataGrid1.Columns[columnIndex].GetCellContent(temp).Parent as DataGridCell;
if (strTemp == data && temp == dgr)
{
//赤い色に設定
dgc.Background = new SolidColorBrush(Colors.Red);
}
else
{
dgc.Background = new SolidColorBrush(Colors.White);
}
}
}
}
}
}
VB.NET
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.Windows.Controls
Imports System.IO
Class Window1
Dim dt As DataTable = New DataTable()
Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Dim bldr As SqlConnectionStringBuilder = New SqlConnectionStringBuilder()
bldr.DataSource = "."
bldr.IntegratedSecurity = True
bldr.InitialCatalog = "Northwind"
Using conn As SqlConnection = New SqlConnection(bldr.ConnectionString)
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "SELECT CustomerID,City FROM Customers"
cmd.Connection = conn
Dim sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
sda.Fill(dt)
End Using
End Using
Me.dataGrid1.DataContext = dt
End Sub
Private Sub dataGrid1_SelectedCellsChanged(ByVal sender As System.Object, ByVal e As Microsoft.Windows.Controls.SelectedCellsChangedEventArgs) Handles dataGrid1.SelectedCellsChanged
Dim dg As DataGrid = CType(sender, DataGrid)
'クリックしたセルを含む行を取得
Dim cell As DataRowView = CType(e.AddedCells(0).Item, DataRowView)
'クリックしたセルの列のインデックスを取得
Dim columnIndex As Integer = dg.Columns.IndexOf(e.AddedCells(0).Column)
Dim index As Integer = -1
'選択されたセルのある行に入っているオブジェクトを取得。
'データベースからLINQで取得した時に無名クラスで一行を表していたので、
'そのクラスがここに現れる。
Dim o As Object = e.AddedCells(0).Item
'選択された行のクラスと同じものであればそこが行番号になる。
For i As Integer = 0 To dg.Items.Count - 1 Step 1
If dg.Items(i) Is o Then
index = i
Exit For
End If
Next i
'DataGridの行オブジェクトを取得
Dim dgr As DataGridRow = CType(dg.ItemContainerGenerator.ContainerFromIndex(index), DataGridRow)
Dim dgc As DataGridCell = Nothing
'クリックしたセルのデータを取得
Dim data As String = cell.Row(columnIndex).ToString()
Dim temp As DataGridRow
Dim strTemp As String
For i As Integer = 0 To dg.Items.Count - 1 Step 1
'行のインデックスから行を取得する
temp = CType(dg.ItemContainerGenerator.ContainerFromIndex(i), DataGridRow)
'Nothingの時があるのでチェック
If Not temp Is Nothing Then
'今の行のデータを取得
strTemp = CType(dg.Columns(columnIndex).GetCellContent(temp), TextBlock).Text
'コンテナがDataGridCellなのでこのようにして取得
dgc = CType(Me.dataGrid1.Columns(columnIndex).GetCellContent(temp).Parent, DataGridCell)
If strTemp = data And temp Is dgr Then
'赤い色に設定
dgc.Background = New SolidColorBrush(Colors.Red)
Else
dgc.Background = New SolidColorBrush(Colors.White)
End If
End If
Next i
End Sub
End Class
