.NET Programming With Me

VB: MultipleLayeredColumnHeader

This following code demonstrates how to display multiple layer column headers on the DataGridView control:


'Enable resizing on the column headers
            Me.DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing

'Adjust the height for the column headers
            Me.DataGridView1.ColumnHeadersHeight = Me.DataGridView1.ColumnHeadersHeight * 2

'Adjust the text alignment on the column headers to make the text display at the center of the bottom
            Me.DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter



'Handle the DataGridView.CellPainting event to draw text for each header cell

Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
     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

'Handle the DataGridView.Paint event to draw "merged" header cells

Private Sub DataGridView1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint
'Data for merged Header cells
Dim feeTitles As String() = {"Admission", "MonthlyFee", "Id Card"}
For j As Integer = 0 To Me.DataGridView1.ColumnCount - 1 Step 2
'Get the column header cell bounds
Dim r1 As Rectangle = Me.DataGridView1.GetCellDisplayRectangle(j, -1, True)
r1.X += 1
r1.Y += 1
r1.Width = r1.Width * 2 - 2
r1.Height = r1.Height / 2 - 2

Using br As SolidBrush = New SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.BackColor)
e.Graphics.FillRectangle(br, r1)
End Using

Using p As Pen = New Pen(SystemColors.InactiveBorder)
e.Graphics.DrawLine(p, r1.X, r1.Bottom, r1.Right, r1.Bottom)
End Using

Using format As StringFormat = New StringFormat()
Using br As SolidBrush = New SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor)
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(feeTitles(j / 2), Me.DataGridView1.ColumnHeadersDefaultCellStyle.Font, Brushes.Brown, r1, format)
End Using
End Using
Next
End Sub


C# - DoubleClickButton

I needed a double-click event for the button, which is never possible.
In order to perform double-click event on the button, you should create a new control should be created as:


namespace DoubleClickButton
{
    // Derive a button with extended functionality
    // from the Button class.
    public class DoubleClickButton: System.Windows.Forms.Button
    {
        // Note that the DoubleClickTime property gets 
        // the maximum number of milliseconds allowed between 
        // mouse clicks for a double-click to be valid.
        int previousClick = SystemInformation.DoubleClickTime;

        public new event EventHandler DoubleClick;

        protected override void OnClick(EventArgs e)
        {
            int now = System.Environment.TickCount;

            // A double-click is detected if the the time elapsed
            // since the last click is within DoubleClickTime.
            if (now - previousClick <= SystemInformation.DoubleClickTime)
            {
                // Raise the DoubleClick event.
                if (DoubleClick != null)
                    DoubleClick(this, EventArgs.Empty);
            }

            // Set previousClick to now so that 
            // subsequent double-clicks can be detected.
            previousClick = now;

            // Allow the base class to raise the regular Click event.
            base.OnClick(e);
        }

        // Event handling code for the DoubleClick event.
        protected new virtual void OnDoubleClick(EventArgs e)
        {
            if (this.DoubleClick != null)
                this.DoubleClick(this, e);
        }
    }
}


Reference: MSDN

SQL Server - alter column - adding default constraint

The following sql syntax is used for adding a default value constraint to a column in the table already created.

alter table <TableName>
  add constraint <df_ConstraintName>
  default <defaultValue> for <columnName>

Example:
alter table employee
  add constraint df_empSalary
  default 0 for empSalary