none
Listbox or Listview with binding source and multiple column? RRS feed

  • Question

  • Hi,

    I would like to have 2 columns in a Listbox or Listview with checkbox to select a row . My data's are from data table i know i can bind them with .atasource, displaymember & value member, Is there a way to one more column in the listbox/view with databindings?

    Thanks

    Thursday, September 5, 2019 9:31 AM

All replies

  • Hi

    Why not use a DataGridView. It's what a DataGridView is for, and easily customised.

    It was unclear if you want 2 columns PLUS a checkbox column or 2 columns INC checkbox column - easily fixed in code.

    Here is example.

    ' Form1 with BLANK DataGridView1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim dt As New DataTable("Freddy")
    
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		' create and fill test data
    		With dt
    			.Columns.Add("CheckBox", GetType(Boolean))
    			.Columns.Add("One", GetType(Double))
    			.Columns.Add("Two", GetType(String))
    
    			Dim r As New Random
    			For i As Integer = 1 To 10
    				.Rows.Add(r.Next(0, 2) = 1, i / 3, " Any String ")
    			Next
    
    		End With
    		DataGridView1.DataSource = dt
    
    		' customise DataGridView to suit
    		With DataGridView1
    			.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
    			.RowHeadersVisible = False
    			.Font = New Font(DataGridView1.Font.FontFamily, 12)
    			.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
    
    			.Columns("One").DefaultCellStyle.BackColor = Color.Pink
    			.Columns("One").DefaultCellStyle.Format = "0.00"
    			.Columns("One").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight
    			.Columns("One").DefaultCellStyle.Padding = New Padding(0, 0, 4, 0)
    
    			.Columns("Two").DefaultCellStyle.BackColor = Color.Green
    			.Columns("Two").DefaultCellStyle.ForeColor = Color.White
    			.Columns("Two").DefaultCellStyle.Font = New Font(DataGridView1.Font.FontFamily, 12, FontStyle.Bold)
    		End With
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, September 5, 2019 11:39 AM
    Thursday, September 5, 2019 11:38 AM
  • Hi

    Why not use a DataGridView. It's what a DataGridView is for, and easily customised.

    It was unclear if you want 2 columns PLUS a checkbox column or 2 columns INC checkbox column - easily fixed in code.

    Here is example.

    ' Form1 with BLANK DataGridView1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim dt As New DataTable("Freddy")
    
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		' create and fill test data
    		With dt
    			.Columns.Add("CheckBox", GetType(Boolean))
    			.Columns.Add("One", GetType(Double))
    			.Columns.Add("Two", GetType(String))
    
    			Dim r As New Random
    			For i As Integer = 1 To 10
    				.Rows.Add(r.Next(0, 2) = 1, i / 3, " Any String ")
    			Next
    
    		End With
    		DataGridView1.DataSource = dt
    
    		' customise DataGridView to suit
    		With DataGridView1
    			.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
    			.RowHeadersVisible = False
    			.Font = New Font(DataGridView1.Font.FontFamily, 12)
    			.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
    
    			.Columns("One").DefaultCellStyle.BackColor = Color.Pink
    			.Columns("One").DefaultCellStyle.Format = "0.00"
    			.Columns("One").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight
    			.Columns("One").DefaultCellStyle.Padding = New Padding(0, 0, 4, 0)
    
    			.Columns("Two").DefaultCellStyle.BackColor = Color.Green
    			.Columns("Two").DefaultCellStyle.ForeColor = Color.White
    			.Columns("Two").DefaultCellStyle.Font = New Font(DataGridView1.Font.FontFamily, 12, FontStyle.Bold)
    		End With
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    Hallo Les, Thanks for the answer, is it possible to add the checkbox option to column "two" i mean i dont want separate column for checkbox (it is just a preference if possible). My goal is to import selected rows from the data table to another datatable.
    Thursday, September 5, 2019 11:59 AM
  • Hi

    Within reason, anything is possible :)

    Try this one.

    ' Form1 with BLANK DataGridView1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim dt As New DataTable("Freddy")
    
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		' create and fill test data
    		With dt
    			.Columns.Add("One", GetType(String))
    			.Columns.Add("CheckBox", GetType(Boolean))
    
    			Dim r As New Random
    			For i As Integer = 1 To 10
    				.Rows.Add(" Any String ", r.Next(0, 2) = 1)
    			Next
    
    		End With
    		DataGridView1.DataSource = dt
    
    		' customise DataGridView to suit
    		With DataGridView1
    			.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
    			.RowHeadersVisible = False
    			.Font = New Font(DataGridView1.Font.FontFamily, 12)
    			.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
    
    			.Columns("One").DefaultCellStyle.BackColor = Color.Green
    			.Columns("One").DefaultCellStyle.ForeColor = Color.White
    			.Columns("One").DefaultCellStyle.Font = New Font(DataGridView1.Font.FontFamily, 12, FontStyle.Bold)
    		End With
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    Thursday, September 5, 2019 12:12 PM
  • Hi

    Within reason, anything is possible :)

    Try this one.

    ' Form1 with BLANK DataGridView1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim dt As New DataTable("Freddy")
    
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		' create and fill test data
    		With dt
    			.Columns.Add("One", GetType(String))
    			.Columns.Add("CheckBox", GetType(Boolean))
    
    			Dim r As New Random
    			For i As Integer = 1 To 10
    				.Rows.Add(" Any String ", r.Next(0, 2) = 1)
    			Next
    
    		End With
    		DataGridView1.DataSource = dt
    
    		' customise DataGridView to suit
    		With DataGridView1
    			.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
    			.RowHeadersVisible = False
    			.Font = New Font(DataGridView1.Font.FontFamily, 12)
    			.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
    
    			.Columns("One").DefaultCellStyle.BackColor = Color.Green
    			.Columns("One").DefaultCellStyle.ForeColor = Color.White
    			.Columns("One").DefaultCellStyle.Font = New Font(DataGridView1.Font.FontFamily, 12, FontStyle.Bold)
    		End With
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    Hi, Les, sorry i dont see any text next to the checkbox. I want to include checkbox with the text from one of the column. Not the checkbox alone.
    • Edited by Shan1986 Thursday, September 5, 2019 12:59 PM
    Thursday, September 5, 2019 12:58 PM
  • Within reason, anything is possible :)


    Hi, Les, sorry i dont see any text next to the checkbox. I want to include checkbox with the text from one of the column. Not the checkbox alone.

    Hi

    I do not believe that is within reason, and I for one do not lnow that it is possible in a DataGridView. (of course, someone else will be unable to resist putting me right :)

    *

    It would be possible to just build a series of Label/CheckBox pairs up to the number of data items you have (within Reason)


    Regards Les, Livingston, Scotland






    • Edited by leshay Friday, September 6, 2019 12:04 AM too many nots
    Thursday, September 5, 2019 1:52 PM
  • Hello,

    There are no standard way to combine columns in a DataGridView or ListView, a ListBox would be totally off-base.

    You could work the style of the DataGridView columns e.g. a CheckBox and TextBox and get this along with some coding but guess what, that's mimicking a CheckedListBox.

    Some formatting with done in the property window of the DataGridView.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            For index As Integer = 0 To 10
                If index Mod 2 = 0 Then
                    DataGridView1.Rows.Add(New Object() {False, "A"})
                Else
                    DataGridView1.Rows.Add(New Object() {True, "B"})
                End If
    
            Next
    
        End Sub
    
        Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) _
            Handles DataGridView1.CellPainting
    
            If e.ColumnIndex = 0 OrElse e.ColumnIndex = 1 Then
                e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None
            End If
            If e.ColumnIndex = 0 Then
                e.AdvancedBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None
            End If
            e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None
    
        End Sub
    End Class
    

    The only advantage for the above againsts a CheckedListBox is you have full access to editing data. With a CheckedListBox you can only check items but if that is okay set the DataSource to a DataTable and DisplayMember. To get at say the selected item (but will not indicate it's checked or not that is more work unlike with a DataGridViewCheckbox column).

    Dim row = CType(CheckedListBox1.SelectedItem, DataRow)
    In closing I've given you things to consider and starting points for two directions.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, September 5, 2019 11:44 PM
    Moderator
  • Hello,

    There are no standard way to combine columns in a DataGridView or ListView, a ListBox would be totally off-base.

    You could work the style of the DataGridView columns e.g. a CheckBox and TextBox and get this along with some coding but guess what, that's mimicking a CheckedListBox.

    Some formatting with done in the property window of the DataGridView.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            For index As Integer = 0 To 10
                If index Mod 2 = 0 Then
                    DataGridView1.Rows.Add(New Object() {False, "A"})
                Else
                    DataGridView1.Rows.Add(New Object() {True, "B"})
                End If
    
            Next
    
        End Sub
    
        Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) _
            Handles DataGridView1.CellPainting
    
            If e.ColumnIndex = 0 OrElse e.ColumnIndex = 1 Then
                e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None
            End If
            If e.ColumnIndex = 0 Then
                e.AdvancedBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None
            End If
            e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None
    
        End Sub
    End Class

    The only advantage for the above againsts a CheckedListBox is you have full access to editing data. With a CheckedListBox you can only check items but if that is okay set the DataSource to a DataTable and DisplayMember. To get at say the selected item (but will not indicate it's checked or not that is more work unlike with a DataGridViewCheckbox column).

    Dim row = CType(CheckedListBox1.SelectedItem, DataRow)
    In closing I've given you things to consider and starting points for two directions.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Hall Karen, Thanks , Now how do i add checkboxes to one of the datagridview column which is bound to a datatable? if i loop through datagridview and set the checkboxes as in your example, does it change the datatable too or only in datagridview? because i want to import selected rows from datasource to another datatable.
    Friday, September 6, 2019 8:37 AM
  • Hi,

    Do you resolve the issue? If you resolve the issue, please mark the helpful as answer. It will be beneficial to other community.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 12, 2019 7:41 AM
    Moderator