📄 sample18.cs
字号:
namespace apiBook
{
/*本程序来自MSDN*/
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components;
private Button button1;
private Button button2;
private DataGrid myDataGrid;
private DataSet myDataSet;
private bool TablesAlreadyAdded;
public Form1()
{
InitializeComponent();
SetUp();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.components = new Container();
this.button1 = new Button();
this.button2 = new Button();
this.myDataGrid = new DataGrid();
this.AutoScaleBaseSize = new Size(5, 13);
this.Text = "DataGrid Control Sample";
this.ClientSize = new Size(450, 330);
button1.Location = new Point(24, 16);
button1.Size = new Size(120, 24);
button1.Text = "Change Appearance";
button1.Click+=new EventHandler(button1_Click);
button2.Location = new Point(150, 16);
button2.Size = new Size(120, 24);
button2.Text = "Get Binding Manager";
button2.Click+=new EventHandler(button2_Click);
myDataGrid.Location = new Point(24, 50);
myDataGrid.Size = new Size(300, 200);
myDataGrid.CaptionText = "Microsoft DataGrid Control";
myDataGrid.MouseUp +=
new MouseEventHandler(Grid_MouseUp);
this.Controls.Add(button1);
this.Controls.Add(button2);
this.Controls.Add(myDataGrid);
}
public static void Main()
{
Application.Run(new Form1());
}
private void SetUp()
{
MakeDataSet();
myDataGrid.SetDataBinding(myDataSet, "Customers");
}
protected void button1_Click(object sender, EventArgs e)
{
if(TablesAlreadyAdded) return;
AddCustomDataTableStyle();
}
private void AddCustomDataTableStyle()
{
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "Customers";
ts1.AlternatingBackColor = Color.LightGray;
DataGridColumnStyle boolCol =
new DataGridBoolColumn();
boolCol.MappingName = "Current";
boolCol.HeaderText = "IsCurrent Customer";
boolCol.Width = 150;
ts1.GridColumnStyles.Add(boolCol);
DataGridColumnStyle TextCol =
new DataGridTextBoxColumn();
TextCol.MappingName = "custName";
TextCol.HeaderText = "Customer Name";
TextCol.Width = 250;
ts1.GridColumnStyles.Add(TextCol);
DataGridTableStyle ts2 = new DataGridTableStyle();
ts2.MappingName = "Orders";
ts2.AlternatingBackColor = Color.LightBlue;
DataGridColumnStyle cOrderDate =
new DataGridTextBoxColumn();
cOrderDate.MappingName = "OrderDate";
cOrderDate.HeaderText = "Order Date";
cOrderDate.Width = 100;
ts2.GridColumnStyles.Add(cOrderDate);
PropertyDescriptorCollection pcol = this.BindingContext
[myDataSet,"Customers.custToOrders"]
.GetItemProperties();
DataGridColumnStyle csOrderAmount =
new DataGridTextBoxColumn(
pcol["OrderAmount"], "c", true);
csOrderAmount.MappingName = "OrderAmount";
csOrderAmount.HeaderText = "Total";
csOrderAmount.Width = 100;
ts2.GridColumnStyles.Add(csOrderAmount);
myDataGrid.TableStyles.Add(ts1);
myDataGrid.TableStyles.Add(ts2);
TablesAlreadyAdded=true;
}
protected void button2_Click(object sender,EventArgs e)
{
BindingManagerBase bmGrid;
bmGrid = BindingContext[myDataSet, "Customers"];
MessageBox.Show("Current BindingManager Position: "
+ bmGrid.Position);
}
private void Grid_MouseUp(
object sender, MouseEventArgs e)
{
DataGrid myGrid = (DataGrid)sender;
DataGrid.HitTestInfo myHitInfo =
myGrid.HitTest(e.X, e.Y);
Console.WriteLine(myHitInfo);
Console.WriteLine(myHitInfo.Type);
Console.WriteLine(myHitInfo.Row);
Console.WriteLine(myHitInfo.Column);
}
private void MakeDataSet()
{
myDataSet = new DataSet("myDataSet");
DataTable tCust = new DataTable("Customers");
DataTable tOrders = new DataTable("Orders");
DataColumn cCustID =
new DataColumn("CustID", typeof(int));
DataColumn cCustName =
new DataColumn("CustName");
DataColumn cCurrent =
new DataColumn("Current", typeof(bool));
tCust.Columns.Add(cCustID);
tCust.Columns.Add(cCustName);
tCust.Columns.Add(cCurrent);
DataColumn cID =
new DataColumn("CustID", typeof(int));
DataColumn cOrderDate =
new DataColumn("orderDate",typeof(DateTime));
DataColumn cOrderAmount =
new DataColumn("OrderAmount", typeof(decimal));
tOrders.Columns.Add(cOrderAmount);
tOrders.Columns.Add(cID);
tOrders.Columns.Add(cOrderDate);
myDataSet.Tables.Add(tCust);
myDataSet.Tables.Add(tOrders);
DataRelation dr =
new DataRelation ("custToOrders", cCustID , cID);
myDataSet.Relations.Add(dr);
DataRow newRow1;
DataRow newRow2;
for(int i = 1; i < 4; i++)
{
newRow1 = tCust.NewRow();
newRow1["custID"] = i;
tCust.Rows.Add(newRow1);
}
tCust.Rows[0]["custName"] = "Alpha";
tCust.Rows[1]["custName"] = "Beta";
tCust.Rows[2]["custName"] = "Omega";
tCust.Rows[0]["Current"] = true;
tCust.Rows[1]["Current"] = true;
tCust.Rows[2]["Current"] = false;
for(int i = 1; i < 4; i++)
{
for(int j = 1; j < 6; j++)
{
newRow2 = tOrders.NewRow();
newRow2["CustID"]= i;
newRow2["orderDate"]=
new DateTime(2001, i, j * 2);
newRow2["OrderAmount"] = i * 10 + j * .1;
tOrders.Rows.Add(newRow2);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -