Tuesday, January 27, 2015

Hide DataGridview Checkbox in Specific Cells

I just wanna share this piece of code which will help someone out there that is having a problem hiding a specific checkbox cell in a datagridview.

This code may not be the best solution out there. But, this piece of code help me achieve my requirements.

With Datagridview1.Rows(rowIndex)
            .Cells(CheckBoxColName) = New DataGridViewTextBoxCell
            .Cells(CheckBoxColName).Value = String.Empty
 End With

Sample:



Let me know if you have the most best solution by commenting below.

Tuesday, January 6, 2015

Vb: Custom Column Header in Datagridview

I was browsing the web when I came across with this site at http://gauravsofts.blogspot.com/2010/02/print-multiple-layered-column-header-in.html. It has a good sample of codes about the multi-layered datagridview and after reading the post, I decided to make use of it.

I made some changes that fit to my requirements.

Sample Output


Codes: 



 Private Sub AddGridViews()  
     Dim subColumnHeader As String() = {"Fabric", "XS", "S", "M", "L", "XL"}  
     Dim dgView As DataGridView  
     FlowLayoutPanel1.SuspendLayout()  
     For i As Integer = 1 To 5  
       dgView = New DataGridView  
       With dgView  
         .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill  
         .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing  
         .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter  
         .Size = New Size(307, 170)  
         .ColumnHeadersHeight = 40  
         For Each col As String In subColumnHeader  
           .Columns.Add("col" & col, col)  
         Next  
         .Rows.Add(3)  
         .Tag = "Fabric " & i ' holds the header of the grid  
       End With  
       AddHandler dgView.CellPainting, AddressOf dataGridView1_CellPainting  
       AddHandler dgView.Paint, AddressOf dataGridView1_Paint  
       FlowLayoutPanel1.Controls.Add(dgView)  
     Next  
     FlowLayoutPanel1.ResumeLayout()  
   End Sub  
  Private Sub dataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)  
     If e.RowIndex = -1 AndAlso e.ColumnIndex > -1 Then  
       e.PaintBackground(e.CellBounds, False)  
       Dim r2 As Rectangle = e.CellBounds  
       r2.Y += e.CellBounds.Height / 2  
       r2.Height = e.CellBounds.Height / 2  
       e.PaintContent(r2)  
       e.Handled = True  
     End If  
   End Sub  
   Private Sub dataGridView1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)  
     ' Data for the "merged" header cells  
     Dim obj = TryCast(sender, DataGridView)  
     ' Get the column header cell bounds  
     Dim r1 As Rectangle = obj.GetCellDisplayRectangle(0, -1, True)  
     r1.X += 1  
     r1.Y += 1  
     r1.Width = r1.Width * 6 - 2  
     r1.Height = r1.Height / 2 - 2  
     e.Graphics.FillRectangle(Brushes.Beige, r1)  
     e.Graphics.DrawLine(Pens.BurlyWood, r1.X, r1.Bottom, r1.Right, r1.Bottom)  
     Using format As StringFormat = New StringFormat()  
       Using br As SolidBrush = New SolidBrush(obj.ColumnHeadersDefaultCellStyle.ForeColor)  
         format.Alignment = StringAlignment.Center  
         format.LineAlignment = StringAlignment.Center  
         e.Graphics.DrawString(obj.Tag.ToString, obj.ColumnHeadersDefaultCellStyle.Font, _  
                    br, r1, format)  
       End Using  
     End Using  
   End Sub  
  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
     AddGridViews()  
   End Sub  




Hope this will help someone out there.

Thanks to Gaurav Khanna.