Forum  Commercial Foru...  Commercial Foru...  Add CheckBox to Bound DataGridView?
Previous Previous
 
Next Next
New Post 3/16/2010 12:29 PM
  jharmon@blinfo.com
837 posts
1st Level Poster




Add CheckBox to Bound DataGridView? 
Modified By jharmon@blinfo.com  on 3/16/2010 2:43:47 PM)

I am wanting to add a checkbox to a bound datagridview so that a user can make a selection by checking the first column of the datagridview row.  Here is what I have done so far:

1.) I added a Boolean column to my datarow. 
DataRow.Columns.Add("Selected", typeof(CheckBox));

2.) I make the datagridview NOT readonly and set all columns to readonly except the selected column.

3.) I move the column to the front of the datagridview columns.
DataGridColumn.DisplayIndex = 0;

4.) I now want to create an event on the clicking of the checkbox column. 
*** This is where I am not sure what to do.  How do I trap the clicking of the checkbox so that I can keep track of what rows are selected?

============================================

Sorry, I guess I posted too soon.  I found what I was looking for in the CellValueChanged event. -- You can disregard this post.


Thanks, Joe
 
New Post 3/16/2010 2:44 PM
  palli
11189 posts
1st Level Poster




Re: Add CheckBox to Bound DataGridView? 

Hi Joe,

Hey !!

I already wrote the code before I noticed your last remark, so I will post it anyway

You probably realized by now that setting the DisplayIndex will not affect the e.ColumnIndex in the CellValueChanged event.

 

Imports Gizmox.WebGUI.Forms
Public Class DGVForm
 
    Private intSelColIndex As Integer
 
    Private Sub DGVForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim DT As DataTable = New DataTable
        DT.Columns.Add("Col1", GetType(String))
        DT.Columns.Add("Col2", GetType(String))
        For i As Integer = 0 To 10
            DT.Rows.Add("Row " + i.ToString, i.ToString)
        Next
        DataGridView1.DataSource = DT
 
        Dim checkCol As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn
        checkCol.Name = "Selected"
        DataGridView1.Columns.Add(checkCol)
        checkCol.DisplayIndex = 0
        intSelColIndex = checkCol.Index
 
    End Sub
 
    Private Sub DataGridView1_CellValueChanged(ByVal sender As System.Object, _
                                               ByVal e As Gizmox.WebGUI.Forms.DataGridViewCellEventArgs) _
                                               Handles DataGridView1.CellValueChanged
        If e.ColumnIndex = intSelColIndex _
            AndAlso e.RowIndex >= 0 _
            AndAlso e.RowIndex < DataGridView1.Rows.Count - 1 Then
            MessageBox.Show("Value of Select on Row=" + _
                            e.RowIndex.ToString() + " is now " + _
                            DataGridView1.Rows(e.RowIndex).Cells(intSelColIndex).Value.ToString())
        End If
    End Sub
End Class

Palli

 


Páll Björnsson - Visual WebGui support team - Email: support@visualwebgui.com
 
Previous Previous
 
Next Next
  Forum  Commercial Foru...  Commercial Foru...  Add CheckBox to Bound DataGridView?
Azure banner
.NET Web, Cloud and Mobile application delivery platform | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2011 Visual WebGui®       Visual WebGui weblog on ASP.NET Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS