介绍:
SourceGrid 是一个的完全地用C#开发的Windows窗体控件;想实现excel或者word表格的样式的话,这是最好的选择.
官方网址: http://sourceforge.net/projects/sourcegrid
下面的代码可以详细的说明如何运用这个好控件.(代码是不能直接运行的,仅做参考).
其中也包含了access数据库操作的常用方法.
还顺便加了DockPanel的演练.
DockPanel是一个可以做到vs2003工具栏,属性窗口样式的控件.
官网:http://sourceforge.net/projects/dockpanelsuite/
DockPanel suite is designed to achieve docking capability for MDI forms. It can be used to develop applications with Visual Studio .Net style user interface.
DockPanel suite is a 100%-native .Net windows forms control, written in C#
Features of DockPanel suite:
· Complex hierarchies
· Auto-hide
· Free drag-and-drop
· Nested docking
· Integration with .Net Framework and VS .Net
· Persistent to XML file
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI;
using System.Collections;
using System.IO;
using System.Data.OleDb;
namespace docktest
{
public partial class Document3 : DockContent//Form 就可以了,这儿是从Dockpannel继承的
{
string path;
public Datas datas = new Datas();
/**//// <summary>
/// 数据库路径
/// </summary>
string data_base = "\\goodcity.mdb";
/**//// <summary>
/// 表名:指标索引
/// </summary>
string data_name = "原件目录";
public bool runing = false;
/**//// <summary>
/// 分页数量
/// </summary>
private int pages = 1;//分页数量
/**//// <summary>
/// 当前页次
/// </summary>
private int pageat = 1;//当前页次
/**//// <summary>
/// 每页行数
/// </summary>
int pageline = 15;//每页行数
/**//// <summary>
/// 总行数
/// </summary>
private int rcount;//行数
/**//// <summary>
/// 总列数
/// </summary>
private int mcount;//列数
private string caption;//测评内容
private string cetotal;//datas.cecis.Length.ToString();//册数
private string ceindex;//册次
private string bianhao;//标题
SourceGrid.Cells.Controllers.Button mController_Link;//连接事件
//Font font1 = new Font("楷体_GB2312", 18, FontStyle.Regular);
Font font1 = new Font(FontFamily.GenericSerif,14,FontStyle.Regular);
Font font2 = new Font(FontFamily.GenericSerif, 11f, FontStyle.Regular);
Font font3 = new Font(FontFamily.GenericSerif, 9f, FontStyle.Regular);
public Document3()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
path = Application.StartupPath;
this.runing = true;
//this.timer1.Interval = 500;
//this.timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
this.timer1.Stop();
if (this.runing)
{
this.init();
}
else
{
this.init2();
}
}
catch (System.Exception op)
{
//MessageBox.Show("init " + op.Message);
}
}
public void init()
{
string key = this.datas.flags + " AND 册次=" + this.datas.n + " ORDER BY 原件顺序号";//
this.searchdata(path + data_base, data_name, key, "search3");
rcount = this.dataSet1.Tables["search3"].Rows.Count;
mcount = this.dataSet1.Tables["search3"].Columns.Count;
caption = datas.caption;//测评内容
cetotal = this.dataSet1.Tables["search3"].Rows[0].ItemArray[3].ToString();//总册数
ceindex = datas.n.ToString();//册次,
bianhao = datas.subtitle;//标题
if (rcount > this.pageline)
{
if (rcount % pageline == 0)
{
this.pages = (int)Math.Floor((double)(rcount / pageline));
}
else
{
this.pages = (int)Math.Floor((double)(rcount / pageline)) + 1;//分页数量
}
}
else
{
this.pages = 1;
}
this.pageat = 1;
this.numer_n.Value = this.pageat;
this.textBox_n.Text = this.pages.ToString();
this.numer_n.Maximum = this.pages;
this.numer_n.Minimum = 1;
this.drawgird();
}
public void init2()
{
this.searchdata(path + data_base, data_name, this.datas.flags, "search3");
rcount = this.dataSet1.Tables["search3"].Rows.Count;
mcount = this.dataSet1.Tables["search3"].Columns.Count;
caption = "";//测评内容
cetotal = "";//册数
ceindex = "";//册次,
ceindex = "";
bianhao = "";//标题
if (rcount > this.pageline)
{
if (rcount % pageline == 0)
{
this.pages = (int)Math.Floor((double)(rcount / pageline));
}
else
{
this.pages = (int)Math.Floor((double)(rcount / pageline)) + 1;//分页数量
}
}
else
{
this.pages = 1;
}
this.pageat = 1;
this.numer_n.Value = this.pageat;
this.textBox_n.Text = this.pages.ToString();
this.numer_n.Maximum = this.pages;
this.numer_n.Minimum = 1;
this.drawgird();
}
private void drawgird()
{
int begin = 0;
if (this.pageat > 1)
{
begin = (this.pageat - 1) * this.pageline;
}
int end = pageline;
if (pageat == pages)
{
end = this.rcount % pageline;
if (end == 0)
{
end = pageline;
}
}
//this.grid1 = new SourceGrid.Grid();
mController_Link = new SourceGrid.Cells.Controllers.Button();
mController_Link.Executed += new EventHandler(mController_Link_Click);
绘制表格#region 绘制表格
grid1.Redim(this.pageline + 7 , 10);
int xx = 0;//行号
int yy = 0;//列号
//1 Header Row
grid1[0, 0] = new MyHeader(" ");
grid1[0, 1] = new MyHeader(" ");
grid1[0, 2] = new MyHeader(" ");
grid1[0, 3] = new MyHeader(" ");
grid1[0, 4] = new MyHeader(" ");
grid1[0, 5] = new MyHeader(" ");
grid1[0, 6] = new MyHeader(" ");
grid1[0, 7] = new MyHeader(" ");
grid1[0, 8] = new MyHeader(" ");
grid1[0, 9] = new MyHeader(" ");
SourceGrid.Cells.Views.Cell viewImage = new SourceGrid.Cells.Views.Cell();
SourceGrid.Cells.Controllers.CustomEvents clickEvent = new SourceGrid.Cells.Controllers.CustomEvents();
clickEvent.Click += new EventHandler(clickEvent_Click);
// 0
xx = 1;
grid1[xx, 0] = new SourceGrid.Cells.RowHeader();
//grid1[xx, 0].Measure(new Size(100, 50));
// 1
xx = 1;
SourceGrid.Cells.Editors.EditorBase edits = new SourceGrid.Cells.Editors.EditorBase(typeof(string));
grid1[xx, 1] = new SourceGrid.Cells.Cell("卷内目录", edits);
grid1[xx, 1].ColumnSpan = 9;
/**////////////////////////////////////////////////
//视图样式
SourceGrid.Cells.Views.Cell cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font1;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
//cview.AnchorArea = new DevAge.Drawing.AnchorArea(0f, 0f, 300f, 50f, true, true);
//cview.AnchorArea = new DevAge.Drawing.AnchorArea(DevAge.Drawing.ContentAlignment.TopCenter, true);
//DevAge.Drawing.Border border = new DevAge.Drawing.Border(Color.Red, 1);
//cview.Border = new DevAge.Drawing.RectangleBorder(border);//边框
//cview.Border = new DevAge.Drawing.RectangleBorder(DevAge.Drawing.Border.Black1Width);//边框
//模型
//SourceGrid.Cells.Models.ModelContainer models = new SourceGrid.Cells.Models.ModelContainer();
//编辑器
//SourceGrid.Cells.Editors.EditorBase editors = new SourceGrid.Cells.Editors.EditorBase(typeof(string));
//虚拟表格
//SourceGrid.Cells.Virtual.CellVirtual cellvirtral = new SourceGrid.Cells.Virtual.CellVirtual();
/**///////////////////////////////////////////////
cview.WordWrap = true;
grid1[xx, 1].View = cview;
// 2
xx = 2;
grid1[xx, 0] = new SourceGrid.Cells.RowHeader();
grid1[xx, 1] = new SourceGrid.Cells.Cell("评测内容");
grid1[xx, 1].RowSpan = 2;
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font2;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
cview.WordWrap = true;
grid1[xx, 1].View = cview;
grid1[xx, 2] = new SourceGrid.Cells.Cell(caption);
grid1[xx, 2].RowSpan = 2;
grid1[xx, 2].ColumnSpan = 3;
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font2;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleLeft;
cview.AnchorArea = new DevAge.Drawing.AnchorArea(DevAge.Drawing.ContentAlignment.TopCenter, true);
cview.WordWrap = true;
grid1[xx, 2].View = cview;
grid1[xx, 5] = new SourceGrid.Cells.Cell("编号");
//grid1[xx, 5].RowSpan = 1;
grid1[xx, 5].ColumnSpan = 2;
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font2;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
cview.WordWrap = true;
grid1[xx, 5].View = cview;
grid1[xx, 7] = new SourceGrid.Cells.Cell(bianhao);
//grid1[xx, 7].RowSpan = 1;
grid1[xx, 7].ColumnSpan = 3;
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font2;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleLeft;
cview.WordWrap = true;
grid1[xx, 7].View = cview;
// 3
xx = 3;
grid1[xx, 0] = new SourceGrid.Cells.RowHeader();
grid1[xx, 5] = new SourceGrid.Cells.Cell("共 " + cetotal + " 册 第 " + ceindex + " 册");
grid1[xx, 5].ColumnSpan = 5;
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font2;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
cview.WordWrap = true;
grid1[xx, 5].View = cview;
// 4
xx = 4;
grid1[xx, 0] = new SourceGrid.Cells.RowHeader();
grid1[xx, 1] = new SourceGrid.Cells.Cell("顺序号");
grid1[xx, 1].RowSpan = 2;
grid1[xx, 1].ColumnSpan = 1;
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font2;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
cview.WordWrap = true;
grid1[xx, 1].View = cview;
grid1[xx, 2] = new SourceGrid.Cells.Cell("文号");
grid1[xx, 2].RowSpan = 2;
grid1[xx, 2].ColumnSpan = 1;
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font2;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
cview.WordWrap = true;
grid1[xx, 2].View = cview;
grid1[xx, 3] = new SourceGrid.Cells.Cell("责任人");
grid1[xx, 3].RowSpan = 2;
grid1[xx, 3].ColumnSpan = 1;
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font2;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
cview.WordWrap = true;
grid1[xx, 3].View = cview;
grid1[xx, 4] = new SourceGrid.Cells.Cell("文件题名");
grid1[xx, 4].RowSpan = 2;
grid1[xx, 4].ColumnSpan = 1;
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font2;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
cview.WordWrap = true;
grid1[xx, 4].View = cview;
grid1[xx, 5] = new SourceGrid.Cells.Cell("日期");
grid1[xx, 5].RowSpan = 2;
grid1[xx, 5].ColumnSpan = 1;
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font2;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
cview.WordWrap = true;
grid1[xx, 5].View = cview;
grid1[xx, 6] = new SourceGrid.Cells.Cell("页号");
grid1[xx, 6].RowSpan = 2;
grid1[xx, 6].ColumnSpan = 1;
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font2;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
cview.WordWrap = true;
grid1[xx, 6].View = cview;
grid1[xx, 7] = new SourceGrid.Cells.Cell("相关载体");
grid1[xx, 7].RowSpan = 1;
grid1[xx, 7].ColumnSpan = 3;
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font2;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
cview.WordWrap = true;
grid1[xx, 7].View = cview;
// 5
xx = 5;
grid1[xx, 0] = new SourceGrid.Cells.RowHeader();
grid1[xx, 7] = new SourceGrid.Cells.Cell("照片(张)");
grid1[xx, 7].RowSpan = 1;
grid1[xx, 7].ColumnSpan = 1;
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font3;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
cview.WordWrap = true;
grid1[xx, 7].View = cview;
grid1[xx, 8] = new SourceGrid.Cells.Cell("录像(盘)");
grid1[xx, 8].RowSpan = 1;
grid1[xx, 8].ColumnSpan = 1;
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font3;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
cview.WordWrap = true;
grid1[xx, 8].View = cview;
grid1[xx, 9] = new SourceGrid.Cells.Cell("媒体报道(条)");
grid1[xx, 9].RowSpan = 1;
grid1[xx, 9].ColumnSpan = 1;
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font3;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
cview.WordWrap = true;
grid1[xx, 9].View = cview;
object[] items = new object[mcount];
for (int kl = 0; kl < mcount; kl++)
{
items[kl] = "";
}
for (int i = 0; i <pageline ; i++)
{
items = new object[mcount];
if (i + begin >= rcount)
{
items =new object[mcount];
for (int kl = 0; kl < mcount;kl++ )
{
items[kl] = "";
}
}
else
{
this.dataSet1.Tables["search3"].Rows[i + begin].ItemArray.CopyTo(items, 0);
}
循环表格内容#region 循环表格内容
xx = i + 6;
//行头
grid1[xx, 0] = new SourceGrid.Cells.RowHeader();
//顺序号
grid1[xx, 1] = new SourceGrid.Cells.Cell(items[5].ToString());
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font3;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
cview.WordWrap = true;
grid1[xx, 1].View = cview;
//文号
grid1[xx, 2] = new SourceGrid.Cells.Cell(items[6].ToString());
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font3;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleLeft;
cview.WordWrap = true;
grid1[xx, 2].View = cview;
//责任人,linklabel
grid1[xx, 3] = new SourceGrid.Cells.Cell(items[7].ToString());
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font3;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleLeft;
cview.WordWrap = true;
grid1[xx, 3].View = cview;
//文件题名,linklabel
grid1[xx, 4] = new SourceGrid.Cells.Link(items[8].ToString());
grid1[xx, 4].ToolTipText = items[14].ToString();
cview = new SourceGrid.Cells.Views.Link();
cview.Font = font3;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleLeft;
cview.WordWrap = true;
grid1[xx, 4].View = cview;
grid1[xx, 4].AddController(mController_Link);
//日期
grid1[xx, 5] = new SourceGrid.Cells.Cell(items[9].ToString());
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font3;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleLeft;
cview.WordWrap = true;
grid1[xx, 5].View = cview;
//页号
grid1[xx, 6] = new SourceGrid.Cells.Cell(items[10].ToString());
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font3;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleLeft;
cview.WordWrap = true;
grid1[xx, 6].View = cview;
//相关载体--照片,linklable
grid1[xx, 7] = new SourceGrid.Cells.Link(items[11].ToString());
grid1[xx, 7].ToolTipText = items[15].ToString();
cview = new SourceGrid.Cells.Views.Link();
cview.Font = font3;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleLeft;
cview.WordWrap = true;
grid1[xx, 7].View = cview;
grid1[xx, 7].AddController(mController_Link);
//相关载体--录像
grid1[xx, 8] = new SourceGrid.Cells.Link(items[12].ToString());
grid1[xx, 8].ToolTipText = items[16].ToString();
cview = new SourceGrid.Cells.Views.Link();
cview.Font = font3;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleLeft;
cview.WordWrap = true;
grid1[xx, 8].View = cview;
grid1[xx, 8].AddController(mController_Link);
//相关载体--媒体报道
grid1[xx, 9] = new SourceGrid.Cells.Cell(items[13].ToString());
cview = new SourceGrid.Cells.Views.Cell();
cview.Font = font3;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleLeft;
cview.WordWrap = true;
grid1[xx, 9].View = cview;
#endregion
}
#endregion
resize();
}
private class MyHeader : SourceGrid.Cells.ColumnHeader
{
public MyHeader(object value) : base(value)
{
//1 Header Row
SourceGrid.Cells.Views.ColumnHeader view = new SourceGrid.Cells.Views.ColumnHeader();
view.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
view.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
View = view;
AutomaticSortEnabled = false;
}
}
private void Form1_Resize(object sender, EventArgs e)
{
resize();
}
private void resize()
{
try
{
if (this.Visible)
{
//grid1.AutoSizeCells();
grid1.Columns[0].Width = 20;
grid1.Rows[0].Height = 20;
grid1.Columns[1].Width = 40;
grid1.Columns[2].Width = 60;
grid1.Columns[3].Width = 120;
grid1.Columns[4].Width = 320;
grid1.Columns[5].Width = 80;
grid1.Columns[6].Width = 60;
grid1.Columns[7].Width = 60;
grid1.Columns[8].Width = 60;
grid1.Columns[9].Width = 60;
grid1.Rows[1].Height = 60;
grid1.Rows[2].Height = 60;
grid1.Rows[3].Height = 30;
grid1.Rows[4].Height = 30;
grid1.Rows[5].Height = 40;
grid1.Rows[6].Height = 40;
grid1.Rows[7].Height = 40;
grid1.Rows[8].Height = 40;
grid1.Rows[9].Height = 40;
grid1.Rows[10].Height = 40;
grid1.Rows[11].Height = 40;
grid1.Rows[12].Height = 40;
grid1.Rows[13].Height = 40;
grid1.Rows[14].Height = 40;
grid1.Rows[15].Height = 40;
grid1.Rows[16].Height = 40;
grid1.Rows[17].Height = 40;
grid1.Rows[18].Height = 40;
grid1.Rows[19].Height = 40;
grid1.Rows[20].Height = 40;
}
}
catch
{ }
}
private SourceGrid.Cells.Views.Cell cviews(Font font)
{
//视图样式
SourceGrid.Cells.Views.Cell cview = new SourceGrid.Cells.Views.Cell();
//cview.Font = new Font("宋体", 22, FontStyle.Regular);
cview.Font = font;
cview.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
//cview.AnchorArea = new DevAge.Drawing.AnchorArea(0f, 0f, 300f, 50f, true, true);
//cview.AnchorArea = new DevAge.Drawing.AnchorArea(DevAge.Drawing.ContentAlignment.TopCenter, true);
//DevAge.Drawing.Border border = new DevAge.Drawing.Border(Color.Red, 1);
//cview.Border = new DevAge.Drawing.RectangleBorder(border);//边框
//cview.Border = new DevAge.Drawing.RectangleBorder(DevAge.Drawing.Border.Black1Width);//边框
//模型
//SourceGrid.Cells.Models.ModelContainer models = new SourceGrid.Cells.Models.ModelContainer();
//编辑器
//SourceGrid.Cells.Editors.EditorBase editors = new SourceGrid.Cells.Editors.EditorBase(typeof(string));
//虚拟表格
//SourceGrid.Cells.Virtual.CellVirtual cellvirtral = new SourceGrid.Cells.Virtual.CellVirtual();
return cview;
}
private void clickEvent_Click(object sender, EventArgs e)
{
//SourceGrid.CellContext context = (SourceGrid.CellContext)sender;
//MessageBox.Show(this, context.Position.ToString());
}
private void mController_Link_Click(object sender, EventArgs e)
{
try
{
SourceGrid.CellContext context = (SourceGrid.CellContext)sender;
string ss = context.Position.Column.ToString();
string str = ((SourceGrid.Cells.Link)context.Cell).ToolTipText.ToString();
string str1 = ((SourceGrid.Cells.Link)context.Cell).Value.ToString();
if ((str == "") || (str == null))
{
return;
}
switch (ss)
{
case "4":
if (!Directory.Exists(path + str))
{
MessageBox.Show("图像路径不存在! " + path + str, "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
Photo photo = new Photo();
photo.filepath = str;
photo.ShowDialog();
photo.Dispose();
break;
case "7":
if (!Directory.Exists(path + str))
{
MessageBox.Show("图像路径不存在! " + path + str, "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
Photo photo2 = new Photo();
photo2.filepath = str;
photo2.ShowDialog();
photo2.Dispose();
break;
case "8":
if (!File.Exists(path + str))
{
MessageBox.Show("录像文件不存在! " + path + str, "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
DevAge.Shell.Utilities.OpenFile(path + str);
break;
default:
break;
}
}
catch (Exception op)
{
//MessageBox.Show(op.Message,"系统信息Doc");
}
}
private void button1_Click(object sender, EventArgs e)
{
//this.Text = this.grid1.Width.ToString()+" "+this.grid1.Height.ToString();
//Form2 form2 = new Form2();
//form2.Show();
}
access 数据库操作#region access 数据库操作
/**//// <summary>
/// 打开数据库,建立新的表和字段
/// </summary>
/// <param name="spath">数据库全路径</param>
/// <param name="dataname">表名</param>
/// <param name="items">字段数组</param>
private void newdatatable(string spath, string dataname, string[] items)
{
try
{
//连接到一个数据库
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + spath;
OleDbConnection myConn = new OleDbConnection(strCon);
myConn.Open();
// string strnew = " CREATE TABLE " + dataname + "( "
// + items[0] + " TEXT(100) CONSTRAINT PK_tblCustomers PRIMARY KEY , ";
string strnew = " CREATE TABLE [" + dataname + "] ( "
+ items[0] + " TEXT(255) , ";
for (int i = 1; i < items.Length - 1; i++)
{
strnew += items[i] + " TEXT(255) , ";
}
strnew += items[items.Length - 1] + " TEXT(255) )";
/**//*
string strnew = " CREATE TABLE "+ dataname +"("
+ " CustomerID INTEGER CONSTRAINT PK_tblCustomers PRIMARY KEY,"
+ " [Last Name] TEXT(50) NOT NULL,"
+ " [First Name] TEXT(50) NOT NULL,"
+ " Phone TEXT(10),"
+ " Email TEXT(50),"
+ " Address TEXT(40) DEFAULT Unknown)";
*/
OleDbCommand myCommand = new OleDbCommand(strnew, myConn);
myCommand.ExecuteNonQuery();
myConn.Close();
}
catch (Exception ed)
{
MessageBox.Show("新建表错误信息: " + ed.ToString(), "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/**//// <summary>
/// 打开数据库,建立新的表和字段
/// </summary>
/// <param name="spath">数据库全路径</param>
/// <param name="dataname">表名</param>
/// <param name="items">字段集合</param>
private void newdatatable(string spath, string dataname, ArrayList items)
{
try
{
//连接到一个数据库
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + spath;
OleDbConnection myConn = new OleDbConnection(strCon);
myConn.Open();
// string strnew = " CREATE TABLE [" + dataname + "] ( "
// + items[0] + " TEXT(100) CONSTRAINT PK_tblCustomers PRIMARY KEY , ";
string strnew = " CREATE TABLE [" + dataname + "] ( "
+ items[0] + " TEXT(255) , ";
for (int i = 1; i < items.Count - 1; i++)
{
strnew += items[i].ToString() + " TEXT(255) , ";
}
strnew += items[items.Count - 1].ToString() + " TEXT(255) )";
/**//*
string strnew = " CREATE TABLE "+ dataname +"("
+ " CustomerID INTEGER CONSTRAINT PK_tblCustomers PRIMARY KEY,"
+ " [Last Name] TEXT(50) NOT NULL,"
+ " [First Name] TEXT(50) NOT NULL,"
+ " Phone TEXT(10),"
+ " Email TEXT(50),"
+ " Address TEXT(40) DEFAULT Unknown)";
*/
OleDbCommand myCommand = new OleDbCommand(strnew, myConn);
myCommand.ExecuteNonQuery();
myConn.Close();
}
catch (Exception ed)
{
MessageBox.Show("新建表错误信息: " + ed.ToString(), "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/**//// <summary>
/// 删除数据库中的表,有则删除,无则忽略错误返回
/// </summary>
/// <param name="spath">数据库全名</param>
/// <param name="dataname">表名</param>
private void deletetable(string spath, string dataname)
{
try
{
//连接到一个数据库
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + spath;
OleDbConnection myConn = new OleDbConnection(strCon);
myConn.Open();
string strnew = " DROP TABLE " + dataname;
OleDbCommand myCommand = new OleDbCommand(strnew, myConn);
myCommand.ExecuteNonQuery();
myConn.Close();
}
catch// (Exception ed)
{
//MessageBox.Show("删除表错误信息: " + ed.ToString(), "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/**//// <summary>
/// 修改表的字段(有错误,sql语法错误)
/// </summary>
/// <param name="spath">数据库全路径</param>
/// <param name="dataname">表名</param>
/// <param name="s1">原字段名</param>
/// <param name="s2">新字段名</param>
private void rename_column(string spath, string dataname, string s1, string s2)
{
try
{
//连接到一个数据库
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + spath;
OleDbConnection myConn = new OleDbConnection(strCon);
myConn.Open();
string strnew = " ALTER TABLE " + dataname + " ALTER COLUMN " + s1 + " TO " + s2 + "";
OleDbCommand myCommand = new OleDbCommand(strnew, myConn);
myCommand.ExecuteNonQuery();
myConn.Close();
}
catch (Exception ed)
{
MessageBox.Show("修改字段错误信息: " + ed.ToString(), "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/**//// <summary>
/// 获取数据库中的表名(不用,有错误)
/// </summary>
/// <param name="spath">数据库全名</param>
private void gettablename2(string spath)
{
try
{
//连接到一个数据库
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + spath;
OleDbConnection myConn = new OleDbConnection(strCon);
myConn.Open();
string strnew = "SELECT MSysObjects.Name FROM MsysObjects WHERE (Left([Name],1)<>'~') AND (Left$([Name],4) <> 'Msys') AND (MSysObjects.Type)=1 ORDER BY MSysObjects.Name";
OleDbCommand myCommand = new OleDbCommand(strnew, myConn);
OleDbDataReader reader = myCommand.ExecuteReader();
while (reader.Read())
{
MessageBox.Show(reader.GetInt32(0) + ", " + reader.GetString(1));
}
reader.Close();
myConn.Close();
}
catch (Exception ed)
{
MessageBox.Show("删除表错误信息: " + ed.ToString(), "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/**//// <summary>
/// 打开指定的access数据库
/// </summary>
/// <param name="spath">access数据库名</param>
/// <param name="dataname">access数据库中的表名</param>
public bool readdata(string spath, string dataname)
{
try
{
//创建一个 OleDbConnection对象
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + spath;
OleDbConnection myConn = new OleDbConnection(strCon);
// string strCom = " SELECT * FROM " + dataname + " ORDER BY id";
string strCom = "SELECT [" + dataname + "].* FROM [" + dataname + "]";
//string strCom = " SELECT * FROM " + dataname;
//创建一个 DataSet对象
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
if (this.dataSet1.Tables.Contains(dataname))
{
this.dataSet1.Tables.Remove(dataname);
}
myCommand.Fill(this.dataSet1, dataname);
myConn.Close();
return true;
}
catch// (Exception e)
{
// MessageBox.Show("连接数据库发生错误:" + e.ToString(), "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
/**//// <summary>
/// 新增记录
/// </summary>
/// <param name="spath">数据库全名</param>
/// <param name="dataname">表名</param>
/// <param name="captions">字段名</param>
/// <param name="items">添加的纪录内容</param>
public void newdata(string spath, string dataname, string[] captions, object[] items)
{
try
{
//连接到一个数据库
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + spath;
OleDbConnection myConn = new OleDbConnection(strCon);
myConn.Open();
string strInsert;
int tt = captions.Length;
int sign = -1;//记录日期字段所在索引号,用来格式化日期格式(只要日期,不要时间)
strInsert = " INSERT INTO [" + dataname + "] ( "
+ captions[0] + " , ";
for (int i = 1; i < tt - 1; i++)
{
if (captions[i].Contains("日期"))
{
sign = i;
}
strInsert += captions[i] + " , ";
}
strInsert += captions[tt - 1] + " ) VALUES ( '";
for (int i = 0; i < tt - 1; i++)
{
if (i == sign)
{
string[] ss = items[i].ToString().Split(' ');
strInsert += ss[0] + "','";
}
else
{
strInsert += items[i].ToString() + "','";
}
}
strInsert += items[tt - 1].ToString() + "')";
/**//*
strInsert = " INSERT INTO " + dataname
+ " ( id,date,dirid,fileid,name,pid,gjjid,bgjjid,why,pages,oldid,bak ) VALUES ( ' "
+ items[0].ToString() + "' , '"
+ items[1].ToString() + "' , '"
+ items[2].ToString() + "' , '"
+ items[3].ToString() + "' , '"
+ items[4].ToString() + "' , '"
+ items[5].ToString() + "' , '"
+ items[6].ToString() + "' , '"
+ items[7].ToString() + "' , '"
+ items[8].ToString() + "' , '"
+ items[9].ToString() + "' , '"
+ items[10].ToString() + "' , '"
+ items[11].ToString() + " ')";
*/
OleDbCommand myCommand = new OleDbCommand(strInsert, myConn);
myCommand.ExecuteNonQuery();
myConn.Close();
}
catch (Exception ed)
{
MessageBox.Show("新增记录错误信息: " + ed.ToString(), "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/**//// <summary>
/// 搜索access数据库
/// </summary>
/// <param name="spath">access数据库名</param>
/// <param name="dataname">access数据库中的表名</param>
/// <param name="keyword">搜索关键字</param>
/// <param name="tablename">填充到dataset中的表名</param>
/// <returns></returns>
public bool searchdata(string spath, string dataname, string keyword, string tablename)
{
//string str = "";
bool yn = false;
//创建一个 OleDbConnection对象
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + spath;
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = "SELECT [" + dataname + "].* FROM [" + dataname + "] WHERE " + keyword;
try
{
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
if (this.dataSet1.Tables.Contains(tablename))
{
this.dataSet1.Tables.Remove(tablename);
}
myCommand.Fill(this.dataSet1, tablename);
yn = true;
}
catch (Exception e)
{
//MessageBox.Show(dataname + " 数据库中不包含搜索的字段!\n" + e.ToString(), "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(dataname + " 数据库中不包含搜索的字段!\n", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
myConn.Close();
}
return yn;
}
#endregion
private void button_head_Click(object sender, EventArgs e)
{
this.pageat = 1;
this.drawgird();
this.numer_n.Value = this.pageat;
this.textBox_n.Text = this.pages.ToString();
}
private void button_prv_Click(object sender, EventArgs e)
{
this.pageat--;
if (this.pageat < 1)
{
this.pageat = 1;
}
this.drawgird();
this.numer_n.Value = this.pageat;
this.textBox_n.Text = this.pages.ToString();
}
private void button_next_Click(object sender, EventArgs e)
{
this.pageat++;
if (this.pageat >this.pages)
{
this.pageat = this.pages;
}
this.drawgird();
this.numer_n.Value = this.pageat;
this.textBox_n.Text = this.pages.ToString();
}
private void button_end_Click(object sender, EventArgs e)
{
this.pageat = this.pages;
this.drawgird();
this.numer_n.Value = this.pageat;
this.textBox_n.Text = this.pages.ToString();
}
private void numer_n_ValueChanged(object sender, EventArgs e)
{
this.pageat = (int)this.numer_n.Value;
this.drawgird();
this.numer_n.Value = this.pageat;
this.textBox_n.Text = this.pages.ToString();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
About about = new About();
about.ShowDialog();
}
}
}