当前位置: 首页>>代码示例>>C#>>正文


C# DataGrid.SetDataBinding方法代码示例

本文整理汇总了C#中System.Windows.Forms.DataGrid.SetDataBinding方法的典型用法代码示例。如果您正苦于以下问题:C# DataGrid.SetDataBinding方法的具体用法?C# DataGrid.SetDataBinding怎么用?C# DataGrid.SetDataBinding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Windows.Forms.DataGrid的用法示例。


在下文中一共展示了DataGrid.SetDataBinding方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BindControls

private void BindControls(){
    // Creates a DataSet named SuppliersProducts.
    DataSet SuppliersProducts = new DataSet("SuppliersProducts");
    // Adds two DataTable objects, Suppliers and Products.
    SuppliersProducts.Tables.Add(new DataTable("Suppliers"));
    SuppliersProducts.Tables.Add(new DataTable("Products"));
    // Insert code to add DataColumn objects.
    // Insert code to fill tables with columns and data.
    // Binds the DataGrid to the DataSet, displaying the Suppliers table.
    dataGrid1.SetDataBinding(SuppliersProducts, "Suppliers");
 }
开发者ID:.NET开发者,项目名称:System.Windows.Forms,代码行数:11,代码来源:DataGrid.SetDataBinding

示例2: Form1

//引入命名空间
using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Windows.Forms;

class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }

    private void getData_Click(object sender, EventArgs e) {
        string orders = "SELECT * FROM Orders";
        string customers = "SELECT * FROM Customers";

        using (SqlConnection con = new SqlConnection("northwind Connection String")) {

            SqlDataAdapter da = new SqlDataAdapter(orders, con);

            DataSet ds = new DataSet();

            da.Fill(ds, "Orders");

            da = new SqlDataAdapter(customers, con);

            da.Fill(ds, "Customers");

            ds.Relations.Add("CustomerOrders",
                ds.Tables["Customers"].Columns["CustomerID"],
                ds.Tables["Orders"].Columns["CustomerID"]);

            DataViewManager dvm = new DataViewManager(ds);

            dvm.DataViewSettings["Customers"].RowFilter = "Country='UK'";

            dataGrid.SetDataBinding(dvm, "Customers");

        }

    }
    private void InitializeComponent() {
        this.getData = new System.Windows.Forms.Button();
        this.dataGrid = new System.Windows.Forms.DataGrid();
        this.SuspendLayout();
        // 
        // getData
        // 
        this.getData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.getData.Location = new System.Drawing.Point(684, 558);
        this.getData.Name = "getData";
        this.getData.TabIndex = 0;
        this.getData.Text = "Get Data";
        this.getData.Click += new System.EventHandler(this.getData_Click);
        // 
        // dataGrid
        // 
        this.dataGrid.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.dataGrid.Location = new System.Drawing.Point(13, 13);
        this.dataGrid.Name = "dataGrid";
        this.dataGrid.Size = new System.Drawing.Size(745, 534);
        this.dataGrid.TabIndex = 1;
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(771, 593);
        this.Controls.Add(this.dataGrid);
        this.Controls.Add(this.getData);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

    }



    private System.Windows.Forms.Button getData;
    private System.Windows.Forms.DataGrid dataGrid;

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}
开发者ID:C#程序员,项目名称:System.Windows.Forms,代码行数:89,代码来源:DataGrid.SetDataBinding


注:本文中的System.Windows.Forms.DataGrid.SetDataBinding方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。