Datagrid with checkbox and SQL

  • Thread starter Thread starter Thomas A
  • Start date Start date
T

Thomas A

Hi !

I'm trying to use a datagrid with an checkbox column, I not shoure to
get it work on several points

I have got the rest of the data mapped to the grid but the checkbox
doesnt work.

1. The column type in the sql is Integer (values 0 or 1), is this ok or
should I change it to something else?

2. my code to load the grid

Sub LoadDataToGrid()

formatdatagrid()
DataSet11.Clear()

DataGrid1.DataSource = (DataSet11)


DataGrid1.DataMember = "RAT_Customer"


SqlDataAdapter1.SelectCommand.CommandText = "Select CustomerCode
as KundNr, SL01002 as KundNamn ,Pfaktor as Påslag, InOutputFile from
RAT_Customer, SL010100 where CustomerCode = SL01001"
SqlConnection1.Open()

SqlDataAdapter1.Fill(DataSet11)

SqlConnection1.Close()



End Sub


3. My code to format the grid

Dim grdColStyle1 As New DataGridTextBoxColumn
With grdColStyle1
.HeaderText = "Kund Nr"
.MappingName = "KundNr"
.Width = 50
End With

Dim grdColStyle2 As New DataGridTextBoxColumn
With grdColStyle2
.HeaderText = "Namn"
.MappingName = "KundNamn"
.Width = 90
End With

Dim grdColStyle3 As New DataGridTextBoxColumn
With grdColStyle3
.HeaderText = "Påslag traktamente (SEK)"
.MappingName = "Påslag"
.Width = 140
'.ReadOnly = True
End With

Dim grdColStyle4 As New DataGridBoolColumn
With grdColStyle4
.HeaderText = "Överföring till RAT"
.MappingName = "InOutputFile"
.Width = 130
.Alignment = HorizontalAlignment.Center
.ReadOnly = False

End With

' Add the style objects to the table style's collection of
' column styles. Without this the styles do not take effect.
grdTableStyle1.GridColumnStyles.AddRange _
(New DataGridColumnStyle() _
{grdColStyle1, grdColStyle2, grdColStyle3, grdColStyle4})
DataGrid1.TableStyles.Add(grdTableStyle1)


I should be very happy if anyone could tell whats wrong or whats missing
 
Hi,

It does not look like you are setting up your tablestyle right. Here
is an example using the northwind database. I assume you have the
northwind.mdb in the bin directory of your project folder and imports
system.data.oledb at the top of your file.

Public ds As New DataSet

Dim dv As DataView

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim conn As OleDbConnection

Dim strConn As String

Dim strSQL As String

Dim da As OleDbDataAdapter

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn += "Data Source = Northwind.mdb;"

conn = New OleDbConnection(strConn)

da = New OleDbDataAdapter("Select * From Products", conn)

da.Fill(ds, "Products")

SetupGrid()

dv = New DataView(ds.Tables("Products"))



With dv

..AllowNew = False

..AllowEdit = True

..AllowDelete = False

End With

DataGrid1.DataSource = dv

End Sub

Private Sub SetupGrid()

Dim ts As New DataGridTableStyle

ts.MappingName = "Products"



Dim colName As New DataGridTextBoxColumn

With colName

..MappingName = "ProductName"

..HeaderText = "Name"

..Width = 200

End With

Dim colDiscontinued As New DataGridBoolColumn

With colDiscontinued

..MappingName = "Discontinued"

..HeaderText = "Discontinued"

..Width = 80

End With

ts.GridColumnStyles.Add(colName)

ts.GridColumnStyles.Add(colDiscontinued)

DataGrid1.TableStyles.Add(ts)

ts = Nothing

colName = Nothing

colDiscontinued = Nothing

End Sub

Ken
-------------------
 
Hi Ken !

I looked in table "products" in the Nortwind database at my sql Server. It
should be BIT and not Integer at the field.
After the tablechange the grid worked correctly with true and false.

Thank you for opening my eyes


Regards Thomas
 
Back
Top