Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Datagrid BoolColumn or checkbox column problem
 

Datagrid BoolColumn or checkbox column problem

("Problem"->i want to get checkboxes in the datagrid column. and the table(SQL) name is BrokSlab and the column name is "PercentageStatus".)

There is a datagrid Column "PercentageStatus" which contains a values '0 and 1'.Now i want to get this column in the datagrid instead of display 1 and 0 in the datagrid column i want to display checkboxes.Now the problem is i am getting this column with all checkboxes are checked and it look like hidden or i can say it looks like disable. so this is my coding. i am past all my coding. there 1 class which is called "DataBase" and the other is "SupportBrokSlab" all working is in this class "SupportBrokSlab".

GetData is used to get the filled table

now

in 1 we get all checkboxes are checked

and in 2 we get values 0 and 1

can anyone tell me whats is the problem in this?

Thanks

#Region "Public"

Public objDb As New Database

#End Region

#Region "Private"

Private mTable As DataTable

#End Region

 

Dim PercentageStatus_Col As New DataGridBoolColumn

Dim TableStyle As New DataGridTableStyle

Dim strquery As String = "Select * FROM BrokSlab "

Dim i As Integer

mTable = objDb.GetData(strquery)

DGrid.DataSource = mTable

---------------------1-----------------------------
   mTable = objDB.GetData(strquery)
    DGrid.DataSource = mTable
    PercentageStatus_Col.MappingName = "PercentageStatus"
    PercentageStatus_Col.HeaderText = "Percentage Status"
    PercentageStatus_Col.Width = 100
    PercentageStatus_Col.NullValue = True
    TableStyle.GridColumnStyles.Add(PercentageStatus_Col)
    DGrid.TableStyles.Add(TableStyle)

    For i = 0 To mTable.Rows.Count - 1
      If mTable.Rows(i).Item("PercentageStatus") = 1 Then
        PercentageStatus_Col.TrueValue = True
      End If
      If mTable.Rows(i).Item("PercentageStatus") = 0 Then
        PercentageStatus_Col.FalseValue = False
      End If
    Next
----------------------1------------------------------

 Public Function GetData(ByVal query As String) As DataTable
    Try
      'msqlConnection = New SqlConnection(mConnStr)
      mdataTable = New DataTable
      msqlConnection.Open()
      mSqlAdapter = New SqlDataAdapter(query, msqlConnection)
      mSqlAdapter.Fill(mdataTable)
      'mSqlAdapter.Fill(mDataSet)
      msqlConnection.Close()
      Return mdataTable
    Catch ex As Exception

    End Try
  End Function
--------------------------------------------------------
in the above coding we get the column which is all checked but i want to get the

checkbox value from the data table but i cannot doing this. i dont know what is

the problem

---------------------2-----------------------------------
  Dim i As Integer
    Dim dc As New DataColumn
    Dim ds As New DataSet
    'Dim dr As DataRow

    With dc
      .ColumnName = "PercentageStatus"
      .DataType = GetType(Boolean)
      .DefaultValue = False
    End With

    ds = objDB.GetDataBroker(strquery, "BrokSlab", dc, DGrid,

"PercentageStatus")
    DGrid.DataSource = ds.Tables("BrokSlab")
    TableStyle.MappingName = "BrokSlab"

    PercentageStatus_Col.MappingName = "PercentageStatus"
    PercentageStatus_Col.HeaderText = "Percentage Status"
    PercentageStatus_Col.Width = 100
    PercentageStatus_Col.NullValue = False
    TableStyle.GridColumnStyles.Add(PercentageStatus_Col)
    DGrid.TableStyles.Add(TableStyle)
--------------------2---------------------------------------
  Public Function GetDataBroker(ByVal query As String, ByVal tableName As

String, ByVal DataCol As DataColumn, ByVal dg As DataGrid, ByVal colName As

String) As DataSet
    Try
      Dim ds As New DataSet
      Dim dc As New DataColumn
      dc = DataCol

      msqlConnection.Open()
      mSqlAdapter = New SqlDataAdapter(query, msqlConnection)
      mSqlAdapter.FillSchema(ds, SchemaType.Mapped, tableName)

      If Not ds.Tables(tableName).Columns.Contains(colName) Then
        ds.Tables(tableName).Columns.Add(dc)
        mSqlAdapter.Fill(ds, tableName)

      End If

      msqlConnection.Close()
      Return ds
    Catch ex As Exception
      MessageBox.Show(ex.ToString)
    End Try
  End Function
--------------------------------------------------------------
and in the above coding we get the value 1 and 0. but i want the checkbox. please tell me.

Ather.Abbas  Friday, October 27, 2006 7:02 AM

If you want to use a checkbox column with on a data column which contains a 0 or 1 you need to set the truevalue and false value



Dim ds As New DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim da As SqlDataAdapter
Dim conn As SqlConnection
Dim strConn As String
strConn = "Server = .;"
strConn &= "Database = NorthWind; Integrated Security = SSPI;"
conn = New SqlConnection(strConn)
da = New SqlDataAdapter("Select * from Employees", conn)
da.FillSchema(ds, SchemaType.Mapped, "Employees")
Dim dc As New DataColumn
With dc
.ColumnName = "GetsBonus"
.DataType = GetType(Integer)
.DefaultValue = 0
End With
ds.Tables("Employees").Columns.Add(dc)
da.Fill(ds, "Employees")
DataGrid1.DataSource = ds.Tables("Employees")
Dim ts As New DataGridTableStyle
ts.MappingName = "Employees"
Dim textCol As New DataGridTextBoxColumn
textCol.MappingName = "LastName"
textCol.HeaderText = "Last Name"
textCol.Width = 120
ts.GridColumnStyles.Add(textCol)

Dim boolCol As New DataGridBoolColumn
boolCol.MappingName = "GetsBonus"
boolCol.HeaderText = "Give Bonus"
boolCol.Width = 80
boolCol.TrueValue = 1
boolCol.FalseValue = 0
boolCol.NullValue = 0

ts.GridColumnStyles.Add(boolCol)
DataGrid1.TableStyles.Add(ts)
End Sub

Ken Tucker  Friday, October 27, 2006 10:58 AM

If you want to use a checkbox column with on a data column which contains a 0 or 1 you need to set the truevalue and false value



Dim ds As New DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim da As SqlDataAdapter
Dim conn As SqlConnection
Dim strConn As String
strConn = "Server = .;"
strConn &= "Database = NorthWind; Integrated Security = SSPI;"
conn = New SqlConnection(strConn)
da = New SqlDataAdapter("Select * from Employees", conn)
da.FillSchema(ds, SchemaType.Mapped, "Employees")
Dim dc As New DataColumn
With dc
.ColumnName = "GetsBonus"
.DataType = GetType(Integer)
.DefaultValue = 0
End With
ds.Tables("Employees").Columns.Add(dc)
da.Fill(ds, "Employees")
DataGrid1.DataSource = ds.Tables("Employees")
Dim ts As New DataGridTableStyle
ts.MappingName = "Employees"
Dim textCol As New DataGridTextBoxColumn
textCol.MappingName = "LastName"
textCol.HeaderText = "Last Name"
textCol.Width = 120
ts.GridColumnStyles.Add(textCol)

Dim boolCol As New DataGridBoolColumn
boolCol.MappingName = "GetsBonus"
boolCol.HeaderText = "Give Bonus"
boolCol.Width = 80
boolCol.TrueValue = 1
boolCol.FalseValue = 0
boolCol.NullValue = 0

ts.GridColumnStyles.Add(boolCol)
DataGrid1.TableStyles.Add(ts)
End Sub

Ken Tucker  Friday, October 27, 2006 10:58 AM
thanks a lot
Ather.Abbas  Saturday, October 28, 2006 6:35 AM
there are 2 datagrid comboboxcolumns country and capital if i select country the capital of the country automatically display. if i select capital the country display accordingly...

Note: i want to fill the comboxbox value from the table(sql).

how we add datagrid combobox column?

Ather.Abbas  Saturday, October 28, 2006 6:38 AM
Is it possible to have a Check box for a DataGridBoolean Column?

The Check Box acts as- Whenc Checked - all records will be selected i.e. all the check boxes in the chck box columnwill be checked andvice-versa
When Unchked - all are unchecked and vice-versa.
TRKReddy  Friday, August 28, 2009 1:05 PM

You can use google to search for other answers

Custom Search

More Threads

• Export dataset to excel C# windows form
• BindingSource.Current question
• Datagridview / Bindinglist display refresh question
• OnValidating event not firing in DataGridView
• runtime binding to list of user defined object
• One to one relationships
• Bound Controls in a Tab Page
• Resizing a user control
• Leaving extra margin blank space below the last row in the grid
• Problem on moving file from One directory to another direcytory --- The process cannot access the file because it is being use