Winfrom 基本处于忘光的阶段.先需要做个winfrom 的软件.然后自己扩展了DataGridView带分页的控件。废话不多说 上图先  

现在一步步实现其效果.

1.添加用户控件 上图即可知道布局以及怎么实现

2.代码如下

  #region 控件属性

        /// <summary>
/// 页大小
/// </summary>
public int PageSize
{
get;
set;
} /// <summary>
/// 总记录数
/// </summary>
public int DataSoureCount
{
get;
set;
} /// <summary>
/// 总页数
/// </summary>
public int PageCount
{
get;
set;
} /// <summary>
/// 当前记录行
/// </summary>
public int CurrentPageIndex
{
get;
set;
} /// <summary>
/// 取得或者设置数据控件的自定义列名
/// </summary>
public List<string> gdvColHeaderText
{
get;
set;
} /// <summary>
/// 取得或者设置绑定控件的数据集
/// </summary>
public DataTable GetGridDataSoure
{
get;
set;
} /// <summary>
/// 绑定数据到控件中
/// </summary>
public void DataBaind()
{
dgv.DataSource = GetGridDataSoure;
lbSoureCount.Text = DataSoureCount.ToString();
lbPageCount.Text = this.PageCount.ToString();
lbPageSize.Text = this.PageSize.ToString();
lbCurrentPageIndex.Text = this.CurrentPageIndex.ToString();
DataGridViewStyle(dgv);
if (gdvColHeaderText != null)
{
for (int i = ; i < gdvColHeaderText.Count; i++)
{
if (dgv.Columns.Count >= i)
{
dgv.Columns[i].HeaderText = gdvColHeaderText[i];
}
}
} if (IsVisibleFirstCom)
{
dgv.Columns[].Visible = false;
}
}
//新增一列填充空余单元格
bool tc_column = true; /// <summary>
/// DataGridView样式处理
/// </summary>
/// <param name="gridview">DataGridView控件</param>
/// <param name="tc_column">是否需要添加列</param>
public void DataGridViewStyle(DataGridView gridview)
{ System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;//211, 223, 240
dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Navy;
dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
gridview.AllowUserToAddRows = false;
gridview.AllowUserToDeleteRows = false;
dataGridViewCellStyle1.BackColor = System.Drawing.Color.LightCyan;
Form f = new System.Windows.Forms.Form();
gridview.AutoGenerateColumns = false; gridview.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;
gridview.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
gridview.EnableHeadersVisualStyles = false;
gridview.GridColor = System.Drawing.SystemColors.GradientInactiveCaption;
gridview.ReadOnly = true;
//gridview.RowHeadersVisible = false;
gridview.RowTemplate.Height = ;
gridview.RowTemplate.ReadOnly = false;
//标题文字居中
gridview.ColumnHeadersDefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; //gridview.RowHeadersVisible = false;
gridview.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
gridview.ColumnHeadersHeight = ; //设置选择单元模式
gridview.SelectionMode = DataGridViewSelectionMode.FullRowSelect; if (tc_column)
{
//设置默认填充列自动填充
DataGridViewTextBoxColumn TextBoxColumn = new DataGridViewTextBoxColumn();
TextBoxColumn.Name = "";
TextBoxColumn.HeaderText = "";
gridview.Columns.Insert(gridview.ColumnCount, TextBoxColumn);
TextBoxColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
//取消该列排序
TextBoxColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
tc_column = false;
} //点击立即进入编辑模式
gridview.EditMode = DataGridViewEditMode.EditOnEnter;
} #endregion #region 分页按钮事件 /// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFirstPage_Click(object sender, EventArgs e)
{
this.CurrentPageIndex = ;
PageChange_Click(sender, e);
//按钮状态
btnFirstPage.Enabled = false;
btnLastPage.Enabled = true;
btnNextPage.Enabled = true;
btnPrevPage.Enabled = false;
} /// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrevPage_Click(object sender, EventArgs e)
{
if (this.CurrentPageIndex >= )
{
btnPrevPage.Enabled = true;
btnLastPage.Enabled = true;
btnNextPage.Enabled = true;
btnPrevPage.Enabled = true;
this.CurrentPageIndex -= ;
PageChange_Click(sender, e);
}
else
{
btnPrevPage.Enabled = false;
btnFirstPage.Enabled = false;
}
} /// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnNextPage_Click(object sender, EventArgs e)
{
if (this.CurrentPageIndex < this.PageCount)
{
btnFirstPage.Enabled = true;
btnLastPage.Enabled = true;
btnNextPage.Enabled = true;
btnPrevPage.Enabled = true;
this.CurrentPageIndex += ;
PageChange_Click(sender, e);
}
else
{
btnNextPage.Enabled = false;
btnLastPage.Enabled = false;
}
} /// <summary>
/// 尾页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLastPage_Click(object sender, EventArgs e)
{
if (this.CurrentPageIndex < this.PageCount)
{
btnLastPage.Enabled = false;
this.CurrentPageIndex = PageCount;
PageChange_Click(sender, e);
btnFirstPage.Enabled = true;
btnNextPage.Enabled = false;
btnPrevPage.Enabled = true;
}
else
{
btnLastPage.Enabled = false;
btnNextPage.Enabled = false;
}
} #endregion /// <summary>
/// 页改变的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public delegate void PageChange(); /// <summary>
/// 页改变的委托事件
/// </summary>
public event EventHandler PageChange_Click; /// <summary>
/// 点击行的事件
/// </summary>
public delegate void clickRow(); /// <summary>
/// 点击行的委托事件
/// </summary>
public event EventHandler dgvRows_Click; /// <summary>
/// 单一值
/// </summary>
public string singleValue
{
get;
set;
} /// <summary>
/// 多值
/// </summary>
public string manyValue
{
get;
set;
} /// <summary>
/// 设置需要取的单一值
/// </summary>
public string setSingleValue
{
get;
set;
} /// <summary>
/// 设置需要取的多值
/// </summary>
public string setManyValue
{
get;
set;
} /// <summary>
/// 设置默认单元格选择行事件为单击 如果为单击则为ture 双击为false
/// </summary>
protected bool tableCheck
{
get
{
bool isSinlgOrDoubleClick=true;
ParameterModel tableCheckModel=new ParameterBLL().GetModelByID(, "");
if (tableCheckModel == null)
return isSinlgOrDoubleClick;
else
isSinlgOrDoubleClick=tableCheckModel.fParValue.ToUpper() == "Y" ? true : false;
return isSinlgOrDoubleClick;
}
} /// <summary>
/// 双击单元格事件发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//如果系统设置的为单击 则不促发双击
if (tableCheck == true) return;
if (e.RowIndex <= -) return;
if (!string.IsNullOrEmpty(setSingleValue))
{
singleValue = dgv.Rows[e.RowIndex].Cells[setSingleValue].Value.ToString();
}
else
{
string[] keyItem = setManyValue.Split(',');
string Values = string.Empty;
foreach (string s in keyItem)
{
Values += dgv.Rows[e.RowIndex].Cells[s].Value.ToString() + ",";
}
Values = Values.Remove(Values.LastIndexOf(','));
manyValue = Values;
}
dgvRows_Click(sender, e);
} /// <summary>
/// 是否隐藏首列的值
/// </summary>
public bool IsVisibleFirstCom
{
get;
set;
} /// <summary>
/// 单击单元格事件发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
//如果系统设置的为双击 则不促发单击
if (tableCheck == false) return;
if (e.RowIndex <= -) return;
if (!string.IsNullOrEmpty(setSingleValue))
{
singleValue = dgv.Rows[e.RowIndex].Cells[setSingleValue].Value.ToString();
}
else
{
string[] keyItem = setManyValue.Split(',');
string Values = string.Empty;
foreach (string s in keyItem)
{
Values += dgv.Rows[e.RowIndex].Cells[s].Value.ToString() + ",";
}
manyValue = Values.Remove(Values.LastIndexOf(','));
}
dgvRows_Click(sender, e);
}

3.如何使用

在页面在拖入控件.填充数据如下

 string sqlWhere = " where 1=1 and fState in(0,1) ";

            if (!string.IsNullOrEmpty(txtArtNo.Text))
{
sqlWhere += string.Format(" and fArtNo like '%{0}%'", txtArtNo.Text);
}
if (!string.IsNullOrEmpty(txtBarCode.Text))
{
sqlWhere += string.Format(" and fBarCode like '%{0}%'", txtBarCode.Text);
}
if (!string.IsNullOrEmpty(txtItemName.Text))
{
sqlWhere += string.Format(" and fItemName like '%{0}%'", txtItemName.Text);
}
if (ddlBigClassID.SelectedValue.ToString() != "-100")
{
sqlWhere += string.Format(" and fBigClassID = '{0}'", ddlBigClassID.SelectedValue);
}
if (ddlMidClassID.SelectedValue != null && ddlMidClassID.SelectedValue .ToString()!= "-100")
{
sqlWhere += string.Format(" and fMidClassID = '{0}'", ddlMidClassID.SelectedValue);
}
if (ddlSubClassID.SelectedValue != null && ddlSubClassID.SelectedValue.ToString() != "-100")
{
sqlWhere += string.Format(" and fSubClassID = '{0}'", ddlSubClassID.SelectedValue);
} ItemBaseBLL bll = new ItemBaseBLL();
fgivItem.PageSize = ;
int CurrentPageIndex = fgivItem.CurrentPageIndex == ? fgivItem.CurrentPageIndex += : fgivItem.CurrentPageIndex += ;
fgivItem.CurrentPageIndex = CurrentPageIndex;
fgivItem.GetGridDataSoure = bll.GetDateSoure(sqlWhere, "fItemID", "fItemID,fBigClassName,fMidClassName,fSubClassName,fItemName,fArtNo,fBarCode,fImportPrice,fSalePrice,fPrice1,fInVeryPrice,fCostPrice,fStateText", fgivItem.PageSize, fgivItem.CurrentPageIndex);
int totalCount = bll.GetCount(sqlWhere);
fgivItem.DataSoureCount = totalCount;
int totalPageNum = (totalCount % fgivItem.PageSize) > ? ((totalCount / fgivItem.PageSize) + ) : (totalCount / fgivItem.PageSize);
fgivItem.gdvColHeaderText = new List<string> {"商品编号", "商品大类", "商品中类", "商品小类", "商品名称", "商品货号", "商品条码", "进价", "售价", "活动价", "换购价", "成本价", "状态" };
fgivItem.PageCount = totalPageNum;
fgivItem.setSingleValue = "fItemID";
fgivItem.IsVisibleFirstCom = true;
fgivItem.DataBaind();

