Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. display checkbox in columns of datagrid

display checkbox in columns of datagrid

Scheduled Pinned Locked Moved Visual Basic
help
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chrissy Callen
    wrote on last edited by
    #1

    i have 4 columns in my datagrid which at the moment are displaying "yes" or "no". My boss wants these existing columns to display checkboxes instead. Ive tried really hard to find a solution to this problem but so far nothing has worked :( Any help would be greatly appreciated - getting desparate now!! Chrissy Callen

    S R 2 Replies Last reply
    0
    • C Chrissy Callen

      i have 4 columns in my datagrid which at the moment are displaying "yes" or "no". My boss wants these existing columns to display checkboxes instead. Ive tried really hard to find a solution to this problem but so far nothing has worked :( Any help would be greatly appreciated - getting desparate now!! Chrissy Callen

      S Offline
      S Offline
      Syed Abdul Khader
      wrote on last edited by
      #2

      DataGridCheckBoxColumn class is there in the API. What is the problem for u in using it?

      C 1 Reply Last reply
      0
      • S Syed Abdul Khader

        DataGridCheckBoxColumn class is there in the API. What is the problem for u in using it?

        C Offline
        C Offline
        Chrissy Callen
        wrote on last edited by
        #3

        basically im very new to this :) i need code examples to help me figure it out. How exactly do i apply DataGridCheckBoxColumn to any particular column that already exists in my datagrid?? Chrissy Callen

        S 1 Reply Last reply
        0
        • C Chrissy Callen

          basically im very new to this :) i need code examples to help me figure it out. How exactly do i apply DataGridCheckBoxColumn to any particular column that already exists in my datagrid?? Chrissy Callen

          S Offline
          S Offline
          Syed Abdul Khader
          wrote on last edited by
          #4

          Sorry it is DataGridBoolColumn class. An example is available in MSDN. Just I m giving it below.. using System; using System.Data; using System.Windows.Forms; using System.Drawing; using System.ComponentModel; public class MyForm : Form { private DataTable myTable; private DataGrid myGrid = new DataGrid(); public MyForm() : base() { try { InitializeComponent(); myTable = new DataTable("NamesTable"); myTable.Columns.Add(new DataColumn("Name")); DataColumn column = new DataColumn ("id", typeof(System.Int32)); myTable.Columns.Add(column); myTable.Columns.Add(new DataColumn("calculatedField", typeof(bool))); DataSet namesDataSet = new DataSet(); namesDataSet.Tables.Add(myTable); myGrid.SetDataBinding(namesDataSet, "NamesTable"); AddTableStyle(); AddData(); } catch (System.Exception exc) { Console.WriteLine(exc.ToString()); } } private void grid_Enter(object sender, EventArgs e) { myGrid.CurrentCell = new DataGridCell(2,2); } private void AddTableStyle() { // Map a new TableStyle to the DataTable. Then // add DataGridColumnStyle objects to the collection // of column styles with appropriate mappings. DataGridTableStyle dgt = new DataGridTableStyle(); dgt.MappingName = "NamesTable"; DataGridTextBoxColumn dgtbc = new DataGridTextBoxColumn(); dgtbc.MappingName = "Name"; dgtbc.HeaderText= "Name"; dgt.GridColumnStyles.Add(dgtbc); dgtbc = new DataGridTextBoxColumn(); dgtbc.MappingName = "id"; dgtbc.HeaderText= "id"; dgt.GridColumnStyles.Add(dgtbc); DataGridBoolColumn db = new DataGridBoolColumn(); db.HeaderText= "less than 1000 = blue"; db.Width= 150; db.MappingName = "calculatedField"; dgt.GridColumnStyles.Add(db); myGrid.TableStyles.Add(dgt); // This expression instructs the grid to change // the color of the inherited DataGridBoolColumn // according to the value of the id field. If it's // less than 1000, the row is blue. Otherwise, // the color is yellow. db.Expression = "id < 1000"; } private void AddData() { // Add data with varying numbers for the id field. // If the number is over 1000, the cell will paint // yellow. Otherwise, it will be blue. DataRow dRow = myTable.NewRow(); dRow["Name"] = "name 1 "; dRow["id"] = 999; myTable.Rows.Add(dRow); dRow = myTable.NewRow(); dRow["Name"] = "name 2"; dRow["id"] = 2300; myTable.Rows.Add(dRow); dR

          C 1 Reply Last reply
          0
          • S Syed Abdul Khader

            Sorry it is DataGridBoolColumn class. An example is available in MSDN. Just I m giving it below.. using System; using System.Data; using System.Windows.Forms; using System.Drawing; using System.ComponentModel; public class MyForm : Form { private DataTable myTable; private DataGrid myGrid = new DataGrid(); public MyForm() : base() { try { InitializeComponent(); myTable = new DataTable("NamesTable"); myTable.Columns.Add(new DataColumn("Name")); DataColumn column = new DataColumn ("id", typeof(System.Int32)); myTable.Columns.Add(column); myTable.Columns.Add(new DataColumn("calculatedField", typeof(bool))); DataSet namesDataSet = new DataSet(); namesDataSet.Tables.Add(myTable); myGrid.SetDataBinding(namesDataSet, "NamesTable"); AddTableStyle(); AddData(); } catch (System.Exception exc) { Console.WriteLine(exc.ToString()); } } private void grid_Enter(object sender, EventArgs e) { myGrid.CurrentCell = new DataGridCell(2,2); } private void AddTableStyle() { // Map a new TableStyle to the DataTable. Then // add DataGridColumnStyle objects to the collection // of column styles with appropriate mappings. DataGridTableStyle dgt = new DataGridTableStyle(); dgt.MappingName = "NamesTable"; DataGridTextBoxColumn dgtbc = new DataGridTextBoxColumn(); dgtbc.MappingName = "Name"; dgtbc.HeaderText= "Name"; dgt.GridColumnStyles.Add(dgtbc); dgtbc = new DataGridTextBoxColumn(); dgtbc.MappingName = "id"; dgtbc.HeaderText= "id"; dgt.GridColumnStyles.Add(dgtbc); DataGridBoolColumn db = new DataGridBoolColumn(); db.HeaderText= "less than 1000 = blue"; db.Width= 150; db.MappingName = "calculatedField"; dgt.GridColumnStyles.Add(db); myGrid.TableStyles.Add(dgt); // This expression instructs the grid to change // the color of the inherited DataGridBoolColumn // according to the value of the id field. If it's // less than 1000, the row is blue. Otherwise, // the color is yellow. db.Expression = "id < 1000"; } private void AddData() { // Add data with varying numbers for the id field. // If the number is over 1000, the cell will paint // yellow. Otherwise, it will be blue. DataRow dRow = myTable.NewRow(); dRow["Name"] = "name 1 "; dRow["id"] = 999; myTable.Rows.Add(dRow); dRow = myTable.NewRow(); dRow["Name"] = "name 2"; dRow["id"] = 2300; myTable.Rows.Add(dRow); dR

            C Offline
            C Offline
            Chrissy Callen
            wrote on last edited by
            #5

            thanks but is there any chance of having this in vb.net code please?? Chrissy Callen

            S 1 Reply Last reply
            0
            • C Chrissy Callen

              thanks but is there any chance of having this in vb.net code please?? Chrissy Callen

              S Offline
              S Offline
              Syed Abdul Khader
              wrote on last edited by
              #6

              Imports System Imports System.Data Imports System.Windows.Forms Imports System.Drawing Imports System.ComponentModel Public Class MyForm Inherits System.Windows.Forms.Form Private components As System.ComponentModel.Container Private myTable As DataTable Private myGrid As DataGrid = New DataGrid Public Shared Sub Main() Application.Run(New MyForm) End Sub Public Sub New() Try InitializeComponent() myTable = New DataTable("NamesTable") myTable.Columns.Add(New DataColumn("Name")) Dim column As DataColumn = New DataColumn _ ("id", GetType(System.Int32)) myTable.Columns.Add(column) myTable.Columns.Add(New DataColumn _ ("calculatedField", GetType(Boolean))) Dim namesDataSet As DataSet = New DataSet("myDataSet") namesDataSet.Tables.Add(myTable) myGrid.SetDataBinding(namesDataSet, "NamesTable") AddData() AddTableStyle() Catch exc As System.Exception Console.WriteLine(exc.ToString) End Try End Sub Private Sub AddTableStyle() ' Map a new TableStyle to the DataTable. Then ' add DataGridColumnStyle objects to the collection ' of column styles with appropriate mappings. Dim dgt As DataGridTableStyle = New DataGridTableStyle dgt.MappingName = "NamesTable" Dim dgtbc As DataGridTextBoxColumn = _ New DataGridTextBoxColumn dgtbc.MappingName = "Name" dgtbc.HeaderText = "Name" dgt.GridColumnStyles.Add(dgtbc) dgtbc = New DataGridTextBoxColumn dgtbc.MappingName = "id" dgtbc.HeaderText = "id" dgt.GridColumnStyles.Add(dgtbc) Dim db As DataGridBoolColumnInherit = _ New DataGridBoolColumnInherit db.HeaderText = "less than 1000 = blue" db.Width = 150 db.MappingName = "calculatedField" dgt.GridColumnStyles.Add(db) myGrid.TableStyles.Add(dgt) ' This expression instructs the grid to change ' the color of the inherited DataGridBoolColumn ' according to the value of the id field. If it's ' less than 1000, the row is blue. Otherwise, ' the color is yellow. 'db.Expression = "id < 1000" End Sub Private Sub AddData() ' Add data with varying numbers for the id field. ' If the number is over 1000,

              C 1 Reply Last reply
              0
              • S Syed Abdul Khader

                Imports System Imports System.Data Imports System.Windows.Forms Imports System.Drawing Imports System.ComponentModel Public Class MyForm Inherits System.Windows.Forms.Form Private components As System.ComponentModel.Container Private myTable As DataTable Private myGrid As DataGrid = New DataGrid Public Shared Sub Main() Application.Run(New MyForm) End Sub Public Sub New() Try InitializeComponent() myTable = New DataTable("NamesTable") myTable.Columns.Add(New DataColumn("Name")) Dim column As DataColumn = New DataColumn _ ("id", GetType(System.Int32)) myTable.Columns.Add(column) myTable.Columns.Add(New DataColumn _ ("calculatedField", GetType(Boolean))) Dim namesDataSet As DataSet = New DataSet("myDataSet") namesDataSet.Tables.Add(myTable) myGrid.SetDataBinding(namesDataSet, "NamesTable") AddData() AddTableStyle() Catch exc As System.Exception Console.WriteLine(exc.ToString) End Try End Sub Private Sub AddTableStyle() ' Map a new TableStyle to the DataTable. Then ' add DataGridColumnStyle objects to the collection ' of column styles with appropriate mappings. Dim dgt As DataGridTableStyle = New DataGridTableStyle dgt.MappingName = "NamesTable" Dim dgtbc As DataGridTextBoxColumn = _ New DataGridTextBoxColumn dgtbc.MappingName = "Name" dgtbc.HeaderText = "Name" dgt.GridColumnStyles.Add(dgtbc) dgtbc = New DataGridTextBoxColumn dgtbc.MappingName = "id" dgtbc.HeaderText = "id" dgt.GridColumnStyles.Add(dgtbc) Dim db As DataGridBoolColumnInherit = _ New DataGridBoolColumnInherit db.HeaderText = "less than 1000 = blue" db.Width = 150 db.MappingName = "calculatedField" dgt.GridColumnStyles.Add(db) myGrid.TableStyles.Add(dgt) ' This expression instructs the grid to change ' the color of the inherited DataGridBoolColumn ' according to the value of the id field. If it's ' less than 1000, the row is blue. Otherwise, ' the color is yellow. 'db.Expression = "id < 1000" End Sub Private Sub AddData() ' Add data with varying numbers for the id field. ' If the number is over 1000,

                C Offline
                C Offline
                Chrissy Callen
                wrote on last edited by
                #7

                that was quick :) im very impressed..thank you..will give that a go right now. Chrissy Callen

                1 Reply Last reply
                0
                • C Chrissy Callen

                  i have 4 columns in my datagrid which at the moment are displaying "yes" or "no". My boss wants these existing columns to display checkboxes instead. Ive tried really hard to find a solution to this problem but so far nothing has worked :( Any help would be greatly appreciated - getting desparate now!! Chrissy Callen

                  R Offline
                  R Offline
                  roshanak
                  wrote on last edited by
                  #8

                  Hi I have the same problem if you find the answer please tell me

                  C 1 Reply Last reply
                  0
                  • R roshanak

                    Hi I have the same problem if you find the answer please tell me

                    C Offline
                    C Offline
                    Chrissy Callen
                    wrote on last edited by
                    #9

                    well at the moment i'm looking into creating a listview instead on the advice of one of my colleagues :) Chrissy Callen

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups