-
Using GridEX in SelfReferencing HierarchicalMode [Janus GridEX WinForms Control v3.5 for .NET]
This tutorial is intended to show you how to use GridEX to display a self-referencing table in a tree fashion.
1) Create a new Visual Basic or C# project using "Windows Application" Template.
2) Add a new Data Source to the application:
Click in the "Add New Data Source..." menu under "Data" and follow the wizard to add "Messages" table from GridEXTutorialsData.mdb database.
In the tutorial we used Provider = "Microsoft Access Database File (OLE DB)" and Database file name = "C:\GridEXTutorialsData.mdb".
3) Build the project to be able to see GridEXTutorialsDataDataSet and MessagesTableAdapter as components in the Toolbox.
4) Drag from the tool box GridEXTutorialsDataDataSet component and MessagesTableAdapter component.
5) Drag a GridEX control from the toolbox into the Form designer.
6) Select GridEX control in the Form and set the following properties in the properties window:
DataSource = GridEXTutorialsDataDataSet1
DataMember = Messages
7) Right click in the GridEX control and select "Retrieve Structure" menu to create the columns matching those found in the DataSource.
8) Right click in the GridEX control and select "Designer" menu. In the Designer dialog, under RootTable node select Columns node and move the columns positions as you want using Move Up and Move Down toolbar buttons. In the sample, the following properties were changed in the columns:
Column
Property
New Value
MessageID
Visible
False
ParentMessageID
Visible
False
Subject
ColumnType
ImageAndText
ImageIndex
0*
*To be able to set ImageIndex, an ImageList was added to the form and assigned as the ImageList property of the GridEX control
** Selectable property is set as False in all columns.
9) In the designer, now select SelfReferencingSettings node under RootTable and click in the "SelfReferencing Wizard" button that appears at the right side. The settings we used in the wizard are:
HierarchicalMode
SelfReferencing
To present Tree-like hierarchies in a GridEX control.
ParentDataMember
MessageID
The field that identifies the parent column in the self-referencing relation.
ChildDataMember
ParentMessageID
The field that identifies the child column in the self-referencing relation.
ExpandColumn
Subject
The column in the table where the expand glyph will be displayed.
10) In the Load event of the Form fill the "Messages" table in the dataset with the following code:
In VB .Net
MessagesTableAdapter1.Fill(GridEXTutorialsDataDataSet1.Messages)
In C#.Net
messagesTableAdapter1.Fill(gridEXTutorialsDataDataSet1.Messages);
11) To create a new MessageDataRow in the application, MessageDialog form was added to the project. The form consists of 3 EditBox controls that let the user enter the Subject, the name of the user creating the message and a Message body. This form is called by the method CreateMessage in Form1 as follows:
private void CreateMessage(object parentId,string subject)
{
MessageDialog message = new MessageDialog();
message.Subject = subject;
if (message.ShowDialog() == DialogResult.OK)
{
//Add a new message row to the dataset
GridEXTutorialsDataDataSet.MessagesRow newMessage;
newMessage = gridEXTutorialsDataDataSet1.Messages.NewMessagesRow();
newMessage.Subject = message.Subject;
if (message.From.Length == 0)
{
newMessage.From = "ANONIMOUS";
}
else
{
newMessage.From = message.From;
}
newMessage.Message = message.Message;
newMessage.Date = DateTime.Now;
if (parentId != null)
{
newMessage.ParentMessageID = (int)parentId;
}
//Add the row to the dataset and it will be displayed
//automatically in the grid.
gridEXTutorialsDataDataSet1.Messages.Rows.Add(newMessage);
//Select the new row in grid
gridEX1.MoveTo(gridEX1.GetRow(newMessage));
gridEX1.Focus();
}
}
12) Finally, add 3 buttons to the form:
btnNewMessage (Text = "New Message...")
In the Click event, a new message with no parent is created as follows:
CreateMessage(null, "New Message");
btnReplyMessage (Text = "Reply Message...")
In the Click event, a message with the selected row id as parent is created as follows:
if (gridEX1.CurrentRow != null &&
gridEX1.CurrentRow.RowType == RowType.Record)
{
CreateMessage(gridEX1.GetValue("MessageID"), "RE: " + gridEX1.GetValue("Subject"));
}
else
{
MessageBox.Show("Select the message to reply." , "",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
btnDelete (Text = "Delete Message")
In the Click event, the selected message is deleted as follows:
if (gridEX1.CurrentRow != null &&
gridEX1.CurrentRow.RowType == RowType.Record)
{
gridEX1.Delete();
}
else
{
MessageBox.Show("Select the message you want to delete", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
13) Other properties set in the GridEX control are:
AllowEdit = False
AllowDelete = True To be able to use GridEX.Delete method
HideSelection = Highlight To see the selected row even when grid is not focused.
14) Press F5 and run the project.
Source Of Information : Janus v3.5 Help Files for VS 2008
Subscribe to:
Post Comments (Atom)
0 comments: