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


C# DataGridView.DataSource属性代码示例

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


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

示例1: DataGridView

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

public class Form1 : System.Windows.Forms.Form
{
    private DataGridView dataGridView1 = new DataGridView();
    private BindingSource bindingSource1 = new BindingSource();

    public Form1()
    {
        dataGridView1.Dock = DockStyle.Fill;
        this.Controls.Add(dataGridView1);
        InitializeDataGridView();
    }

    private void InitializeDataGridView()
    {
        try
        {
            // Set up the DataGridView.
            dataGridView1.Dock = DockStyle.Fill;

            // Automatically generate the DataGridView columns.
            dataGridView1.AutoGenerateColumns = true;

            // Set up the data source.
            bindingSource1.DataSource = GetData("Select * From Products");
            dataGridView1.DataSource = bindingSource1;

            // Automatically resize the visible rows.
            dataGridView1.AutoSizeRowsMode =
                DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;

            // Set the DataGridView control's border.
            dataGridView1.BorderStyle = BorderStyle.Fixed3D;

            // Put the cells in edit mode when user enters them.
            dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
        }
        catch (SqlException)
        {
            MessageBox.Show("To run this sample replace connection.ConnectionString" +
                " with a valid connection string to a Northwind" +
                " database accessible to your system.", "ERROR",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            System.Threading.Thread.CurrentThread.Abort();
        }
    }

    private static DataTable GetData(string sqlCommand)
    {
        string connectionString = "Integrated Security=SSPI;" +
            "Persist Security Info=False;" +
            "Initial Catalog=Northwind;Data Source=localhost";

        SqlConnection northwindConnection = new SqlConnection(connectionString);

        SqlCommand command = new SqlCommand(sqlCommand, northwindConnection);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = command;

        DataTable table = new DataTable();
        table.Locale = System.Globalization.CultureInfo.InvariantCulture;
        adapter.Fill(table);

        return table;
    }

    [STAThreadAttribute()]
    public static void Main()
    {
        Application.Run(new Form1());
    }
}
开发者ID:.NET开发者,项目名称:System.Windows.Forms,代码行数:78,代码来源:DataGridView.DataSource

示例2: ToString

//引入命名空间
using System.Drawing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

public class Book {
    public String Title { get; set; }

    public override String ToString() {
        return Title;
    }
}
public class FormBooks : Form {

    static public Book[] Books =
    {
      new Book {Title="F"},
      new Book {Title="B"}
    };
    public FormBooks() {
        InitializeComponent();
    }

    private void FormStrings_Load(object sender, EventArgs e) {
        String[] books = { "F", "A", "B", "R", "B" };

        var query =
          from book in books
          where book.Length > 10
          orderby book
          select new { Book = book.ToUpper() };

        dataGridView1.DataSource = query.ToList();
    }
    private void InitializeComponent() {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        //
        // dataGridView1
        //
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.dataGridView1.Location = new System.Drawing.Point(10, 10);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(272, 251);
        this.dataGridView1.TabIndex = 0;
        //
        // FormStrings
        //
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 271);
        this.Controls.Add(this.dataGridView1);
        this.Name = "FormStrings";
        this.Padding = new System.Windows.Forms.Padding(10);
        this.Text = "FormStrings";
        this.Load += new System.EventHandler(this.FormStrings_Load);
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);

    }
    private System.Windows.Forms.DataGridView dataGridView1;
    public static void Main() {
        Application.Run(new FormBooks());
    }
}
开发者ID:C#程序员,项目名称:System.Windows.Forms,代码行数:69,代码来源:DataGridView.DataSource


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