Solved

Datagrid checkbox  column - How to Prevent  multiple checkbox selection ?

Posted on 2006-10-29
6
1,673 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

732 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