Solved

Datagrid checkbox  column - How to Prevent  multiple checkbox selection ?

Posted on 2006-10-29
6
1,546 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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 …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

10 Experts available now in Live!

Get 1:1 Help Now