?
Solved

Datagrid checkbox  column - How to Prevent  multiple checkbox selection ?

Posted on 2006-10-29
6
Medium Priority
?
1,746 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
Independent Software Vendors: 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 1000 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

Technology Partners: 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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

764 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