首 页 | 精品电影 | 音乐天堂 | 在线游戏 | Flash MTV | 三湘书屋 | 幽默笑话 | 三湘图库 | 美女写真 | IT知识库 | QQ贴图 | 加入书签

网页制作网络编程图形图象操作系统冲浪宝典软件教学网络安全认证考试通信技术电子商务业内动态书籍教程原码

最近更新 文章分类 多媒体类 精品软件

本站搜索:
您的位置:三湘时空 -> IT知识库 -> 文章分类 -> ASP.NET技巧 -> DataGrid实例(简单易懂,无复杂功能,适合初学者)
DataGrid实例(简单易懂,无复杂功能,适合初学者)


文章类别:ASP.NET技巧 来源: 作者: 发表日期:2006-4-5 字体:[ ]

小游戏 | 在线影院 | 幽默笑话 | 源码下载 | Flash MTV | 音乐试听 | 书屋 | 美女写真

使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());
    }
}

上一篇:c#摄氏华氏转换 下一篇:QQ聊天记录保护器制作全过程
本栏目热门文章
·如何实现无刷新的DropdownList联动效果 2005-10-4
·使用HttpWebRequest向网站模拟上传数据 2005-10-4
·当DataSet中包含主/子表时,Update更新步骤 2005-10-6
·ASP.NET2.0实现无刷新客户端回调 2005-11-13
·ASP.NET中文件上传下载方法集合 2006-5-28
·分享个极好的无刷新二级联动下拉列表,同样适用与firefox 2005-10-19
·在Web DataGrid中当鼠标移到某行与离开时行的颜色发生改变( 2005-10-4
·ASP.NET中实现Flash与.NET的紧密集成 2005-11-21
·关于Asp.net页面Page_Load被执行两次的问题 2005-10-4
·ASP.NET极限:页面导航 (翻译) 2005-10-8
新近更新文章
·在VS2003中直接用DREAMWEAVER8打开ASPX文件 2006-6-22
·asp.net2.0中异步调用WebService(异步页) 2006-6-22
·ASP.NET2.0数据库入门之常见错误 2006-6-22
·ASP.NET 2.0中XSLT的使用 2006-6-22
·.Net2.0 使用ConfigurationManager读写配置 2006-6-22
·.net 做的IP 访问限制 2006-6-22
·ASP.Net2.0 GridView 多列排序,显示排序图标,分页 2006-6-22
·数据岛出到Excel最为简易的方法 2006-6-20
·权限树中Checkbox的操作[Asp.Net2.0] 2006-6-20
·使用Data Access Application Block 得到 2006-6-20
首 页 | 软件发布 | 广告联系 | 下载帮助 | 意见反馈 | 网站地图
  CopyRight? 2002-2004 WWW.SXSKY.NET? All Rights Reserved
三湘时空 站长QQ:82675303 Email: