Add checkbox to items in a datagrid

  • Thread starter Thread starter Bill Nguyen
  • Start date Start date
B

Bill Nguyen

I need to add a checkbox in front of all the rows in a datagrid so that
users can select/unselect them.

Any help is greatly appreciated!

Bill
 
Thanks Cor.

I was able to add the checkbox in front of each row. There are 2 problems
though:

1. The box is dimmed and has the check mark in it. I want the box unchecked
(default value = FALSE). When I click on it, I can check or uncheck the box,
but as soon as the cursor move to the next row, the box goes back to its
original state (dimmed with box checked!)


2. This column doesn't come from the datasource for the remaining columns.
How do I select only rows (in the datasource) with the checkbox checked?

Thanks again

Bill
 
B ill,

You have to set the boolean columns in a for each after that you have
created them to false

\\\
For each dr as datarow in dt
dr("mycolumn") = false
Next
///

To see if they are checked you do in fact in the same way or in a for index
loop.

I hope this helps,

Cor
 
Cor;
I spent a great deal of time trying almost everything and still got the same
results. I added a boolean column (datatype = bit) to the table and set
value to zero for all rows. The column still displays a checkmark and
behaves exactly the same way (doubleclick twice changes the check stat but
goes back to dim with checkmark as soon as the cursor leaves the row).

below is my code. Please take a look and let me know what I did wrong.

Thanks a million

Bill

-------------------------
DataGrid1.Enabled = True

Dim iType As Integer = 1

Dim iSort As Integer

Dim iBool As Boolean = False



dOrder = New DataSet("mpOrder")

Dim tOrder As DataTable

'tBol = Nothing

tOrder = New DataTable("mpOrderTable")

Dim cRowID As DataColumn

cRowID = New DataColumn("rowID")

Dim cRowSelect As DataColumn

cRowSelect = New DataColumn("rowSelect")

Dim cOrderID As DataColumn

cOrderID = New DataColumn("OrderID")

Dim cStID As DataColumn

cStID = New DataColumn("StID")

Dim cStName As DataColumn

cStName = New DataColumn("StName")

Dim cscDate As DataColumn

cscDate = New DataColumn("scDate")

Dim cscTime As DataColumn

cscTime = New DataColumn("scTime")

Dim cStat As DataColumn

cStat = New DataColumn("stat")

Try

tOrder.Columns.Add(cRowID)

tOrder.Columns.Add(cRowSelect) 'checkbox column

tOrder.Columns.Add(cOrderID)

tOrder.Columns.Add(cStID)

tOrder.Columns.Add(cStName)

tOrder.Columns.Add(cscDate)

tOrder.Columns.Add(cscTime)

tOrder.Columns.Add(cStat)



dOrder.Tables.Add(tOrder)

'---

'dNULL = False

dCount = 0

'

' MsgBox(rSQL)

dMain = MAPPOINTDataBoss.dMAPSelectView(rSQL)

If dMain.Tables(0).Rows.Count = 0 Then

Beep()

Call DisplayStatus("No records found! Nothing to display.", "red")

Exit Sub

Else

Call DisplayStatus("Total Order Records: " & (dMain.Tables(0).Rows.Count),
"red")

End If

Dim dOrderRow, dmainRow As DataRow

For Each dmainRow In dMain.Tables(0).Rows



dOrderRow = tOrder.NewRow

dOrderRow("rowID") = iType.ToString

iBool = False



dOrderRow("rowSelect") = dmainRow.Item("assigned")

dOrderRow("orderID") = dmainRow.Item("IF_orderID")

dOrderRow("stID") = dmainRow.Item("stationID")

dOrderRow("stName") = dmainRow.Item("stationName")

dOrderRow("scdate") = Format(dmainRow.Item("scheduledDate"), "MM/dd/yyyy")

dOrderRow("sctime") = dmainRow.Item("scheduledTime")

dOrderRow("stat") = dmainRow.Item("if_orderStatus")

tOrder.Rows.Add(dOrderRow)

dCount += 1



Next

' Datagrid table style setup

Dim ts1 As DataGridTableStyle

' ts1 = Nothing

ts1 = New DataGridTableStyle

ts1.MappingName = "mporderTable"

ts1.AlternatingBackColor = Color.LightGray

ts1.AllowSorting = False







Dim txtCol As DataGridTextBoxColumn

txtCol = New DataGridTextBoxColumn

txtCol.MappingName = "orderID"

txtCol.HeaderText = "Order#"

txtCol.ReadOnly = True

txtCol.Width = 60

ts1.GridColumnStyles.Add(txtCol)

'

txtCol = New DataGridTextBoxColumn

txtCol.MappingName = "stID"

txtCol.HeaderText = "Station#"

txtCol.ReadOnly = True

txtCol.Width = 70

ts1.GridColumnStyles.Add(txtCol)

txtCol = New DataGridTextBoxColumn

txtCol.MappingName = "stName"

txtCol.HeaderText = "Station Name"

txtCol.ReadOnly = True

txtCol.Width = 240

ts1.GridColumnStyles.Add(txtCol)

txtCol = New DataGridTextBoxColumn

txtCol.MappingName = "scDate"

txtCol.HeaderText = "Scheduled Date"

txtCol.ReadOnly = True

txtCol.Width = 80

ts1.GridColumnStyles.Add(txtCol)

txtCol = New DataGridTextBoxColumn

txtCol.MappingName = "scTime"

txtCol.HeaderText = "Time"

txtCol.ReadOnly = True

txtCol.Width = 60

ts1.GridColumnStyles.Add(txtCol)

txtCol = New DataGridTextBoxColumn

txtCol.MappingName = "stat"

txtCol.HeaderText = "Order Status"

txtCol.ReadOnly = True

txtCol.Width = 80

ts1.GridColumnStyles.Add(txtCol)

Dim boolCol As New DataGridBoolColumn

boolCol.MappingName = "rowSelect"

boolCol.HeaderText = "Select"

boolCol.Width = 50

' boolCol.n

boolCol.AllowNull = False

boolCol.ReadOnly = False

boolCol.FalseValue = False

boolCol.TrueValue = True

ts1.GridColumnStyles.Add(boolCol)

DataGrid1.TableStyles.Clear()

DataGrid1.TableStyles.Add(ts1)



Me.DataGrid1.DataSource = dOrder

Me.DataGrid1.DataMember = "mpordertable"

Call NoAppenRow()





Catch ex As Exception

MsgBox(ex.Message & ex.Source)

Catch ex As SqlException

MsgBox(ex.Number & ": " & ex.Message & ex.LineNumber)

End Try







End Sub
 
Bill,

Can you next time first copy the code in a notebook, and than copy it back
and in the message. In this way it is unreadable.

I think that you have tried a lot to fullfil your goal, see the row above,
normally the scheme should come from your fill, (what is not in the code so
I assume that is that function mappointdataboss.

However if you declare a table than you have to tell what kind of columns it
are (I never do that if it are only strings in samples).

By instance like this.
\\\
Dim cscDate As New DataColumn("scDate", GetType(System.DateTime))
Dim cRowSelect As New DataColumn("rowSelect", GetType(System.Boolean))
///

Now the datagrid knows that it is a boolean column and will act acoording to
that.

(I do not know it this is the answer, however as start and if not than
please the code back as I said, (including this of course).

I hope this helps sofar

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top