ASP. NET GridView manually change the number of rows

Source: Internet
Author: User

 

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 ();
// Set the number of lines displayed on each page
Int TotalRowCount = 12;
// Number of automatically filled rows
Int numCount = 0;

Protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
This.txt CartonQty. Focus ();
ViewState ["SortOrder"] = "ctn no ";
ViewState ["OrderDire"] = "ASC ";

}
}

DataTable dt = new DataTable ();
String CartonSN = string. Empty;
String _ CartonSN = string. Empty;


// Calculate the data, which can be obtained from the database by modifying the data here
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)
{
// Calculate the number of automatically filled rows
NumCount ++;
}
If (e. Row. RowType = DataControlRowType. Footer)
{
// Add the missing row here
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;
}


If (e. Row. RowIndex! =-1)
{
Int id = e. Row. RowIndex + 1;
E. Row. Cells [0]. Text = id. ToString ();
}
}


}

Allen Chen's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.