使用 DataGrid






2.70/5 (30投票s)
如何使用 DataGrid 控件。
引言
这是一个简单的例子,向您展示如何通过 DataGrid
控件连接和编辑数据源。
什么是 DataGrid
DataGrid
是一个数据绑定列表控件,它以表格形式显示数据源中的项目。DataGrid 控件允许您选择、排序、添加、删除和编辑这些项目。
如何使用 DataGrid
我认为我对代码中的所有内容都进行了注释;但是,如果我遗漏了任何内容,请告诉我。
//By Muammar Yacoob
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//################# Required Library #################//
using System.Data.OleDb;
//####################################################//
namespace ez_Datagrid
{
public partial class DG_Form : Form
{
//################# Needed Database Objects #################//
OleDbConnection ConnectMe;
DataSet ds;
OleDbDataAdapter dap;
//###########################################################//
public DG_Form()
{
InitializeComponent();
}
private void btn_Connect_Click(object sender, EventArgs e)
{
//---------------- Connecting the database (mdb file) ------------------//
//################## Creating the connection ###########################//
ConnectMe = new OleDbConnection("Provider=Microsoft.Jet.OLEDB" +
".4.0;Data Source=" + txt_db_Location.Text);
//######################################################################//
try
{
//##### Connecting to the mdb file #####//
ConnectMe.Open();
//######################################//
//###### Setting up buttons, text boxes and status bar ######//
btn_Connect.Enabled = false;
txt_db_Location.Enabled = false;
btn_Fill.Enabled = true;
txt_SQL.Enabled = true;
status_Connection.Text = "Connected";
//###########################################################//
}
catch (OleDbException)
{
MessageBox.Show("Cannot connect to " + txt_db_Location.Text);
}
private void btn_Fill_Click(object sender, EventArgs e)
{
//----------------------- Filling the datagrid -------------------------//
try
{
//### Creating the sql command using the tables in the mdb file ###//
OleDbCommand cmd = new OleDbCommand(txt_SQL.Text, ConnectMe);
//#################################################################//
//#### Creating the adabter according to the sql command ####//
dap = new OleDbDataAdapter(cmd);
//###########################################################//
//#### Creating the dataset ####//
ds = new DataSet();
//##############################//
//##### Filling the dataset #####//
dap.Fill(ds);
//###############################//
//################ Filling the datagrid control ##################//
//################ with the first & only table ##############//
//################ retrieved by the connection ##################//
dataGrid1.DataSource = ds.Tables[0];
//################################################################//
//########## I dont have to explain that again, do I?? ###########//
btn_Fill.Text = "Refresh";
btn_Update.Enabled = true;
//################################################################//
}
catch (OleDbException)
{
MessageBox.Show("Please make sure you type in a correct SQL statement");
}
}
private void btn_Update_Click(object sender, EventArgs e)
{
//-------------------------- Updating data ----------------------------//
//############### Testing changes made to the dataset #################//
if (ds.GetChanges() != null)
//#####################################################################//
{
//########## generates a command that is used to #########//
//########### merge changes made to the dataset ##########//
OleDbCommandBuilder builder = new OleDbCommandBuilder(dap);
//########################################################//
try
{
//########### Getting changes made to the dataset ##########//
//############# applay them against the adapter ############//
MessageBox.Show(dap.Update(ds.GetChanges()) +
" row(s) updated");
//##########################################################//
//######### Confirming changes made to the dataset ##########//
//############ and resetting it for new changes #############//
ds.AcceptChanges();
//###########################################################//
}
catch (InvalidOperationException)
{
MessageBox.Show("Error retrieving data");
//######### Rejecting changes made to the dataset ########//
//############# through the datagrid control #############//
ds.RejectChanges();
//########################################################//
}
}
else
{
MessageBox.Show("nothing to update", "Ez Datagrid",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
关注点
请注意,如果您尝试连接到不包含主键的表,将会收到错误消息。