using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.Primitives;
using Telerik.WinControls.UI;
namespace GridCellEnabled
{
public partial class Form1 : Form
{
private GridViewTextBoxColumn _yearColumn = null;
private GridViewTextBoxColumn _valueColumn = null;
private GridViewCommandColumn _insertRowColumn = null;
private GridViewCommandColumn _deleteRowColumn = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SetupGrid();
}
private void SetupGrid()
{
// Add Columns
{
// Year Column
{
_yearColumn = new GridViewTextBoxColumn();
_yearColumn.Name = "Year";
_yearColumn.HeaderText = "Year";
_grid.Columns.Add(_yearColumn);
}
// Value Column
{
_valueColumn = new GridViewTextBoxColumn();
_valueColumn.Name = "Value";
_valueColumn.HeaderText = "Value";
_grid.Columns.Add(_valueColumn);
}
// Insert Row Column
{
_insertRowColumn = new GridViewCommandColumn();
_insertRowColumn.Name = "InsertRow";
_insertRowColumn.HeaderText = string.Empty;
_insertRowColumn.UseDefaultText = true;
_insertRowColumn.DefaultText = "+";
_insertRowColumn.MinWidth = 20;
_insertRowColumn.TextAlignment = ContentAlignment.MiddleCenter;
_grid.Columns.Add(_insertRowColumn);
}
// Delete Row Column
{
_deleteRowColumn = new GridViewCommandColumn();
_deleteRowColumn.Name = "DeleteRow";
_deleteRowColumn.HeaderText = string.Empty;
_deleteRowColumn.UseDefaultText = true;
_deleteRowColumn.DefaultText = "-";
_deleteRowColumn.MinWidth = 20;
_deleteRowColumn.TextAlignment = ContentAlignment.MiddleCenter;
_grid.Columns.Add(_deleteRowColumn);
}
}
// Add Rows
{
for (int i = 1; i <= 5; i++)
{
GridViewRowInfo row = _grid.Rows.AddNew();
row.Cells[_yearColumn.Name].Value = i;
row.Cells[_valueColumn.Name].Value = i * 10;
}
}
_grid.BestFitColumns();
}
private void _grid_CommandCellClick(object sender, EventArgs e)
{
GridViewCellEventArgs args = e as GridViewCellEventArgs;
if (args.Column == _insertRowColumn)
{
InsertRow(args.RowIndex);
}
else if (args.Column == _deleteRowColumn)
{
DeleteRow(args.RowIndex);
}
}
private void InsertRow(int aRow)
{
GridViewDataRowInfo rowInfo = new GridViewDataRowInfo(_grid.MasterView);
int startYear = Convert.ToInt32(_grid.Rows[aRow].Cells["Year"].Value) + 1;
rowInfo.Cells[_yearColumn.Name].Value = startYear;
rowInfo.Cells[_valueColumn.Name].Value = startYear * 10;
_grid.Rows.Insert(aRow + 1, rowInfo);
}
private void DeleteRow(int aRow)
{
_grid.Rows.RemoveAt(aRow);
}
private void _grid_ViewCellFormatting(object sender, CellFormattingEventArgs e)
{
if (e.CellElement is GridCommandCellElement && e.RowIndex >= 0)
{
if (e.Column == _insertRowColumn)
{
if (e.RowIndex + 1 < _grid.RowCount)
{
int startYear = Convert.ToInt32(_grid.Rows[e.RowIndex].Cells[_yearColumn.Name].Value);
int nextYear = Convert.ToInt32(_grid.Rows[e.RowIndex + 1].Cells[_yearColumn.Name].Value);
if ((startYear + 1) == nextYear)
{
e.CellElement.Enabled = false;
}
else
{
e.CellElement.Enabled = true;
}
}
else
{
e.CellElement.Enabled = true;
}
}
else if (e.Column == _deleteRowColumn)
{
if (e.RowIndex == 0)
{
e.CellElement.Enabled = false;
}
else
{
e.CellElement.Enabled = true;
}
}
}
}
}
}
namespace GridCellEnabled
{
partial class Form1
{
/// <
summary
>
/// Required designer variable.
/// </
summary
>
private System.ComponentModel.IContainer components = null;
/// <
summary
>
/// Clean up any resources being used.
/// </
summary
>
/// <
param
name
=
"disposing"
>true if managed resources should be disposed; otherwise, false.</
param
>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <
summary
>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </
summary
>
private void InitializeComponent()
{
this._grid = new Telerik.WinControls.UI.RadGridView();
((System.ComponentModel.ISupportInitialize)(this._grid)).BeginInit();
this.SuspendLayout();
//
// _grid
//
this._grid.Location = new System.Drawing.Point(12, 12);
this._grid.Name = "_grid";
this._grid.Size = new System.Drawing.Size(260, 238);
this._grid.TabIndex = 0;
this._grid.ViewCellFormatting += new Telerik.WinControls.UI.CellFormattingEventHandler(this._grid_ViewCellFormatting);
this._grid.CommandCellClick += new Telerik.WinControls.UI.CommandCellClickEventHandler(this._grid_CommandCellClick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this._grid);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this._grid)).EndInit();
this.ResumeLayout(false);
}
#endregion
private Telerik.WinControls.UI.RadGridView _grid;
}
}
VB.NET
.NET 3.5
RadControls for WinForms Q1 2009 SP1
MS Visual Studio 2008
I have been able to do just about everything else I need to do in the Treeview control, except capturing click events in my form. The tree is generated from SQL tables and otherwise appears fine. Ultimately I am hoping to wire the tree nodes to files in the filesystem.
Following is the code to capture the DoubleClick event (copied from documentation):
Private
Sub RadTreeView1_DoubleClick(ByVal sender As Object, ByVal e As EventArgs)
Dim args As MouseEventArgs = TryCast(e, MouseEventArgs)
Dim clickedNode As RadTreeNode = RadTreeView1.GetNodeAt(args.X, args.Y)
If clickedNode <> Nothing Then
MessageBox.Show("Node Text: " + clickedNode.Text + " Node Value: " + clickedNode.Tag)
End If
End Sub
The only other block of code in my form is my form load which loads the tree with data from the database.
I have 2 questions/problems:
1. Visual Studio is complaining about the IF statement: "Operator '<>' is not defined for types 'Telerik.WinControls.UI.RadTreeNode' and 'Telerik.WinControls.UI.RadTreeNode'. What does this mean?
2. It appears the DoubleClick event isn't being captured since nothing happens when I double click a node. Is the IF statement error related to this? What am I missing? For my testing I simply commented out the 'If...' and 'End If' lines.
Thanks in advance for any guidance you can provide.
Matt