wpf datagrid - Selecting specific colomns / rows

Jun 14, 2009 at 8:26 AM

Hi

I am having a problem with my datagrid.
I need to add an enevt to my column so that when the user selects one ot the 3 checkboxes, the remaining 2 get unchecked

here is the code

    <Grid Height="287" Width="424">
        <Border BorderThickness="10" BorderBrush="CadetBlue" CornerRadius="45" MouseRightButtonDown="Border_MouseRightButtonDown" MouseLeftButtonDown="Border_MouseLeftButtonDown" Margin="2,0,40,0">
        <Border.Background>
            <SolidColorBrush Color="white" />
        </Border.Background>
        <my:DataGrid Margin="8,0,8,10" Name="DataGrid1"  AutoGenerateColumns="False" ItemsSource="{Binding}" RowDetailsVisibilityMode="Visible" Height="207" Width="335">
                <my:DataGrid.Columns>
                    <my:DataGridTextColumn Header="Home" Binding="{Binding Path=home}"/>
                    <my:DataGridCheckBoxColumn Header="H" Binding="{Binding Path=hw}"  Mouse.MouseUp="DataGridCheckBoxColumn_MouseUp"/>
                    <my:DataGridCheckBoxColumn Header="D" Binding="{Binding Path=draw}"/>
                    <my:DataGridCheckBoxColumn Header="A" Binding="{Binding Path=awayw}"/>
                    <my:DataGridTextColumn Header="Away" Binding="{Binding Path=away}"/>
                    <my:DataGridTextColumn Header="Time" Binding="{Binding Path=heurematch}"/>
                </my:DataGrid.Columns>
            </my:DataGrid>
        </Border>
        <Button Height="23" Margin="132,0,182,16" Name="Button1"  VerticalAlignment="Bottom">Valider</Button>
    </Grid>

   ConnectDatabase()

        Dim cmd As New MySql.Data.MySqlClient.MySqlCommand("SELECT id, DATEDIFF(date_limite, NOW()) as reste,DATE_FORMAT( date_limite, '%D %M %Y') as date_limite, competition_name FROM competition_public WHERE idsport=1 And idchampionnat=15  ORDER BY reste LIMIT 0,1;", conn)
        Dim dat As MySql.Data.MySqlClient.MySqlDataReader
        Dim idCompet As Integer

        dat = cmd.ExecuteReader
        Do While dat.Read()
            idCompet = IIf(dat("id") <> 0, dat("id"), 0)
        Loop
        dat.Close()

        Dim sql As New MySql.Data.MySqlClient.MySqlCommand
        'Dim query = "SELECT c1.equipe as home, c1.id as homeId, c2.equipe as away, c2.id as awayId, matchs.heurematch, matchs.id as idM FROM competition_public_match AS compet "
        'query &= " INNER JOIN matchs_soccer AS matchs ON matchs.id=compet.idmatch "
        'query &= " INNER JOIN clubs as c1 ON c1.id=matchs.h_id And c1.idsport=1 "
        'query &= " INNER JOIN clubs as c2 ON c2.id=matchs.v_id And c2.idsport=1 "
        'query &= " WHERE compet.idcompetition = " & idCompet

        Dim query = "SELECT c1.equipe as home, 0 as hw,0 as awayw,0 as draw,c2.equipe as away, matchs.heurematch FROM competition_public_match AS compet "
        query &= " INNER JOIN matchs_soccer AS matchs ON matchs.id=compet.idmatch "
        query &= " INNER JOIN clubs as c1 ON c1.id=matchs.h_id And c1.idsport=1 "
        query &= " INNER JOIN clubs as c2 ON c2.id=matchs.v_id And c2.idsport=1 "
        query &= " WHERE compet.idcompetition = " & idCompet

        sql.CommandText = query
        sql.Connection = conn
        'Dim sqlD As MySql.Data.MySqlClient.MySqlDataReader = sql.ExecuteReader
        Dim ds As New System.Data.DataSet
        Dim sqla As New MySql.Data.MySqlClient.MySqlDataAdapter

        sqla.SelectCommand = sql
        sqla.Fill(ds, "users")
        Dim w3 As New grille

        Dim x As Integer
        Dim myMusic As New ObservableCollection(Of match)()
        For x = 0 To ds.Tables("users").Rows.Count - 1
            myMusic.Add(New match(ds.Tables("users").Rows(x)("home"), ds.Tables("users").Rows(x)("hw"), ds.Tables("users").Rows(x)("draw"), ds.Tables("users").Rows(x)("awayw"), ds.Tables("users").Rows(x)("away"), ds.Tables("users").Rows(x)("heurematch")))
        Next

        m_orders = ds.Tables("users")
        w3.DataGrid1.ItemsSource = myMusic

        w3.Show()

I must be able to clear the remaining 2 checkboxes on a row when the 3rd is checked

 

thanking you in anticipation

Jun 15, 2009 at 9:00 AM

The proper way to do this would be to implement it in your business objects rather than in the DataGrid that displays the information. Such that when you set hw to true, set draw and awayw to false. Then raise a PropertyChanged event (see INotifyPropertyChanged) for the ones you update, and you're all set.

Jun 15, 2009 at 2:58 PM

Hi,

 

thanks a lot for your help

 

can you please tell me how i can implement the event?

 

here is my data class

 

Public Class match

    Public Event PropertyChanged()

    Public Sub New(ByVal id As Integer, ByVal idmatch As Integer, ByVal home As String, ByVal hw As Boolean, ByVal draw As Boolean, ByVal awayw As Boolean, ByVal away As String, ByVal heurematch As TimeSpan)
        Me.home = home
        Me.hw = hw
        Me.draw = draw
        Me.awayw = awayw
        Me.away = away
        Me.heurematch = heurematch
        Me.idmatch = idmatch
        Me.id = id
    End Sub

    Private _id As String
    Public Property id() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Private _home As String
    Public Property home() As String
        Get
            Return _home
        End Get
        Set(ByVal value As String)
            _home = value
        End Set
    End Property
    Private _hw As Boolean
    Public Property hw() As Boolean
        Get
            Return _hw
        End Get
        Set(ByVal value As Boolean)
            _hw = value
        End Set
    End Property
    Private _awayw As Boolean
    Public Property awayw() As Boolean
        Get
            Return _awayw
        End Get
        Set(ByVal value As Boolean)
            _awayw = value
        End Set
    End Property

    Private _draw As Boolean
    Public Property draw() As Boolean
        Get
            Return _draw
        End Get
        Set(ByVal value As Boolean)
            _draw = value
        End Set
    End Property

    Private _away As String
    Public Property away() As String
        Get
            Return _away
        End Get
        Set(ByVal value As String)
            _away = value
        End Set
    End Property

    Private _heurematch As TimeSpan
    Public Property heurematch() As TimeSpan
        Get
            Return _heurematch
        End Get
        Set(ByVal value As TimeSpan)
            _heurematch = value
        End Set
    End Property

    Private _idmatch As String
    Public Property idmatch() As Integer
        Get
            Return _idmatch
        End Get
        Set(ByVal value As Integer)
            _idmatch = value
        End Set
    End Property

    Private Sub match_PropertyChanged() Handles Me.PropertyChanged

    End Sub
End Class

Jun 15, 2009 at 9:08 PM

Hi,

You need to implement INotifyPropertyChanged on your class.  There is an example on this page.  http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

You can also check out the Data Binding documentation on MSDN.

Margaret