using
grid_iterate_grid.Annotations;
using
System;
using
System.ComponentModel;
using
System.Text;
using
System.Windows.Forms;
using
Telerik.WinControls.UI;
namespace
grid_iterate_grid
{
public
partial
class
Form1 : Form
{
private
RadGridView gridView =
new
RadGridView();
private
BindingList<TestClass> dataSource =
new
BindingList<TestClass>();
public
Form1()
{
InitializeComponent();
var parseGrid =
new
RadButton();
parseGrid.Text =
"Parse!"
;
parseGrid.Dock = DockStyle.Bottom;
parseGrid.Click += parseGrid_Click;
this
.Controls.Add(parseGrid);
var addProgramaticallyButton =
new
RadButton();
addProgramaticallyButton.Text =
"Add row programatically!"
;
addProgramaticallyButton.Dock = DockStyle.Bottom;
addProgramaticallyButton.Click += addProgramaticallyButton_Click;
this
.Controls.Add(addProgramaticallyButton);
//gridView.Columns.Add(new GridViewTextBoxColumn());
//gridView.Columns.Add(new GridViewDecimalColumn());
//gridView.Columns.Add(new GridViewDateTimeColumn());
gridView.Dock = DockStyle.Fill;
gridView.DataSource = dataSource;
this
.Controls.Add(gridView);
}
private
void
addProgramaticallyButton_Click(
object
sender, EventArgs e)
{
dataSource.Add(
new
TestClass { Id = 1000, Date = DateTime.Now, Name =
"Programatically added!"
});
}
private
void
parseGrid_Click(
object
sender, EventArgs e)
{
var stringBuilder =
new
StringBuilder();
// old version
//foreach (var row in gridView.Rows)
//{
// foreach (GridViewCellInfo cell in row.Cells)
// {
// var value = cell.Value;
// stringBuilder.Append(value != null ? value.ToString() : "N/A");
// stringBuilder.Append(" | ");
// }
// stringBuilder.AppendLine();
//}
foreach
(var testClass
in
dataSource)
{
stringBuilder.AppendLine(
string
.Format(
"Id: {0}, Name: {1}, Date: {2}"
, testClass.Id, testClass.Name,
testClass.Date));
}
MessageBox.Show(stringBuilder.ToString());
}
public
class
TestClass : INotifyPropertyChanged
{
private
DateTime date;
private
string
name;
private
int
id;
public
int
Id
{
get
{
return
id; }
set
{
if
(value == id)
return
;
id = value;
OnPropertyChanged(
"Id"
);
}
}
public
string
Name
{
get
{
return
name; }
set
{
if
(value == name)
return
;
name = value;
OnPropertyChanged(
"Name"
);
}
}
public
DateTime Date
{
get
{
return
date; }
set
{
if
(value.Equals(date))
return
;
date = value;
OnPropertyChanged(
"Date"
);
}
}
public
event
PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected
virtual
void
OnPropertyChanged(
string
propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if
(handler !=
null
) handler(
this
,
new
PropertyChangedEventArgs(propertyName));
}
}
}
}
Hope this helps, if you have any more questions please don't hesitate to ask.
Best Regards.