How to change Yes/No (True/False) results from SQL query to checkboxes in DataGrid

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 16
Reputation: ads248 is an unknown quantity at this point 
Solved Threads: 0
ads248 ads248 is offline Offline
Newbie Poster

How to change Yes/No (True/False) results from SQL query to checkboxes in DataGrid

 
0
  #1
Feb 18th, 2008
Hi

I've populates a DataGridView from the results of a SQL query programmatically, so I haven't had to define the number of columns, etc.

Just before I display it, I'd like to change the columns with Yes/No in them with checkboxes.

Any ideas how I can do this programmatically?

Regards

Andrew
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 8
Reputation: ashishparihar is an unknown quantity at this point 
Solved Threads: 0
ashishparihar ashishparihar is offline Offline
Newbie Poster

Re: How to change Yes/No (True/False) results from SQL query to checkboxes in DataGrid

 
0
  #2
Feb 18th, 2008
Hi
in your presentation layer logic class you can manage it
ex.
i have a class student with data member
1. string StudentName
2. int Age
3. bool IsDelete

so on rowdatabound event on your grid you can acess that column by column number(in above should be 3) and check that it contain true or false and according to that assign yes or no vales to that column
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 16
Reputation: ads248 is an unknown quantity at this point 
Solved Threads: 0
ads248 ads248 is offline Offline
Newbie Poster

Re: How to change Yes/No (True/False) results from SQL query to checkboxes in DataGrid

 
0
  #3
Feb 18th, 2008
Hi

Thanks for your response, but I think you misunderstand my requirement..

I have it displaying yes or no as it is passed in 1 or 0 from the sql query.

What I want to do is change the yes/nos in the resulting DataGridView column to checkboxes i.e. checked for the yes & unchecked for the nos.

Is there an attribute to the column that will do this or convert it.

i.e Column(3).????????????

Regards

Andrew
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 16
Reputation: ads248 is an unknown quantity at this point 
Solved Threads: 0
ads248 ads248 is offline Offline
Newbie Poster

Re: How to change Yes/No (True/False) results from SQL query to checkboxes in DataGrid

 
0
  #4
Feb 20th, 2008
Any ideas guys, as I'm struggling to find anything on this?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 121
Reputation: manal is an unknown quantity at this point 
Solved Threads: 17
manal's Avatar
manal manal is offline Offline
Junior Poster

Re: How to change Yes/No (True/False) results from SQL query to checkboxes in DataGrid

 
0
  #5
Feb 20th, 2008
i hope this artical will help you
"give only what u willing to receive "
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 16
Reputation: ads248 is an unknown quantity at this point 
Solved Threads: 0
ads248 ads248 is offline Offline
Newbie Poster

Re: How to change Yes/No (True/False) results from SQL query to checkboxes in DataGrid

 
0
  #6
Feb 21st, 2008
Hi

Thanks.

I understand the concept of that article to add a column with a checkbox, but I have the following code which populates the data grid from my SQL query. I end up with several columns which have either "True" or "False" in their cells. My question is, can I convert a column so that it has a checkbox instead of the "True" or "False". i.e. Columns(3) for example, or can the following code be rewritten, so that it makes these columns contain checkboxes from the outset?

Andrew

  1. Me.dgdBrowseCttrLocns.Columns.Clear()
  2.  
  3. Dim sql As String
  4.  
  5. sql = "select CttrLocn_AACCode as [AAC Code],CttrLocn_AIMSNo as [AIMS No],CttrLocn_CttrName as [Cttr Name]" _
  6. + ",CttrLocn_GFE_Held as [GFA Held]" _
  7. + ",cast(round(CttrLocn_Materiality,5)as decimal (20,5)) as Materiality" _
  8. + ",CttrLocn_PriorityCttr as [Priority Contractor]" _
  9. + ",CttrLocn_AACGuidelinesMet as [Meets AAC Guidelines]" _
  10. + ",CttrLocn_Address1 + ' ' + CttrLocn_Address2 + ' ' + CttrLocn_Address_Town + ' '" _
  11. + "+ CttrLocn_Address_County + ' ' + CttrLocn_Address_PostCode as Address,CttrLocn_Remarks as Remarks" _
  12. + ",CttrLocn_Issues as Issues,CttrLocn_NoLines as [Num Lines]" _
  13. + ",cast(cast(round(CttrLocn_Materiality/(select sum(CttrLocn_Materiality) from cttrlocation)*100,5)" _
  14. + "as decimal(7,5))as varchar(12)) + '%' as [% Total Materiality]" _
  15. + "from cttrlocation compute sum(cast(round(cttrlocn_materiality,5)as decimal(20,5)))"
  16.  
  17. Me.Cursor = Cursors.WaitCursor
  18.  
  19. Dim cmd As New SqlCommand(sql, aiidb2.Connection)
  20. cmd.Connection.Open()
  21.  
  22. dgdBrowseCttrLocns.Visible = False
  23.  
  24. Dim myReader As SqlDataReader = cmd.ExecuteReader
  25. Dim record As Integer = 0
  26. Dim column As Integer
  27. Dim header As String
  28. header = ""
  29. While myReader.Read()
  30. record = record + 1
  31. Dim tmp(200) As String
  32. For column = 0 To myReader.FieldCount - 1
  33. If record = 1 Then
  34. dgdBrowseCttrLocns.ColumnCount = column + 1
  35. dgdBrowseCttrLocns.Columns(column).Name = myReader.GetName(column)
  36. End If
  37. tmp(column) = myReader.GetValue(column).ToString()
  38. Next
  39. dgdBrowseCttrLocns.Rows.Add(tmp)
  40. End While
  41.  
  42. With dgdBrowseCttrLocns
  43. .Columns(0).Width = 60
  44. .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  45. .Columns(1).Width = 60
  46. .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  47. .Columns(2).Width = 250
  48. .Columns(3).Width = 60
  49. .Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  50.  
  51. .Columns(4).Width = 90
  52. .Columns(5).Width = 80
  53. .Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  54. .Columns(6).Width = 80
  55. .Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  56. .Columns(7).Width = 200
  57. .Columns(8).Width = 200
  58. .Columns(9).Width = 200
  59. .Columns(10).Width = 60
  60. .Columns(10).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  61. .Columns(11).Width = 80
  62. .Columns(11).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  63. End With
  64.  
  65. dgdBrowseCttrLocns.Visible = True
  66.  
  67. cmd.Connection.Close()
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 121
Reputation: manal is an unknown quantity at this point 
Solved Threads: 17
manal's Avatar
manal manal is offline Offline
Junior Poster

Re: How to change Yes/No (True/False) results from SQL query to checkboxes in DataGrid

 
0
  #7
Feb 21st, 2008
My question is, can I convert a column so that it has a checkbox instead of the "True" or "False". i.e. Columns(3) for example
yes you can
after you establish the connection with database , and fill the dataset with data, assuming you have datagrid with name dgdFunctionArea
creat new table and define the columns

  1. Dim dtCol As DataColumn = Nothing 'Data Column variable
  2. Dim arrstrFunctionalArea As String() = Nothing 'string array variable
  3. Dim dtblFunctionalArea As New DataTable 'Data Table var
  4. Dim n As Integer
  5. n = 5 ' n is the number of columns in your table that are not checkbox
  6. arrstrFunctionalArea = New String(n)
  7. arrstrFunctionalArea(0) = "AAC Code"
  8. arrstrFunctionalArea(1) = "Cttr Name"
  9. ........
  10. 'Create the Data Table object which will then be used to hold columns and rows
  11. dtblFunctionalArea = New DataTable("TableName")
  12.  
  13. 'Add the string array of columns to the DataColumn object
  14. Dim i As Integer
  15. For i = 0 To 4
  16. Dim str As String = arrstrFunctionalArea(i)
  17. dtCol = New DataColumn(str)
  18. dtCol.DataType = System.Type.GetType("System.String")
  19. dtCol.DefaultValue = ""
  20. dtblFunctionalArea.Columns.Add(dtCol)
  21. Next i

now you created columns in table that are not check box as in the artical
after that you creat columns that will hold true/false values

  1. 'Add a Column with checkbox in the Grid
  2. 'create the data column object with the name TFColumn
  3. Dim dtcCheck As New DataColumn("TFColumn")
  4. 'Set its data Type
  5. dtcCheck.DataType = System.Type.GetType("System.Boolean")
  6. dtcCheck.DefaultValue = False 'Set the default value
  7. dtblFunctionalArea.Columns.Add(dtcCheck) 'Add the above column to the Data Table

after that you set the source of your datagrid as the Data Table createed above
  1. dgdFunctionArea.DataSource = dtblFunctionalArea

now you will fill the table you created from dataset by creating rows and then filling them from data in dataset

  1. Me.OleDbDataAdapter1.Fill(Me.DataSet11)
  2. Dim count As Integer
  3. count = Me.DataSet11.TableName.Count 'to gte the number of rows in dataset
  4. For i = 0 To count - 1
  5. Dim Row1 As DataRow
  6. Row1 = dtblFunctionalArea.NewRow() 'declaring a new row
  7. 'filling the row with values
  8. Row1.Item("AAC Code") = Me.DataSet11.TableName.Rows(i).Item(0)
  9. .
  10. .
  11. .
  12. Row1.Item("TFColumn") = Me.DataSet11.TableName.Rows(i).Item(3)
  13. .
  14. .
  15. dtblFunctionalArea.Rows.Add(Row1) 'to add the row you created to the table
  16. Next i
Last edited by manal; Feb 21st, 2008 at 12:42 pm.
"give only what u willing to receive "
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 16
Reputation: ads248 is an unknown quantity at this point 
Solved Threads: 0
ads248 ads248 is offline Offline
Newbie Poster

Re: How to change Yes/No (True/False) results from SQL query to checkboxes in DataGrid

 
0
  #8
Feb 24th, 2008
Hi

I get "value of type 'Integer' cannot be converted tp '1 dimensional array of Char" with the following line of code when attempting to define the columns as suggested: -

arrstrFunctionalArea = New String(n)

Thanks

Andrew
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 121
Reputation: manal is an unknown quantity at this point 
Solved Threads: 17
manal's Avatar
manal manal is offline Offline
Junior Poster

Re: How to change Yes/No (True/False) results from SQL query to checkboxes in DataGrid

 
0
  #9
Feb 24th, 2008
I get "value of type 'Integer' cannot be converted tp '1 dimensional array of Char" with the following line of code when attempting to define the columns as suggested: -

arrstrFunctionalArea = New String(n)
error means you defined something as integer then you tried to store in it array of char
I tried the code and I did not have this error
maybe you defined n like this
Dim n As String() ?
"give only what u willing to receive "
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 16
Reputation: ads248 is an unknown quantity at this point 
Solved Threads: 0
ads248 ads248 is offline Offline
Newbie Poster

Re: How to change Yes/No (True/False) results from SQL query to checkboxes in DataGrid

 
0
  #10
Feb 24th, 2008
No, defined as per your code, because I copied it and pasted it in ?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC