首页 > 开发 > 综合 > 正文

DataGrid实例(简单易懂,无复杂功能,适合初学者)

2024-07-21 02:29:20
字体:
来源:转载
供稿:网友

使access数据库,适合初学者,修改连接、查询语句后可直接运行,代码中有注明。

      填充dataset的步骤
      1、使用数据库连接字符串创建数据库连接对象
      2、用sql查询语句和数据库连接对象创建数据库适配器dataadapter
      3、使用dataadapter的fill 方法填充dataset


using system;
using system.windows.forms;
using system.data;
using system.data.sqlclient;
using system.data.oledb;
//professional c# 2nd的datagrid实例
/**////    <summary>
///    this class provides    an example of creating and using a data    grid.
///    </summary>
public class displaytabulardata : system.windows.forms.form
{
    private system.windows.forms.button retrievebutton;
    private system.windows.forms.datagrid datagrid;

    /**////    <summary>
    ///    construct the window.
    ///    </summary>
    ///    <remarks>
    ///    this method    constructs the window by creating both the data    grid and the button.
    ///    </remarks>
    public displaytabulardata()
    {
        this.autoscalebasesize = new system.drawing.size(5, 13);
        this.clientsize = new system.drawing.size(464, 253);
        this.text = "01_displaytabulardata";
        this.datagrid = new system.windows.forms.datagrid();
        datagrid.begininit();
        datagrid.location = new system.drawing.point(8, 8);
        datagrid.size = new system.drawing.size(448, 208);
        datagrid.tabindex = 0;
        datagrid.anchor = anchorstyles.bottom | anchorstyles.top | anchorstyles.left | anchorstyles.right;
        this.controls.add(this.datagrid);
        datagrid.endinit();
        this.retrievebutton = new system.windows.forms.button();
        retrievebutton.location = new system.drawing.point(384, 224);
        retrievebutton.size = new system.drawing.size(75, 23);
        retrievebutton.tabindex = 1;
        retrievebutton.anchor = anchorstyles.bottom | anchorstyles.right;
        retrievebutton.text = "retrieve";
        retrievebutton.click += new system.eventhandler(this.retrievebutton_click);
        this.controls.add(this.retrievebutton);
    }

    /**////    <summary>
    ///    retrieve the data
    ///    </summary>
    ///    <param name="sender"> </param>
    ///    <param name="e"> </param>
    protected void retrievebutton_click(object sender, system.eventargs e)
    {
        retrievebutton.enabled = false;

        string source = @"provider=microsoft.jet.oledb.4.0;data source=c:/documents and settings/manio/my documents/printmanager/program/printmanagev1/database/printdb.mdb";
        string select = "select * from maininfo";

        /**/////////////////////////////////
        //填充dataset的步骤
        //1、使用数据库连接字符串创建数据库连接对象
        //    2、用sql查询语句和数据库连接对象创建数据库适配器dataadapter
        //        3、使用dataadapter的fill 方法填充dataset

        oledbconnection olecon = new oledbconnection(source);

        oledbdataadapter da = new oledbdataadapter(select,olecon);

        dataset ds = new dataset();

        da.fill(ds, "maininfo");

        datagrid.setdatabinding(ds, "maininfo");    //datagrid的数据绑定,使用dataset 和 数据库的表名
    }

    /**////    <summary>
    ///    display    the    application    window
    ///    </summary>
    static void main()
    {
        application.run(new displaytabulardata());
    }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表