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;
namespace DataBindingForm
{
public partial class MainForm : Form
{
//View of the datatable
DataView yugosOnlyView;
// collection of cars object
List<Car> listCars = null;
// inventory information.
DataTable inventoryTable = new DataTable();
public MainForm()
{
InitializeComponent();
// fill the list with some cars
listCars = new List<Car>
{
new Car { ID = 100, PetName = "Chucky", Make = "BMW", Color = "Green"},
new Car { ID = 101, PetName = "Tiny", Make = "Yugo", Color = "White"},
new Car { ID = 102, PetName = "Ami", Make = "Yugo", Color = "Tan"},
new Car { ID = 103, PetName = "Pain Inducer", Make = "Caravan", Color = "Pink"},
new Car { ID = 104, PetName = "Fred", Make = "BMW", Color = "Green"},
new Car { ID = 105, PetName = "Sidd", Make = "BMW", Color = "Black"},
new Car { ID = 106, PetName = "Mel", Make = "Firebird", Color = "Red"},
new Car { ID = 107, PetName = "Sarah", Make = "Colt", Color = "Black"},
};
CreateDataTable();
CreateDataView();
}
private void CreateDataTable()
{
//create table schema.
DataColumn carIDColumn = new DataColumn("ID", typeof(int));
DataColumn carMakeColumn = new DataColumn("Make", typeof(string));
DataColumn carColorColumn = new DataColumn("Color", typeof(string));
DataColumn carPetNameColumn = new DataColumn("PetName", typeof(string));
carPetNameColumn.Caption = "Pet Name";
inventoryTable.Columns.AddRange(new DataColumn[] { carIDColumn,
carMakeColumn, carColorColumn, carPetNameColumn});
// iterate over the list<t> to make rows.
foreach (Car c in listCars)
{
DataRow newRow = inventoryTable.NewRow();
newRow["ID"] = c.ID;
newRow["Make"] = c.Make;
newRow["Color"] = c.Color;
newRow["PetName"] = c.PetName;
inventoryTable.Rows.Add(newRow);
}
//bind the datatable to the carInventoryGridview.
carInventoryGridview.DataSource = inventoryTable;
}
private void CreateDataView()
{
//set the table that is used to construct this view.
yugosOnlyView = new DataView(inventoryTable);
//now configure the views using a filter.
[You must be registered and logged in to see this link.] = "ID = '(0)' ";
//bind to the new grid.
dataGridYugosView.DataSource = yugosOnlyView;
}
//remove this row from the datarowcollection
private void btnRemoveCar_Click(object sender, EventArgs e)
{
try
{
if (carInventoryGridview.SelectedRows.Count > 0)
{
// remove multiple rows
/*foreach (DataGridViewRow row in carInventoryGridview.SelectedRows)
{
carInventoryGridview.Rows.RemoveAt(row.Index);
}*/
// remove rows 1 at a time
/*carInventoryGridview.Rows.RemoveAt(carInventoryGridview.SelectedRows[0].Index);
*/
// remove row using a filter
// find the right row to delete
}
DataRow[] rowToDelete = inventoryTable.Select(string.Format
("ID = {0}", txtCarToRemove.Text));
rowToDelete[0].Delete();
inventoryTable.AcceptChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnDisplayMake_Click(object sender, EventArgs e)
{
//build a filter based on user input.
string filterStr = string.Format
("Make = '{0}' ", txtMakeToView.Text);
// find all rows matching the filter.
DataRow[] makes = inventoryTable.Select(filterStr);
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Make");
dt.Columns.Add("PetName");
dt.Columns.Add("Color");
if (makes.Length == 0)
MessageBox.Show("Sorry, no cars...", "Selection error!");
else
{
string strMake = "";
for (int i = 0; i < makes.Length; i++)
{
//from the current row, get the PetName value.
dt.ImportRow(makes[i]);
}
dataGridYugosView.DataSource = dt;
}
}
//find the rows you want to edit with a filter.
private void btnChangeMake_Click(object sender, EventArgs e)
{
// confirm selection.
if (DialogResult.Yes ==
MessageBox.Show("Are you sure?? BMW are much nicer than Yugos!",
"Please Confirm!", MessageBoxButtons.YesNo))
{
//build a filter.
string filterStr = "Make='BMW'";
string strMake = string.Empty;
//find all rows matching the filter.
DataRow[] makes = inventoryTable.Select(filterStr);
//change all beemers to Yugos!
for (int i = 0; i < makes.Length; i++)
{
makes[i]["Make"] = "Yugo";
}
}
}
private void txtMakeToView_TextChanged(object sender, EventArgs e)
{
if (txtMakeToView.Text == string.Empty)
{
carInventoryGridview.DataSource = inventoryTable;
}
}
private void dataGridYugosView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}