| Objectivo |
| Marcar la accion realizada en las filas del DataGridView. |
|
| Requisito: |
|
| Para realizar esta practica : |
|
|
|
|
|
|
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace WindowsCS.Formularios
{
public partial class GridMarkedActionForm : Form
{
DataTable table = new DataTable("TB1");
public GridMarkedActionForm()
{
InitializeComponent();
DoColumn("ID", "System.Int32", true, true);
DoColumn("NameAccion", "System.String", false, false);
DoColumn("Image", "System.Byte[]", false, false);
}
#region Metodos de ayuda
private void DoColumn(string name, string type, bool unique, bool isprimary)
{
DataColumn column = new DataColumn(name);
column.DataType = Type.GetType(type);
column.Unique = unique;
table.Columns.Add(column);
if (isprimary)
{
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = table.Columns[name];
table.PrimaryKey = PrimaryKeyColumns;
}
}
public byte[] ConvertImageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
public Image ConvertByteArrayToImage(int id)
{
MemoryStream ms = new MemoryStream((byte[])table.Rows.Find(id).ItemArray[2]);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
public Image ConvertByteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
#endregion Metodos de ayuda
#region EventHandler
private void Form4_Load(object sender, EventArgs e)
{
// TODO: esta línea de código carga datos en la tabla 'testDataSet.T1' Puede moverla o quitarla según sea necesario.
this.t1TableAdapter.Fill(this.testDataSet.T1);
foreach (DataGridViewRow row in t1DataGridView.Rows)
{
row.Cells[0].Value = null;
}
}
private void t1BindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in t1DataGridView.Rows)
{
if (table.Rows.Contains(row.Cells[1].Value))
{
if ((table.Rows.Find((int)row.Cells[1].Value).ItemArray[1]).ToString() == "Delete")
{
t1BindingSource.RemoveCurrent();
}
}
}
this.Validate();
this.t1BindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.testDataSet);
foreach (DataGridViewRow row in t1DataGridView.Rows)
{
row.Cells[0].Value = null;
}
table.Clear();
}
private void t1BindingSource_ListChanged(object sender, ListChangedEventArgs e)
{
DataRow row = table.NewRow();
switch (e.ListChangedType.ToString())
{
case "ItemChanged":
row["ID"] = (int)t1DataGridView.CurrentRow.Cells[1].Value;
row["NameAccion"] = "Edit";
row["Image"] = ConvertImageToByteArray((System.Drawing.Image)Resource1.Edit);
table.Rows.Add(row);
t1DataGridView.CurrentRow.Cells[0].Value = (System.Drawing.Image)Resource1.Edit;
break;
case "ItemAdded":
row["ID"] = (int)t1DataGridView.CurrentRow.Cells[1].Value;
row["NameAccion"] = "Add";
row["Image"] = ConvertImageToByteArray((System.Drawing.Image)Resource1.Add);
table.Rows.Add(row);
t1DataGridView.Rows[e.NewIndex].Cells[0].Value = (System.Drawing.Image)Resource1.Add;
break;
// movido al evento clic del boton eliminar, con el objectivo de eliminar esta
// caracteristica del bindingsource e implementarlo de forma manual.
//case "ItemDeleted":
//
// break;
/*
// no implementado
case "ItemMoved":
break;
*/
default:
break;
}
}
private void t1DataGridView_Sorted(object sender, EventArgs e)
{
if (table.Rows.Count > 0)
{
foreach (DataGridViewRow row in t1DataGridView.Rows)
{
if (table.Rows.Contains((Convert.ToInt32(row.Cells[1].Value))))
{
row.Cells[0].Value = ConvertByteArrayToImage((Convert.ToInt32(row.Cells[1].Value)));
}
}
}
}
private void bindingNavigatorDeleteItem_Click_1(object sender, EventArgs e)
{
DataRow row = table.NewRow();
row["ID"] = (int)t1DataGridView.CurrentRow.Cells[1].Value;
row["NameAccion"] = "Delete";
row["Image"] = ConvertImageToByteArray((System.Drawing.Image)Resource1.Delete);
table.Rows.Add(row);
t1DataGridView.CurrentRow.Cells[0].Value = (System.Drawing.Image)Resource1.Delete;
}
#endregion EventHandler
}
}
|
| Descripcion: |
private void DoColumn(string name, string type, bool unique, bool isprimary)
{
DataColumn column = new DataColumn(name);
column.DataType = Type.GetType(type);
column.Unique = unique;
table.Columns.Add(column);
if (isprimary)
{
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = table.Columns[name];
table.PrimaryKey = PrimaryKeyColumns;
}
}
public byte[] ConvertImageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
public Image ConvertByteArrayToImage(int id)
{
MemoryStream ms = new MemoryStream((byte[])table.Rows.Find(id).ItemArray[2]);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
private void Form4_Load(object sender, EventArgs e)
{
// TODO: esta línea de código carga datos en la tabla 'testDataSet.T1' Puede moverla o quitarla según sea necesario.
this.t1TableAdapter.Fill(this.testDataSet.T1);
foreach (DataGridViewRow row in t1DataGridView.Rows)
{
row.Cells[0].Value = null;
}
}
|
|
martes, 24 de enero de 2012
Marcar Accion Realizada en DataGridView
Suscribirse a:
Comentarios (Atom)