473,394 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Datagrid with checkbox and SQL

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
Nov 20 '05 #1
2 1136
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
-------------------

"Thomas A" <th**************@msn.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
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

Nov 20 '05 #2
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

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Just D | last post by:
Hi All, Does anybody know how to write the following code? I have a database table with a few columns including: ID , "Task" , ForceRun . I need to show the DataGrid on the ASPX page with...
4
by: Jim Heavey | last post by:
Hello, I am starting to learn how to use the Datagrid and I have a couple of questions. My datagrid as a checkbox in it. It looks like the following in the datagrid... <asp:TemplateColumn...
2
by: Sebi | last post by:
Hello all is it possible to add a checkbox in a DataGrid for Boolean Data? Thanks in advance
0
by: mike | last post by:
Hi there: I've read an excellent "how to"-article by Microsoft (no. 306227) - partly cited cited at the end of this email). I have implemented the code related to the part "How to Add a...
4
by: Mike | last post by:
Hi, Is there a possibility to have one of the Web Control Datagrid's column as a Calendar when editing data? Any resources on this subject? Thanks Mike
7
by: Lars Netzel | last post by:
If I put a checkbox in a datagrid (ASP.NET) and set the Autopostback to true I can catch OnChange event on checkbox but how do I then catch what DataGridItemIndex is? Can I use some Event in the...
2
by: Mortar | last post by:
i have a datagrid with 2 columns. the 1st column contains an id which will be used by the database for the selected checkbox records. the 2nd column is a template column containing a server...
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
3
by: Fao, Sean | last post by:
I have a DataGrid that I'm adding CheckBox controls to at runtime (in the code behind) and I'm not sure if I'm doing it correctly. First of all, I noticed that the MyDataGrid.Columns.Add() method...
7
by: rn5a | last post by:
The first column of a DataGrid has a CheckBox for all the rows. I want that when users check a CheckBox, the BackColor of that entire row in the DataGrid should change to a different color. To...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.