winfrom dataGridView 自定义分页实现的更多相关文章

  1. C# DataGridView自定义分页控件

    好些日子不仔细写C#代码了,现在主要是Java项目,C#.Net相关项目不多了,有点手生了,以下代码不足之处望各位提出建议和批评. 近日闲来无事想研究一下自定义控件,虽然之前也看过,那也仅限于皮毛,粗 ...

  2. winform中DataGridView实现分页功能

    WinForm轻松实现自定义分页 (转载) WinForm轻松实现自定义分页 (转载)   转载至http://xuzhihong1987.blog.163.com/blog/static/26731 ...

  3. asp.net webform 自定义分页控件

    做web开发一直用到分页控件,自己也动手实现了个,使用用户自定义控件. 翻页后数据加载使用委托,将具体实现放在在使用分页控件的页面进行注册. 有图有真相,给个直观的认识: 自定义分页控件前台代码: & ...

  4. Python之路【第十九篇】自定义分页实现(模块化)

    自定义分页 1.目的&环境准备 目的把分页写成一个模块的方式然后在需要分页的地方直接调用模块就行了. 环境准备Django中生成一个APP并且注册,配置URL&Views 配置URL ...

  5. MVC下分页的自定义分页一种实现

    1.引言 在MVC开发中我们经常会对数据进行分页的展示.通过分页我们可以从服务端获取指定的数据来进行展示.这样既节约了数据库查询的时间也节约了网络传输的数据量.在MVC开发中使用的比较多的应该是MVC ...

  6. Django自定义分页、bottle、Flask

    一.使用django实现之定义分页 1.自定义分页在django模板语言中,通过a标签实现; 2.前段a标签使用<a href="/user_list/?page=1"> ...

  7. Mvc自定义分页控件

    MVC开发分页常常使用第三方控件,生成的分页HTML带有版权申明,虽然免费,但是总有的别扭.于是,某日,楼主闲来蛋疼,折腾了个自定义分页控件: 先来展示下效果图: 1>当分页不超过10页的时候, ...

  8. MVC自定义分页

    MVC自定义分页 之前我发表了一篇MVC无刷新分页的文章,里面用的是MvcPager控件,但是那个受那个控件限制,传值只能用PagedList,各方面都受到了限制,自由度不够高,现在还是做MVC无刷新 ...

  9. PHPCMS快速建站系列之自定义分页函数

    内容分页的实现方法:{pc:content action="lists" catid="$catid" order="id DESC" nu ...

随机推荐

  1. hdoj 2202 最大三角形

    题目大意:给定n(3<=n<=50000)个点,求其中任意三个点组成的三角形面积最大,输出该面积. 题目传送:http://acm.hdu.edu.cn/showproblem.php?p ...

  2. .net中的认证(authentication)与授权(authorization)

    “认证”与“授权”是几乎所有系统中都会涉及的概念,通俗点讲: 1.认证(authentication) 就是 "判断用户有没有登录?",好比windows系统,没登录就无法使用(不 ...

  3. the-type-java-lang-charsequence-cannot-be-resolved-in-package-declaration

    http://stackoverflow.com/questions/24301986/the-type-java-lang-charsequence-cannot-be-resolved-in-pa ...

  4. android 输入法的打开和关闭

    一.打开输入法窗口: InputMethodManager inputMethodManager = (InputMethodManager)  getSystemService(Context.IN ...

  5. IOS时间格式转换

    在开发iOS程序时,有时候需要将时间格式调整成自己希望的格式,这个时候我们可以用NSDateFormatter类来处理. 例如:如何将格式为“12-May-14 05.08.02.000000 PM” ...

  6. http://jingyan.baidu.com/article/08b6a591f0fafc14a9092275.html

    http://jingyan.baidu.com/article/08b6a591f0fafc14a9092275.html http://jingyan.baidu.com/article/414e ...

  7. lintcode:Add Binary 二进制求和

    题目: 二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 样例 a = 11 b = 1 返回 100 解题: 和求两个链表的和很类似 考虑进位,考虑最后一项的进位 0+0 = 0 不 ...

  8. Android getActionBar()报空指针异常

    1. 加载完视图后,再去获取: 写在setContentView()后面. 2.sdk版本: Actionbar的主题在3.0以后才有,使用的时候要确保,最低的版本不能小于3.0. <uses- ...

  9. js中鼠标滚轮事件详解

    js中鼠标滚轮事件详解   (以下内容部分内容参考了http://adomas.org/javascript-mouse-wheel/ ) 之前js 仿Photoshop鼠标滚轮控制输入框取值中已使用 ...

  10. 使用phantomjs实现highcharts等报表通过邮件发送

    使用phantomjs实现highcharts等报表通过邮件发送(本文仅提供完整解决方案和实现思路,完全照搬不去整理代码无法马上得到效果)   前不久项目组需要将测试相关的质量数据通过每日自动生成报表 ...