@dan1001

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

WPF DataGrid セル値について

VB.Net WPFアプリ DataGrid セル値について

WPFのDataGridからセルの値を上手く読み込む方法がわかりません。

DataGridに、チェックボックス、IPAddressと機器の型番を記入し、
チェックのある機器のみ、ソケット通信を行いたいとおもってます。

ですが、ネット検索しても中々該当の解決策に辿り着くことが出来ませんでした。

目標としては、String型の2次元配列に格納し、if DataGrid(0)(0) = Ischecked then
ソケット通信するという動きにしたいです。

簡単に参照または、書き込みをしたいです。

どなたか力添えお願いいたします。

試しているサンプルコード

Imports System.Windows
Imports System.Data
Imports System
Imports System.Collections.ObjectModel
Imports System.Collections.Generic
Imports System.Data.Objects
Imports System.Linq

Partial Public Class MainWindow
    Inherits Window

    Public Sub New()
        InitializeComponent()
        dataGrid.ItemsSource = New ObservableCollection(Of Product) From {
            New Product With {
                .Name = "化粧品",
                .Price = 1900,
                .Tax = 10
            },
            New Product With {
                .Name = "洗剤",
                .Price = 500,
                .Tax = 10
            },
            New Product With {
                .Name = "パン",
                .Price = 800,
                .Tax = 8
            },
            New Product With {
                .Name = "牛乳",
                .Price = 800,
                .Tax = 8
            }
        }


    End Sub


End Class

Public Class Product
    Private _name As String
    Private _price As Integer
    Private _tax As Double
    Public Property Name As String
        Get
            Return _name
        End Get
        Set(value As String)
            _name = value
        End Set
    End Property
    Public Property Price As Integer
        Get
            Return _price
        End Get
        Set(value As Integer)
            _price = value
        End Set
    End Property
    Public Property Tax As Double
        Get
            Return _tax
        End Get
        Set(value As Double)
            _tax = value
        End Set
    End Property
End Class

0 likes

1Answer

ちょっとやりたいことの意図がなかなかつかめなかったですが、
こんな感じでしょうか。

MainWindow.xaml
<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:VBWPFTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="30" />
        </Grid.RowDefinitions>
        <DataGrid Name="dataGrid" Grid.Row="0"
                  AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn Width="30">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding Path=IsChecked, UpdateSourceTrigger=PropertyChanged}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="IPAddress" Binding="{Binding IPAddress}" Width="200" />
                <DataGridTextColumn Header="Model" Binding="{Binding ModelNumber}" Width="200" />
            </DataGrid.Columns>
        </DataGrid>
        <Grid Grid.Row="1">
            <Button Click="Button_Click"
                    Content="Communication"
                    Height="24"
                    Width="120"/>
        </Grid>
    </Grid>
</Window>
MainWindow.xaml.vb
Imports System.Collections.ObjectModel

Class MainWindow

    Public Sub New()
        InitializeComponent()

        dataGrid.ItemsSource = New ObservableCollection(Of Product) From {
            New Product With {
                .IsChecked = False,
                .IPAddress = "192.168.0.1",
                .ModelNumber = "Model No.1"
            },
            New Product With {
                .IsChecked = False,
                .IPAddress = "192.168.0.2",
                .ModelNumber = "Model No.2"
            },
            New Product With {
                .IsChecked = False,
                .IPAddress = "192.168.0.3",
                .ModelNumber = "Model No.3"
            }
        }

    End Sub

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        For Each product As Product In dataGrid.ItemsSource
            If product.IsChecked Then
                Console.WriteLine("Start Communication with {0}", product.IPAddress)
            End If
        Next

    End Sub
End Class

Class Product
    Public Property IsChecked As Boolean

    Public Property IPAddress As String

    Public Property ModelNumber As String
End Class
0Like

Comments

  1. @dan1001

    Questioner

    有難うございます。望んでいたコードです。
    ちなみに、、対象のIPアドレスをx()に格納したいのですが、、教えていただけますか。

Your answer might help someone💌