当前位置:编程学习 > asp >>

ASP.NET GridView手动改变行数

\\

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Drawing;

public partial class PackingInfo : System.Web.UI.Page
{
    ReportsManager rm = new ReportsManager();
    // 设置每页显示的行数 
    int TotalRowCount = 12;
    // 自动填充的行数
    int numCount = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.txtCartonQty.Focus();
            ViewState["SortOrder"] = "CTN NO";
            ViewState["OrderDire"] = "ASC";

        }   
    }

    DataTable dt = new DataTable();
    string CartonSN = string.Empty;
    string _CartonSN = string.Empty;


    // 计算数据,这里可以适当修改从数据库中获取
    ICollection CreateDataSource(int RowsCount)
    {
        System.Data.DataTable dt = new System.Data.DataTable();
        System.Data.DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("P/N", typeof(System.String)));
        dt.Columns.Add(new System.Data.DataColumn("CTN NO", typeof(System.String)));
        dt.Columns.Add(new System.Data.DataColumn("CustomerSN", typeof(System.Decimal)));

        for (int i = 0; i < RowsCount; i++)
        {
            dr = dt.NewRow();
          
            dt.Rows.Add(dr);
        }
        System.Data.DataView dv = new System.Data.DataView(dt);
        return dv;
    }


    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
        }
        int m;
        for (m = 0; m < gv.Rows.Count; m++)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor,this.style.backgroundColor='#00A9FF'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }

        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // 计算自动填充的行数
                numCount++;
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                //在此添加缺少的行
                int toLeft = TotalRowCount - numCount;
                int numCols = gv.Rows[0].Cells.Count;

                for (int i = 0; i < toLeft; i++)
                {
                    GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
                    for (int j = 0; j < numCols; j++)
                    {
                        TableCell cell = new TableCell();
                        cell.Text = " ";
                        row.Cells.Add(cell);
                    }
                    gv.Controls[0].Controls.AddAt(numCount + 1 + i, row);
                }
            }
        }
        catch (Exception ex)
        {
            this.lblMessage.Text = ex.Message;
            this.lblMessage.ForeColor = Color.Red;补充:Web开发 , ASP.Net ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,