當前位置: 首頁>>代碼示例>>C#>>正文


C# DataGrid.DataSource屬性代碼示例

本文整理匯總了C#中System.Windows.Forms.DataGrid.DataSource屬性的典型用法代碼示例。如果您正苦於以下問題:C# DataGrid.DataSource屬性的具體用法?C# DataGrid.DataSource怎麽用?C# DataGrid.DataSource使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在System.Windows.Forms.DataGrid的用法示例。


在下文中一共展示了DataGrid.DataSource屬性的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: BindToDataView

private void BindToDataView(DataGrid myGrid){
    // Create a DataView using the DataTable.
    DataTable myTable = new DataTable("Suppliers");
    // Insert code to create and populate columns.
    DataView myDataView = new DataView(myTable);
    myGrid.DataSource = myDataView;
 }
 private void BindToDataSet(DataGrid myGrid){
    // Create a DataSet.
    DataSet myDataSet = new DataSet("myDataSet");
    // Insert code to populate DataSet with several tables.
    myGrid.DataSource = myDataSet;
    // Use the DataMember property to specify the DataTable.
    myGrid.DataMember = "Suppliers";
 }
 private DataView GetDataViewFromDataSource(){
    // Create a DataTable variable, and set it to the DataSource.
    DataView myDataView;
    myDataView = (DataView) dataGrid1.DataSource;
    return myDataView;
 }
 private DataSet GetDataSetFromDataSource(){
    // Create a DataSet variable, and set it to the DataSource.
    DataSet myDataSet;
    myDataSet = (DataSet) dataGrid1.DataSource;
    return myDataSet;
 }
開發者ID:.NET開發者,項目名稱:System.Windows.Forms,代碼行數:27,代碼來源:DataGrid.DataSource

示例2: Form1

//引入命名空間
using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

public class Form1 : System.Windows.Forms.Form {
    internal System.Windows.Forms.DataGrid DataGrid1;
    internal System.Windows.Forms.Button btnRunQuery;
    private System.Windows.Forms.Button btnRunQuery2;
    public Form1() {
        this.DataGrid1 = new System.Windows.Forms.DataGrid();
        this.btnRunQuery = new System.Windows.Forms.Button();
        this.btnRunQuery2 = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.DataGrid1)).BeginInit();
        this.SuspendLayout();

        this.DataGrid1.DataMember = "";
        this.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
        this.DataGrid1.Location = new System.Drawing.Point(8, 8);
        this.DataGrid1.Size = new System.Drawing.Size(280, 200);

        this.btnRunQuery.Location = new System.Drawing.Point(8, 224);
        this.btnRunQuery.Size = new System.Drawing.Size(80, 24);
        this.btnRunQuery.Text = "Run Query 1";
        this.btnRunQuery.Click += new System.EventHandler(this.btnRunQuery_Click);

        this.btnRunQuery2.Location = new System.Drawing.Point(104, 224);
        this.btnRunQuery2.Size = new System.Drawing.Size(80, 24);
        this.btnRunQuery2.Text = "Run Query 2";
        this.btnRunQuery2.Click += new System.EventHandler(this.btnRunQuery2_Click);

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 273);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.btnRunQuery2,
                                                                          this.DataGrid1,
                                                                          this.btnRunQuery});
        ((System.ComponentModel.ISupportInitialize)(this.DataGrid1)).EndInit();
        this.ResumeLayout(false);

    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }

    private void btnRunQuery_Click(object sender, System.EventArgs e) {
        try {
            SqlConnection cn = new SqlConnection("data source=.;database=biblio;uid=admin;pwd=pw");

            SqlDataAdapter da = new SqlDataAdapter("Select top 50 Author, Year_born from Authors Where Year_born is not null", cn);

            DataSet ds = new DataSet();

            da.Fill(ds, "Authors");

            DataGrid1.DataSource = ds.Tables["Authors"];
        } catch (SqlException ex) {
            Debug.WriteLine(ex.ToString());
        }
    }

    private void btnRunQuery2_Click(object sender, System.EventArgs e) {
        try {
            SqlConnection cn = new SqlConnection("data source=.;database=biblio;uid=admin;pwd=pw");

            SqlDataAdapter da = new SqlDataAdapter("Select Title, Price from Titles where Title like 'Hit%'", cn);

            DataSet ds = new DataSet();

            da.Fill(ds, "Titles and Price");

            DataGrid1.DataSource = ds.Tables["Titles and Price"];
        } catch (SqlException ex) {
            Debug.WriteLine(ex.ToString());
        }
    }

}
開發者ID:C#程序員,項目名稱:System.Windows.Forms,代碼行數:84,代碼來源:DataGrid.DataSource


注:本文中的System.Windows.Forms.DataGrid.DataSource屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。