Hi,
Actualy i have made a control like Weekly Outlook , i have made somehow every aspect of this but the issue that i am facing is i have to make certain cells grouped or merged , i have control this in a DATAGRID control , made runtime datagrid and give column a binding with my DATABASE , i successfully did that ,i want to mentioned my whole XAML.cs code ,please see the below code
private void BuildDataGrid()
{
// Create a new instance of the DataGrid class
// Configure some properties and data source
gdEmployees.AutoGenerateColumns = false;
gdEmployees.CanUserSortColumns = false;
//gdEmployees.ItemsSource = EmployeeManager.GetEmployeeList();
gdEmployees.IsReadOnly = false;
gdEmployees.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
gdEmployees.SetValue(Grid.RowProperty, 2);
// Add the Grid to the Page
this.LayoutRoot.Children.Add(gdEmployees);
// Add the Time custom column
DataGridTextColumn Time = new DataGridTextColumn();
Time.Header = System.DateTime.Now; //"Full Name";
Time.Binding = new Binding("Time");
Time.IsReadOnly = true;
gdEmployees.Columns.Add(Time);
// Add the Sunday column
DataGridTextColumn Sunday = new DataGridTextColumn();
Sunday.Header = System.DateTime.Now.AddDays(-2).ToString();
Sunday.Binding = new Binding("Sunday");
Sunday.IsReadOnly = true;
gdEmployees.Columns.Add(Sunday);
// Add the Monday column
DataGridTextColumn Monday = new DataGridTextColumn();
Monday.Header = System.DateTime.Now.AddDays(-1).ToString();
Monday.Binding = new Binding("Monday");
Monday.IsReadOnly = true;
gdEmployees.Columns.Add(Monday);
// Add the Tuesday column
DataGridTextColumn Tuesday = new DataGridTextColumn();
Tuesday.Header = System.DateTime.Now;
Tuesday.Binding = new Binding("Tuesday");
Tuesday.IsReadOnly = true;
gdEmployees.Columns.Add(Tuesday);
// Add the Wednesday column
DataGridTextColumn Wednesday = new DataGridTextColumn();
Wednesday.Header = System.DateTime.Now.AddDays(1).ToString();
Wednesday.Binding = new Binding("Wednesday");
Wednesday.IsReadOnly = true;
gdEmployees.Columns.Add(Wednesday);
// Add the Thursday column
DataGridTextColumn Thursday = new DataGridTextColumn();
Thursday.Header = System.DateTime.Now.AddDays(2).ToString();
Thursday.Binding = new Binding("Thursday");
Thursday.IsReadOnly = true;
gdEmployees.Columns.Add(Thursday);
// Add the Friday column
DataGridTextColumn Friday = new DataGridTextColumn();
Friday.Header = System.DateTime.Now.AddDays(3).ToString();
Friday.Binding = new Binding("Friday");
Friday.IsReadOnly = true;
gdEmployees.Columns.Add(Friday);
// Add the Saturday column
DataGridTextColumn Saturday = new DataGridTextColumn();
Saturday.Header = System.DateTime.Now.AddDays(4).ToString();
Saturday.Binding = new Binding("Saturday");
Saturday.IsReadOnly = true;
gdEmployees.Columns.Add(Saturday);
}
The binding for the column entries handled in another function ,in which i have looped through my all DB rows using Foreach loop , and add that row by row in a LIST (generic type) and give that LIST to source to DATAGRID , i have made 15 minute distribution with the Weekly representation ,if a person have appointement of 45 mins then it give a view like it distributed into 3 cells separatly,i want to make these 3 cells merge together,i think you understand my point of view,please help me out to resolve this issue
Loading
Master BillaPosted Sep 2, 2009, 5:01 AM
Yes, here is cell merge and code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing.Drawing2D;
public class HMergedCell : DataGridViewTextBoxCell
{
private int m_nLeftColumn = 0;
private int m_nRightColumn = 0;
///
/// Column Index of the left-most cell to be merged.
/// This cell controls the merged text.
///
public int LeftColumn {
get { return m_nLeftColumn; }
set { m_nLeftColumn = value; }
}
///
/// Column Index of the right-most cell to be merged
///
public int RightColumn {
get { return m_nRightColumn; }
set { m_nRightColumn = value; }
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
try {
int mergeindex = ColumnIndex - m_nLeftColumn;
int i = 0;
int nWidth = 0;
int nWidthLeft = 0;
string strText = null;
Pen pen = new Pen(Brushes.Black);
// Draw the background
graphics.FillRectangle(new SolidBrush(SystemColors.Control), cellBounds);
// Draw the separator for rows
graphics.DrawLine(new Pen(new SolidBrush(SystemColors.ControlDark)), cellBounds.Left, cellBounds.Bottom - 1, cellBounds.Right, cellBounds.Bottom - 1);
// Draw the right vertical line for the cell
if (ColumnIndex == m_nRightColumn) {
graphics.DrawLine(new Pen(new SolidBrush(SystemColors.ControlDark)), cellBounds.Right - 1, cellBounds.Top, cellBounds.Right - 1, cellBounds.Bottom);
}
// Draw the text
RectangleF rectDest = RectangleF.Empty;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.Trimming = StringTrimming.EllipsisCharacter;
// Determine the total width of the merged cell
nWidth = 0;
for (i = m_nLeftColumn; i <= m_nRightColumn; i++) {
nWidth += this.OwningRow.Cells(i).Size.Width;
}
// Determine the width before the current cell.
nWidthLeft = 0;
for (i = m_nLeftColumn; i <= ColumnIndex - 1; i++) {
nWidthLeft += this.OwningRow.Cells(i).Size.Width;
}
// Retrieve the text to be displayed
strText = this.OwningRow.Cells(m_nLeftColumn).Value.ToString();
rectDest = new RectangleF(cellBounds.Left - nWidthLeft, cellBounds.Top, nWidth, cellBounds.Height);
graphics.DrawString(strText, new Font("Arial", 10, FontStyle.Regular), Brushes.Black, rectDest, sf);
}
catch (Exception ex) {
Trace.WriteLine(ex.ToString());
}
}
}
// class
Use code
Private Sub BuildGridViewHeader()
If baseGridView Is Nothing Then
baseGridView = New DataGridView()
End If
If listOfAQLLevelColumnHeader Is Nothing Then
CYMessageBox.Show("Param collection not configured in the Database")
Return
End If
If listOfSampleSizeRow Is Nothing Then
CYMessageBox.Show("Param collection not configured in the Database")
Return
End If
DirectCast((baseGridView), System.ComponentModel.ISupportInitialize).BeginInit()
Dim columnCount As Integer = 0
columnCount = listOfAQLLevelColumnHeader.Count * 2 + 1
Dim rowCount As Integer = listOfSampleSizeRow.Count
Dim pColumn As DataGridViewColumn
Dim i As Integer, j As Integer
Dim strTemp As [String]
Dim pCell As HMergedCell
Dim nOffset As Integer
'add front colum to gridiview
strTemp = "SampleSize"
baseGridView.Columns.Add("SampleSize", "Sample Size")
pColumn = baseGridView.Columns(strTemp)
pColumn.SortMode = DataGridViewColumnSortMode.NotSortable
pColumn.ReadOnly = True
pColumn.HeaderCell.Style.ForeColor = System.Drawing.Color.Black
pColumn.Width = 80
For Each processpara As ProcessParam In listOfAQLLevelColumnHeader
For index As Integer = 0 To 1
strTemp = processpara.Name
baseGridView.Columns.Add(strTemp, strTemp)
pColumn = baseGridView.Columns(strTemp)
pColumn.SortMode = DataGridViewColumnSortMode.NotSortable
pColumn.Width = 80
Next
Next
baseGridView.Rows.Add(rowCount + 2)
nOffset = 1
For i = 0 To listOfAQLLevelColumnHeader.Count - 1
For j = nOffset To nOffset + 1
baseGridView.Rows(0).Cells(j) = New HMergedCell()
pCell = DirectCast(baseGridView.Rows(0).Cells(j), HMergedCell)
pCell.LeftColumn = nOffset
pCell.RightColumn = nOffset + 1
Next
nOffset += 2
Next
For i = 1 To rowCount
baseGridView.Rows(i + 1).Cells(0).Value = listOfSampleSizeRow(i - 1).Name
Next
Dim baseindex As Integer = 0
For i = 1 To baseGridView.Rows(0).Cells.Count - 1
If i = 1 Then
baseGridView.Rows(0).Cells(i).Value = listOfAQLLevelColumnHeader(baseindex).Name
Else
baseGridView.Rows(0).Cells(i).Value = listOfAQLLevelColumnHeader(baseindex).Name
End If
i = i + 1
baseindex = baseindex + 1
Next
For i = 1 To baseGridView.Rows(1).Cells.Count - 1
If i Mod 2 = 1 Then
baseGridView.Rows(1).Cells(i).Value = "Accept"
Else
baseGridView.Rows(1).Cells(i).Value = "Reject"
End If
Next
baseGridView.Rows(0).Cells(0).Value = "Sample Size\AQL Level"
'For i = 2 To rowCount + 1
' For j = 1 To columnCount - 1
' baseGridView.Rows(i).Cells(j).Value = ("{" & i.ToString() & ",") + j.ToString() & "}"
' Next
'Next
baseGridView.RowsDefaultCellStyle.BackColor = System.Drawing.Color.LightSteelBlue
baseGridView.ColumnHeadersVisible = False
baseGridView.RowHeadersVisible = False
baseGridView.Refresh()
baseGridView.AllowUserToAddRows = False
baseGridView.AllowUserToDeleteRows = False
baseGridView.AllowUserToResizeColumns = False
baseGridView.AllowUserToOrderColumns = False
baseGridView.AllowUserToResizeRows = False
pColumn = Nothing
DirectCast((baseGridView), System.ComponentModel.ISupportInitialize).EndInit()
windowsFormsHostMain.Child = baseGridView
End Sub
Note:Here i have given my project sample code to how to use the MergeCell.
thank youRoei BarPosted Sep 2, 2009, 5:00 AM
here is a snippet for that metter
onPreRender call this Method, it should work