using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Example
{
public partial class RaxDataGridView : DataGridView
{
public RaxDataGridView()
{
InitializeComponent();
}
private DataSet ds = new DataSet();
private int intToltalPage = 1;
private int intNowPage = 1;
private int intOnePageRow;
private int intNowRowNo = 0;
[Category("Rax")]
public int PageSize
{
get
{
return intOnePageRow;
}
set
{
if (value > 0)
{
intOnePageRow = value;
}
}
}
public int CurrentPage
{
get
{
return intNowPage;
}
}
public int Toltal_Page
{
get
{
return intToltalPage;
}
}
public void RaxDataGridView_DataSourceChanged(Object sender, System.EventArgs e)
{
DataGridView dataGridView = (DataGridView)sender;
DataTable dtl = (DataTable)dataGridView.DataSource;
if (dtl.Rows.Count > 0)
{
intToltalPage = 1;
intNowPage = 1;
if (PageSize != 0)
{
int intTotal = dtl.Rows.Count;
int intTempPage = intTotal / PageSize;
if (intTotal % PageSize > 0)
{
intTempPage++;
}
//if (intTempPage * PageSize > intTotal)
//{
// intToltalPage = intTempPage;
//}
//else
//{
// intToltalPage = intTempPage + 1;
//}
intToltalPage = intTempPage;
intNowRowNo = 0;
ds.Tables.Clear();
for (int i = 0; i < intToltalPage; i++)
{
DataTable tempTable = new DataTable();
for (int j = 0; j < dtl.Columns.Count; j++)
{
DataColumn dcol = new DataColumn();
dcol.ColumnName = dtl.Columns[j].ColumnName;
tempTable.Columns.Add(dcol);
}
for (int s = 0; s < PageSize; s++)
{
if (intNowRowNo != dtl.Rows.Count)
{
tempTable.ImportRow(dtl.Rows[intNowRowNo]);
intNowRowNo++;
}
}
ds.Tables.Add(tempTable);
}
if (ds.Tables.Count > 0)
{
dtl.Rows.Clear();
for (int n = 0; n < PageSize; n++)
{
dtl.ImportRow(ds.Tables[0].Rows[n]);
}
}
}
}
dataGridView.DataSource = dtl;
}
public void ToNext()
{
DataTable dtl = (DataTable)this.DataSource;
if (this.DataSource == null)
{
return;
}
if (intNowPage == intToltalPage)
{
//MessageBox.Show("提示", "已经到最后一页", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
dtl.Rows.Clear();
for (int i = 0; i < ds.Tables[intNowPage].Rows.Count; i++)
{
dtl.ImportRow(ds.Tables[intNowPage].Rows[i]);
}
intNowPage++;
}
this.DataSource = dtl;
}
public void ToUp()
{
DataTable dtl = (DataTable)this.DataSource;
if (this.DataSource == null)
{
return;
}
if (intNowPage == 1)
{
//MessageBox.Show("提示", "已经到第一页", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
dtl.Rows.Clear();
for (int i = 0; i < ds.Tables[intNowPage - 2].Rows.Count; i++)
{
dtl.ImportRow(ds.Tables[intNowPage - 2].Rows[i]);
}
intNowPage--;
}
this.DataSource = dtl;
}
public void ToFirst()
{
DataTable dtl = (DataTable)this.DataSource;
if (this.DataSource == null)
{
return;
}
if (intNowPage == 1)
{
//MessageBox.Show("提示", "已经到第一页", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
dtl.Rows.Clear();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dtl.ImportRow(ds.Tables[0].Rows[i]);
}
intNowPage = 1;
}
this.DataSource = dtl;
}
public void ToLast()
{
DataTable dtl = (DataTable)this.DataSource;
if (this.DataSource == null)
{
return;
}
if (intNowPage == intToltalPage)
{
///MessageBox.Show("提示", "已经到最后一页", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
dtl.Rows.Clear();
for (int i = 0; i < ds.Tables[intToltalPage - 1].Rows.Count; i++)
{
dtl.ImportRow(ds.Tables[intToltalPage - 1].Rows[i]);
}
intNowPage = intToltalPage;
}
this.DataSource = dtl;
}
public enum BottomAlign { left, right, center }
private BottomAlign _BottomAlignment = BottomAlign.left;
/// <summary>
/// 获取或设置统计结果显示位置
/// </summary>
[Category("Rax")]
public BottomAlign BottomAlignment
{
get
{
return _BottomAlignment;
}
set
{
_BottomAlignment = value;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g;
if (e == null)
{
g = Graphics.FromHwnd(this.Handle);
}
else
{
g = e.Graphics;
}
SolidBrush myBrush1 = new SolidBrush(SystemColors.Control);
SolidBrush myBrush2 = new SolidBrush(Color.DarkGray);
Pen pen1 = new Pen(Brushes.White, 1);
if (this.Rows.Count > 0 && this.Rows[this.Rows.Count - 1].Displayed)
{
int LocY = this.GetRowDisplayRectangle(this.Rows.Count - 1, true).Location.Y + this.Rows[this.Rows.Count - 1].Height;
//draw caption
g.FillRectangle(myBrush1, 2, LocY, this.RowHeadersWidth - 2, 23);
//caption's top line
g.DrawLine(pen1, new Point(2, LocY), new Point(this.RowHeadersWidth - 1, LocY));
// caption's left line
g.DrawLine(pen1, new Point(2, LocY), new Point(2, LocY + 23));
//draw cells
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Near;
int cellLocX = this.RowHeadersWidth + 1;
int i = this.Columns.Count;
for (int j = 0; j < i; j++)
{
if (this.Columns[j].Displayed)
{
int cellTextLocX = cellLocX;
g.FillRectangle(myBrush2, cellLocX, LocY, this.GetColumnDisplayRectangle(j, false).Width - 1, 23);
#region DrawString
if (this.GetColumnDisplayRectangle(j, false).Width == this.Columns[j].Width)
{
int cellWidth = this.GetColumnDisplayRectangle(j, false).Width;
try
{
Type type = this.Columns[j].ValueType;
string strFmValue = "";
decimal decValue = 0m;
if (type == typeof(decimal))
{
for (int l = 0; l < this.Rows.Count; l++)
{
if (this.Rows[l].Cells[j].Value != null && !this.Rows[l].Cells[j].Value.ToString().Equals(""))
{
decValue += decimal.Parse(this.Rows[l].Cells[j].Value.ToString());
}
}
//strFmValue = this.Columns[j].DataPropertyName + "列相关值";
strFmValue = decValue.ToString();
}
switch (this.BottomAlignment)
{
case BottomAlign.right:
cellTextLocX = cellTextLocX + cellWidth - (int)g.MeasureString(strFmValue, new Font(new FontFamily("Verdana"), 8), cellWidth).Width;
break;
case BottomAlign.center:
cellTextLocX = cellTextLocX + cellWidth / 2 - (int)(g.MeasureString(strFmValue, new Font(new FontFamily("Verdana"), 8), cellWidth).Width / 2);
break;
}
g.DrawString(strFmValue, new Font(new FontFamily("Verdana"), 8), Brushes.Black, cellTextLocX, LocY + 6, sf);
}
catch
{
g.DrawString("", new Font(new FontFamily("Verdana"), 8), Brushes.Black, cellTextLocX, LocY + 6, sf);
}
}
#endregion
cellLocX += this.GetColumnDisplayRectangle(j, false).Width;
}
}
// draw caption string
g.DrawString("合计", new Font(new FontFamily("Verdana"), 8), Brushes.Black, 4, LocY + 6, sf);
}
}
}
}