刚才复习了一下ADO.NET,做了一个简单例子,加图片,以后可以没事回头复习看看
- 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 WindowsFormsDataTableView
- {
- public partial class MainForm : Form
- {
- List<Car> listcars = new List<Car>();
- //创建
- DataTable invertoryTable = new DataTable("Inventroy");
- DataView coltsOnlyView;
- //主表单
- public MainForm()
- {
- InitializeComponent();
- listcars.Add(new Car("chucky","Bmw","green"));
- listcars.Add(new Car("Tiny", "Yugo", "white"));
- listcars.Add(new Car("ami", "Jeep", "tan"));
- listcars.Add(new Car("pain inducer", "caravan", "pink"));
- listcars.Add(new Car("fred", "Bmw", "pea soap green"));
- listcars.Add(new Car("sidd", "Bmw", "black"));
- listcars.Add(new Car("mel", "Firebind", "red"));
- listcars.Add(new Car("sarah", "colt", "black"));
- //创建一个表单;
- createDataTable();
- //创建一个视图;
- createDataView();
- }
- //显示行号,没有实现。。。
- void carInventoryGridVIew_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
- {
- using (SolidBrush b = new SolidBrush(Color.Black))
- {
- e.Graphics.DrawString((e.RowIndex).ToString(),
- e.InheritedRowStyle.Font, b,
- e.RowBounds.Location.X + 15,
- e.RowBounds.Location.Y + 4);
- }
- }
- //创建制造商为Colt的视图
- void createDataView()
- {
- coltsOnlyView =new DataView(invertoryTable);
- coltsOnlyView.RowFilter = "Make = 'Colt'";
- dataGridColtsVIew.DataSource=coltsOnlyView;
- }
- //创建图表
- void createDataTable()
- {
- DataColumn carIDColumn = new DataColumn("CarID", typeof(int));
- carIDColumn.Caption = "Car ID";
- carIDColumn.ReadOnly = true;
- carIDColumn.AllowDBNull = false;
- carIDColumn.Unique = true;
- carIDColumn.AutoIncrement = true;
- carIDColumn.AutoIncrementSeed = 0;
- carIDColumn.AutoIncrementStep = 1;
- 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";
- invertoryTable.Columns.AddRange(new DataColumn[] { carIDColumn,carMakeColumn, carColorColumn, carPetNameColumn });
- invertoryTable.PrimaryKey = new DataColumn[] { invertoryTable.Columns[0] };
- foreach (Car c in listcars)
- {
- DataRow newRow = invertoryTable.NewRow();
- newRow["Make"] = c.carMake;
- newRow["Color"] = c.carColor;
- newRow["PetName"] = c.carPetName;
- invertoryTable.Rows.Add(newRow);
- }
- carInventoryGridVIew.DataSource = invertoryTable;
- }
- //删除某一行Button
- private void BtnRemoverRow_Click(object sender, EventArgs e)
- {
- try
- {
- invertoryTable.Rows[(int.Parse(txtRowRemove.Text))].Delete();
- invertoryTable.AcceptChanges();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- //显示耨一制造商Button
- private void btnDisplayMakes_Click(object sender, EventArgs e)
- {
- string filterstr = string.Format("Make= '{0}'", txtMakeToView.Text);
- DataRow[] makes = invertoryTable.Select(filterstr,"PetName DESC");
- if (makes.Length == 0)
- MessageBox.Show("Sorry, no cars...", "Selection error!");
- else{
- string strmake =null;
- for(int i=0;i<makes.Length;i++)
- {
- DataRow temp =makes[i];
- strmake += temp["PetName"]+"/n";
- }
- MessageBox.Show(strmake,string.Format("{0} types:",txtMakeToView.Text));
- }
- }
- //显示ID号大于5的汽车名字和ID号方法
- private void showcarswithIDgreaterthanfive()
- {
- DataRow[] properIDs;
- string newfilterstr = "CarID >5";
- properIDs = invertoryTable.Select(newfilterstr);
- string striDs = null;
- for(int i=0;i<properIDs.Length;i++)
- {
- DataRow temp = properIDs[i];
- striDs += temp["PetName"] + "is ID:" + temp["CarID"] + "/n";
- }
- MessageBox.Show(striDs, "Pet names of cars where CarID >5");
- }
- //显示ID号大于5的Button
- private void btnshowcar_Click(object sender, EventArgs e)
- {
- showcarswithIDgreaterthanfive();
- }
- //更改BMWtoYUGOS的Button
- private void btnchangeemwtoyugos_Click(object sender, EventArgs e)
- {
- if (DialogResult.Yes == MessageBox.Show("Are you sure?? Bmws are much nicer than Yugos!",
- "Please Confirm!", MessageBoxButtons.YesNo))
- {
- string filterstr = "Make ='Bmw'";
- string strMake = string.Empty;
- DataRow[] makes = invertoryTable.Select(filterstr);
- for (int i = 0; i < makes.Length; i++)
- makes[i]["Make"] = "Yugo";
- }
- }
- }
- }
- //汽车类,包含汽车ID,汽车名,制造商,汽车颜色
- class Car
- {
- public int carID
- { get; set; }
- public string carPetName
- { get; set; }
- public string carMake
- { get; set; }
- public string carColor
- { get; set; }
- public Car(string petName, string make, string color)
- {
- carPetName = petName;
- carMake = make;
- carColor = color;
- }
- }