首页 > Linux操作系统 > Linux操作系统 > c#之datagridview控件_用于从数据库重新加载数据和向数据库提交更改


原创 Linux操作系统 作者:wisdomone1 时间:2011-08-26 16:27:23 0 删除 编辑

using System;
using System.Data;
using System.Data.SqlClient;//sqlclient是与sqlserver交互的名称空间,若为oracle,则是;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form
    private DataGridView dataGridView1 = new DataGridView();//系统自动生成
    private BindingSource bindingSource1 = new BindingSource();//bindingsource组件用于管理控件和数据源绑定工作,简化这个绑定管理
    private SqlDataAdapter dataAdapter = new SqlDataAdapter();//sqldataadapter用于把加工的命令到数据集合上(由command->datatable或者command->dataset)
    private Button reloadButton = new Button();
    private Button submitButton = new Button();

    public static void Main()
        Application.Run(new Form1());//application类

    // Initialize the form.
    public Form1()
        dataGridView1.Dock = DockStyle.Fill;

        reloadButton.Text = "reload";
        submitButton.Text = "submit";
         // system.eventhandler(要订阅的事件名)
        reloadButton.Click += new System.EventHandler(reloadButton_Click);//为重载按钮订阅事件reloadbutton_click
        submitButton.Click += new System.EventHandler(submitButton_Click);

        FlowLayoutPanel panel = new FlowLayoutPanel();
        panel.Dock = DockStyle.Top;
        panel.AutoSize = true;

         //panel1.controls.addrange(new control[] {控件名称})
        panel.Controls.AddRange(new Control[] { reloadButton, submitButton });

        this.Controls.AddRange(new Control[] { dataGridView1, panel });
        this.Load += new System.EventHandler(Form1_Load);
        this.Text = "DataGridView databinding and updating demo";

    private void Form1_Load(object sender, System.EventArgs e)
        // Bind the DataGridView to the BindingSource
        // and load the data from the database.
        dataGridView1.DataSource = bindingSource1;
        GetData("select * from Customers");

    private void reloadButton_Click(object sender, System.EventArgs e)
        // Reload the data from the database.

    private void submitButton_Click(object sender, System.EventArgs e)
        // Update the database with the user's changes.

    private void GetData(string selectCommand) //方法参数为运行的sql
            // Specify a connection string. Replace the given value with a
            // valid connection string for a Northwind SQL Server sample
            // database accessible to your system.
            String connectionString =
                "Integrated Security=SSPI;Persist Security Info=False;" +
                "Initial Catalog=Northwind;Data Source=localhost";

            // Create a new data adapter based on the specified query.
            dataAdapter = new SqlDataAdapter(selectCommand, connectionString);

            // Create a command builder to generate SQL update, insert, and
            // delete commands based on selectCommand. These are used to
            // update the database.
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

            // Populate a new data table and bind it to the BindingSource.
            DataTable table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            bindingSource1.DataSource = table;

            // Resize the DataGridView columns to fit the newly loaded content.
        catch (SqlException)
            MessageBox.Show("To run this example, replace the value of the " +
                "connectionString variable with a connection string that is " +
                "valid for your system.");


来自 “ ITPUB博客 ” ,链接:,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
提供针对oracle初学者及进阶的数据库培训,欢迎大家咨询: 微信: wisdomone 微信公众号: lovedb 工作经历: 中国普天 北京科蓝 北京云和恩墨 北京神州新桥


  • 博文量
  • 访问量