using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class Form2 : Form {
private readonly Dictionary<int, int> dicRandomValues;
private int cantidadFilas = 600;
public Form2()
{
InitializeComponent();
dicRandomValues
= new Dictionary
<int,
int>(); }
public object DataGridSource { get; set; }
private void button1_Click(object sender, EventArgs e)
{
button2.Enabled = false;
dataGridView1.DataSource = null;
// Creamos Datos ( este seria tu acceso a disco )
Dato
[] datos
= new Dato
[cantidadFilas
]; for (int i = 0; i < cantidadFilas; i++)
{
datos
[i
] = new Dato
(i
.ToString(), i
.ToString().Length); }
// Seleccino los que tienen largo par ( Simple tratamiento con los datos)
var myDataSource = from d in datos where d.Contenido.Last() != '1' select d;
cantidadFilas = myDataSource.Count();
// Seteamos el resultado de ordenar como DataSource
SetGridDataSource(myDataSource);
button1.Enabled = false;
button2.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
IEnumerable<Dato> query =
(((BindingSource)dataGridView1.DataSource).DataSource as IEnumerable<Dato>);
if (query == null)
{
MessageBox.Show("Primero crea las columnas");
return;
}
// Agregamos nueva columna
DataGridViewColumn column = CreateColumn();
dataGridView1.Columns.Add(column);
// Agregamos los datos de la nueva columna
// En este caso inventamos numeros con CreateRandomNumber()
query = from dato in query
select new Dato
(dato
.Contenido,
dato.Largo, CreateRandomNumber());
// Ordenamos segun la nueva columna
query = from dato in query.AsEnumerable()
orderby dato.OrderBy ascending
select dato;
// Seteamos el resultado de ordenar como DataSource
SetGridDataSource(query);
button2.Enabled = false;
}
private int CreateRandomNumber()
{
int randomNumber;
do
{
string ticksDate = DateTime.Now.Ticks.ToString();
int seek = Convert.ToInt32(ticksDate.Remove(0, ticksDate.Length/2));
randomNumber
= new Random
(seek
).Next(cantidadFilas
); } while (dicRandomValues.ContainsKey(randomNumber));
dicRandomValues.Add(randomNumber, 0);
return randomNumber;
}
private DataGridViewColumn CreateColumn()
{
return new DataGridViewTextBoxColumn
{
DataPropertyName = "OrderBy",
HeaderText = "OrderBy",
Name = "OorderBy",
};
}
private void SetGridDataSource(IEnumerable source)
{
dataGridView1.DataSource = null;
BindingSource binding
= new BindingSource
{DataSource
= source
}; dataGridView1.DataSource = binding;
}
/// <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.components = new System.ComponentModel.Container(); this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.Contenido = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Largo = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Contenido,
this.Largo});
this.dataGridView1.DataSource = this.bindingSource1;
this.dataGridView1.Location = new System.Drawing.Point(28,
24); this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(402,
235); this.dataGridView1.TabIndex = 0;
//
// Contenido
//
this.Contenido.DataPropertyName = "Contenido";
this.Contenido.HeaderText = "Contenido";
this.Contenido.Name = "Contenido";
//
// Largo
//
this.Largo.DataPropertyName = "Largo";
this.Largo.HeaderText = "Largo";
this.Largo.Name = "Largo";
//
// button1
//
this.button1.Location = new System.Drawing.Point(28,
303); this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(132,
40); this.button1.TabIndex = 1;
this.button1.Text = "Create";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click); //
// button2
//
this.button2.Location = new System.Drawing.Point(177,
303); this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(131,
42); this.button2.TabIndex = 2;
this.button2.Text = "AddColumn";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click); //
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F
); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(462,
381); this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGridView1);
this.Name = "Form2";
this.Text = "Form2";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit();
this.ResumeLayout(false);
}
#endregion
public DataGridView dataGridView1;
private Button button1;
private BindingSource bindingSource1;
private Button button2;
private DataGridViewTextBoxColumn Contenido;
private DataGridViewTextBoxColumn Largo;
}
}