📄 mainform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace PrintDataGrid
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
// Declare dataBase variables
string cnStr, cmdText;
cnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source= Northwind.mdb";
OleDbConnection cn = new OleDbConnection(cnStr);
OleDbCommand cmd;
OleDbDataReader dr;
DataTable dt = new DataTable("Orders");
try
{
cn.Open();
// Load Data into DataGrid
cmdText = "SELECT * FROM Orders";
cmd = new OleDbCommand(cmdText, cn);
dr = cmd.ExecuteReader();
if (dr.HasRows) dt.Load(dr);
dr.Close();
dg.DataSource = dt;
// Define a DataGrid TableStyle and add it to DataGrid
DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = dt.TableName;
dg.TableStyles.Add(ts);
foreach (DataGridTextBoxColumn txtcol in ts.GridColumnStyles)
{
txtcol.NullText = "";
txtcol.ReadOnly = true;
}
ts.RowHeadersVisible = false;
ts.BackColor = Color.Yellow; // Odd rows
ts.AlternatingBackColor = Color.Yellow; // Even rows
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
cn.Close();
cn = null;
}
}
private void btnPrint_Click(object sender, EventArgs e)
{
// Calling Datagrid Printing
PrintDG.Print_DataGrid(dg);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -