Solved

Datagrid checkbox  column - How to Prevent  multiple checkbox selection ?

Posted on 2006-10-29
6
1,570 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Library to convert HTML to PDF. 8 55
Check if number is currency 15 31
Recommendation vb6 to vb.net or others 14 109
vb.net 2 37
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now