Solved

Datagrid checkbox  column - How to Prevent  multiple checkbox selection ?

Posted on 2006-10-29
6
1,655 Views
Last Modified: 2007-12-19
Hi,
 
#####   I am develping a vb.net windows appln. I have a datagrid and I have added a checkbox column to the datagrid. How to restrict at
              one time   only  one checkbox  can be selected .  That is , on the given checkbox column of datagrid,  only  single checkbox can be
             selected at a time.

   (I  have added the msdn fix given for the singleclick behaviour of the checkbox . It works fine)
    I took it from
            http://msdn2.microsoft.com/en-us/library/ms996485.aspx#wnf_custdatagridtopic_04

================================

#####   Also, how to know the state of selection of a given checkbox ?  
                    me.Datagrid1(hi.Row, CheckBoxColumnIndex)  --   returns true when checkbox is not checked


    thanking you,
                 Sam
0
Comment
Question by:sgs1970
  • 2
  • 2
  • 2
6 Comments
 
LVL 13

Expert Comment

by:newyuppie
ID: 17828675
me.Datagrid1(hi.Row, CheckBoxColumnIndex)

i think first you put the column, then the row

me.Datagrid1(CheckBoxColumnIndex, hi.Row) .Value
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17829392
newyuppie

If it is indeed a DataGrid, rather than a DataGridView, then the Row reference should precede the Column reference.  But if it is a DataGridView, then the Column reference should precede the Row reference.

Here's the declaration for the DataGrid.Item Property (Int32, Int32)

Visual Basic (Declaration)
Public Default Property Item ( _
      rowIndex As Integer, _
      columnIndex As Integer _
) As Object


And here it is for the DataGridView.Item Property (Int32, Int32)

Visual Basic (Declaration)
Public Default Property Item ( _
      columnIndex As Integer, _
      rowIndex As Integer _
) As DataGridViewCell
 
Simple, it ain't ;-)

Roger
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17829497
good observation! i had no idea, one learns something every day...
NY
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sgs1970
ID: 17840092

Hi friends,
    I am using vs 2003, if that matters anyway. Is there any luck for me in restricting the checkbox behaviour ?    
     The ideal solution I am looking is  - if  a checkbox is selected and if another checkbox is selected again, then the first checkbox should be unchecked (thru the code)      

0
 
LVL 34

Accepted Solution

by:
Sancler earned 250 total points
ID: 17841899
Try this.

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents dg As System.Windows.Forms.DataGrid
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.dg = New System.Windows.Forms.DataGrid
        CType(Me.dg, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'dg
        '
        Me.dg.DataMember = ""
        Me.dg.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.dg.Location = New System.Drawing.Point(16, 48)
        Me.dg.Name = "dg"
        Me.dg.Size = New System.Drawing.Size(256, 192)
        Me.dg.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.dg)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.dg, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim dt As New DataTable("MyTable")
    Dim cm As CurrencyManager
    Dim CheckCol As New DataGridBoolColumn

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        fillTable()
        doTableStyle()
        dg.DataSource = dt
    End Sub

    Private Sub fillTable()
        Dim dc As New DataColumn("Check", GetType(Boolean))
        dc.DefaultValue = False
        dt.Columns.Add(dc)
        Dim dc1 As New DataColumn("Name", GetType(String))
        dt.Columns.Add(dc1)
        For i As Integer = 1 To 10
            Dim dr As DataRow = dt.NewRow
            dr("Name") = "Name " & i.ToString
            dt.Rows.Add(dr)
        Next
        cm = BindingContext(dt)

    End Sub


    Private Sub doTableStyle()

        Dim tableStyle As New DataGridTableStyle
        tableStyle.MappingName = "MyTable"

        CheckCol.MappingName = "Check"
        CheckCol.HeaderText = ""
        CheckCol.Width = 30
        'turn off tristate
        CheckCol.AllowNull = False
        tableStyle.GridColumnStyles.Add(CheckCol)

        Dim column As New DataGridTextBoxColumn
        column.MappingName = "Name"
        column.HeaderText = "Name"
        column.Width = 120
        tableStyle.GridColumnStyles.Add(column)

        dg.TableStyles.Add(tableStyle)

    End Sub

    Private Sub dg_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dg.MouseUp
        Dim thisRow As Integer = dg.CurrentRowIndex
        dg.EndEdit(CheckCol, thisRow, False)
        cm.EndCurrentEdit()
        If dg.Item(thisRow, 0) = True Then
            For i As Integer = 0 To dt.Rows.Count - 1
                If i <> thisRow Then
                    dt.Rows(i)(0) = False
                End If
            Next
        End If

    End Sub
End Class

It uses dummy data, and I haven't included the modification to "single-click" the checkbox column, but it illustrates the essence of the approach I think you will need.

Roger
0
 

Author Comment

by:sgs1970
ID: 17849020
Hi Roger,
   Excellent !!! Thank you so much


   Sam
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question