using System;
using System.Windows.Forms;
using System.Collections.Generic;
namespace GrapeCity.Win.MultiRow.SampleCode
{
public class VirtualModeDemo : Form
{
private GcMultiRow gcMultiRow1 = new GcMultiRow();
private List<Student> userData = new List<Student>();
private Student uncommitNewStudent = null;
private Student restoreRow = null;
public VirtualModeDemo()
{
this.Text = "VirtualMode Demo";
// Add MultiRow to form
this.gcMultiRow1.Dock = DockStyle.Fill;
this.Controls.Add(this.gcMultiRow1);
this.InitializeTemplate();
this.InitializeData();
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
// Set virtual mode event, you can manage date by you self by CellValueNeeded,CellValuePushed etc. events.
gcMultiRow1.VirtualMode = true;
gcMultiRow1.CellValueNeeded += new EventHandler<CellValueEventArgs>(gcMultiRow1_CellValueNeeded);
gcMultiRow1.CellValuePushed += new EventHandler<CellValueEventArgs>(gcMultiRow1_CellValuePushed);
gcMultiRow1.CancelRowEdit += new EventHandler<QuestionEventArgs>(gcMultiRow1_CancelRowEdit);
gcMultiRow1.NewRowNeeded += new EventHandler<RowEventArgs>(gcMultiRow1_NewRowNeeded);
gcMultiRow1.RowEnter += new EventHandler<CellEventArgs>(gcMultiRow1_RowEnter);
gcMultiRow1.RowsAdded += new EventHandler<RowsAddedEventArgs>(gcMultiRow1_RowsAdded);
gcMultiRow1.RowsRemoved += new EventHandler<RowsRemovedEventArgs>(gcMultiRow1_RowsRemoved);
}
void gcMultiRow1_CellValuePushed(object sender, CellValueEventArgs e)
{
// When user edit value, MultiRow control will notify update your data by this event.
Student student = userData[e.RowIndex];
if (e.CellName == "Name")
{
student.Name = e.Value.ToString();
}
else
{
int value;
if (!int.TryParse(e.Value.ToString(), out value))
{
MessageBox.Show("Score should be a number");
return;
}
if (e.CellName == "Mathematics")
{
student.MathematicsScore = value;
}
else if (e.CellName == "Philosophy")
{
student.PhilosophyScore = value;
}
}
}
void gcMultiRow1_CellValueNeeded(object sender, CellValueEventArgs e)
{
Student student = null;
if (e.RowIndex == userData.Count)
{
student = uncommitNewStudent;
}
else
{
student = userData[e.RowIndex];
}
// When MultiRow control paint a cell, the control will ask the value of the specific cell by this event.
if (e.CellName == "Name")
{
e.Value = student.Name;
}
if (e.CellName == "Mathematics")
{
e.Value = student.MathematicsScore;
}
if (e.CellName == "Philosophy")
{
e.Value = student.PhilosophyScore;
}
if (e.CellName == "ID")
{
e.Value = student.ID;
}
}
void gcMultiRow1_NewRowNeeded(object sender, RowEventArgs e)
{
// Add new row when user move current cell to last new row.
uncommitNewStudent = new Student(userData.Count, null, 0, 0);
}
void gcMultiRow1_CancelRowEdit(object sender, QuestionEventArgs e)
{
int currentRowIndex = gcMultiRow1.CurrentCellPosition.RowIndex;
// Cancel uncommitted new row.
if (currentRowIndex >= 0 && currentRowIndex < gcMultiRow1.RowCount - 1)
{
// Cancel row level edit.
userData[currentRowIndex] = new Student(restoreRow.ID, restoreRow.Name, restoreRow.MathematicsScore, restoreRow.PhilosophyScore);
}
}
void gcMultiRow1_RowsRemoved(object sender, RowsRemovedEventArgs e)
{
// When user select rows and press Ctrl+Delete key, the rows romoved.
// Update user data source.
userData.RemoveRange(e.RowIndex, e.RowCount);
}
void gcMultiRow1_RowsAdded(object sender, RowsAddedEventArgs e)
{
// When user add rows, add uncommitted new row to user data source.
userData.Add(uncommitNewStudent);
}
void gcMultiRow1_RowEnter(object sender, CellEventArgs e)
{
if (e.RowIndex != gcMultiRow1.NewRowIndex)
{
// When row enter cache old value, if user press ESC key to cancel row level edit value, use this row restore.
restoreRow = new Student(userData[e.RowIndex].ID, userData[e.RowIndex].Name, userData[e.RowIndex].MathematicsScore, userData[e.RowIndex].PhilosophyScore);
}
}
private void InitializeTemplate()
{
// Create a template with 3 text box and a header cell.
Template template = Template.CreateGridTemplate(3);
// Initialize column header's caption
template.ColumnHeaders[0][0].Value = "Name";
template.ColumnHeaders[0][1].Value = "Mathematics";
template.ColumnHeaders[0][2].Value = "Philosophy";
template.ColumnHeaders[0][3].Value = "ID";
// Initialize cell's Name.
template.Row.Cells[0].Name = "Name";
template.Row.Cells[1].Name = "Mathematics";
template.Row.Cells[2].Name = "Philosophy";
template.Row.Cells[3].Name = "ID";
this.gcMultiRow1.Template = template;
}
#region Initial user data
public class Student
{
public Student(int id, string name, int mathematicsScore, int philosophyScore)
{
ID = id;
Name = name;
MathematicsScore = mathematicsScore;
PhilosophyScore = philosophyScore;
}
public int ID;
public string Name;
public int MathematicsScore;
public int PhilosophyScore;
}
private void InitializeData()
{
userData.Add(new Student(0, "Jim", 100, 100));
userData.Add(new Student(1, "Tom", 88, 92));
userData.Add(new Student(2, "Smith", 80, 66));
// 3 records and a new row.
this.gcMultiRow1.RowCount = 4;
}
#endregion
[STAThreadAttribute()]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new VirtualModeDemo());
}
}
}
Imports System
Imports System.Windows.Forms
Imports GrapeCity.Win.MultiRow
Public Class VirtualModeDemo
Inherits Form
Friend WithEvents gcMultiRow1 As New GcMultiRow()
Private userData As New List(Of Student)()
Private uncommitNewStudent As Student = Nothing
Private restoreRow As Student = Nothing
Public Sub New()
Me.Text = "VirtualMode Demo"
' Add MultiRow to form
Me.gcMultiRow1.Dock = DockStyle.Fill
Me.Controls.Add(Me.gcMultiRow1)
Me.InitializeTemplate()
Me.InitializeData()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
' Set virtual mode event, you can manage date by you self by CellValueNeeded,CellValuePushed etc. events.
gcMultiRow1.VirtualMode = True
End Sub
Private Sub gcMultiRow1_CellValuePushed(ByVal sender As Object, ByVal e As CellValueEventArgs) Handles gcMultiRow1.CellValuePushed
' When user edit value, MultiRow control will notify update your data by this event.
Dim student As Student = userData(e.RowIndex)
If e.CellName = "Name" Then
student.Name = e.Value.ToString()
Else
Dim value As Integer
If Not Integer.TryParse(e.Value.ToString(), value) Then
MessageBox.Show("Score should be a number")
Return
End If
If e.CellName = "Mathematics" Then
student.MathematicsScore = value
ElseIf e.CellName = "Philosophy" Then
student.PhilosophyScore = value
End If
End If
End Sub
Private Sub gcMultiRow1_CellValueNeeded(ByVal sender As Object, ByVal e As CellValueEventArgs) Handles gcMultiRow1.CellValueNeeded
Dim student As Student = Nothing
If e.RowIndex = userData.Count Then
student = uncommitNewStudent
Else
student = userData(e.RowIndex)
End If
' When MultiRow control paint a cell, the control will ask the value of the specific cell by this event.
If e.CellName = "Name" Then
e.Value = student.Name
End If
If e.CellName = "Mathematics" Then
e.Value = student.MathematicsScore
End If
If e.CellName = "Philosophy" Then
e.Value = student.PhilosophyScore
End If
If e.CellName = "ID" Then
e.Value = student.ID
End If
End Sub
Private Sub gcMultiRow1_NewRowNeeded(ByVal sender As Object, ByVal e As RowEventArgs) Handles gcMultiRow1.NewRowNeeded
' Add new row when user move current cell to last new row.
uncommitNewStudent = New Student(userData.Count, Nothing, 0, 0)
End Sub
Private Sub gcMultiRow1_CancelRowEdit(ByVal sender As Object, ByVal e As QuestionEventArgs) Handles gcMultiRow1.CancelRowEdit
Dim currentRowIndex As Integer = gcMultiRow1.CurrentCellPosition.RowIndex
' Cancel uncommitted new row.
If currentRowIndex >= 0 AndAlso currentRowIndex < gcMultiRow1.RowCount - 1 Then
' Cancel row level edit.
userData(currentRowIndex) = New Student(restoreRow.ID, restoreRow.Name, restoreRow.MathematicsScore, restoreRow.PhilosophyScore)
End If
End Sub
Private Sub gcMultiRow1_RowsRemoved(ByVal sender As Object, ByVal e As RowsRemovedEventArgs) Handles gcMultiRow1.RowsRemoved
' When user select rows and press Ctrl+Delete key, the rows romoved.
' Update user data source.
userData.RemoveRange(e.RowIndex, e.RowCount)
End Sub
Private Sub gcMultiRow1_RowsAdded(ByVal sender As Object, ByVal e As RowsAddedEventArgs) Handles gcMultiRow1.RowsAdded
' When user add rows, add uncommitted new row to user data source.
If (Not uncommitNewStudent Is Nothing) Then
userData.Add(uncommitNewStudent)
End If
End Sub
Private Sub gcMultiRow1_RowEnter(ByVal sender As Object, ByVal e As CellEventArgs) Handles gcMultiRow1.RowEnter
If e.RowIndex <> gcMultiRow1.NewRowIndex Then
' When row enter cache old value, if user press ESC key to cancel row level edit value, use this row restore.
restoreRow = New Student(userData(e.RowIndex).ID, userData(e.RowIndex).Name, userData(e.RowIndex).MathematicsScore, userData(e.RowIndex).PhilosophyScore)
End If
End Sub
Private Sub InitializeTemplate()
' Create a template with 3 text box and a header cell.
Dim template1 As Template = Template.CreateGridTemplate(3)
' Initialize column header's caption
template1.ColumnHeaders(0)(0).Value = "Name"
template1.ColumnHeaders(0)(1).Value = "Mathematics"
template1.ColumnHeaders(0)(2).Value = "Philosophy"
template1.ColumnHeaders(0)(3).Value = "ID"
' Initialize cell's Name.
template1.Row.Cells(0).Name = "Name"
template1.Row.Cells(1).Name = "Mathematics"
template1.Row.Cells(2).Name = "Philosophy"
template1.Row.Cells(3).Name = "ID"
Me.gcMultiRow1.Template = template1
End Sub
#Region "Initial user data"
Public Class Student
Public Sub New(ByVal id1 As Integer, ByVal name2 As String, ByVal mathematicsScore3 As Integer, ByVal philosophyScore4 As Integer)
ID = id1
Name = name2
MathematicsScore = mathematicsScore3
PhilosophyScore = philosophyScore4
End Sub
Public ID As Integer
Public Name As String
Public MathematicsScore As Integer
Public PhilosophyScore As Integer
End Class
Private Sub InitializeData()
userData.Add(New Student(0, "Jim", 100, 100))
userData.Add(New Student(1, "Tom", 88, 92))
userData.Add(New Student(2, "Smith", 80, 66))
' 3 records and a new row.
Me.gcMultiRow1.RowCount = 4
End Sub
#End Region
<STAThreadAttribute()> _
Public Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New VirtualModeDemo())
End Sub
End Class