asp.net the GridView control Usage Method Encyclopedia _ Practical Tips

Source: Internet
Author: User

Front desk. aspx

Copy Code code as follows:

<asp:label id= "TPLB" runat= "Server" text= "total Pages:" ></asp:Label>
<asp:label id= "Lblpagecount" runat= "Server" text= "" ></asp:Label>
<asp:label id= "Curlabel" runat= "Server" text= "Current page:" ></asp:Label>
<asp:label id= "Lblpage" text= "1" runat= "Server" ></asp:Label>
<asp:linkbutton id= "Lblfirstbutton" runat= "Server" onclick= "Lblfirstbutton_click" >|<</asp:linkbutton >
<asp:linkbutton id= "Lblprebutton" runat= "Server" onclick= "Lblprebutton_click" ><</asp:LinkButton>
<asp:linkbutton id= "Lblnextbutton" runat= "Server" onclick= "Lblnextbutton_click" >></asp:LinkButton>
<asp:linkbutton id= "Lbllastbutton" runat= "Server" onclick= "Lbllastbutton_click" >>|</asp:linkbutton >
<asp:dropdownlist id= "Ddlpage" runat= "Server" width= "40px" autopostback= "True"
onselectedindexchanged= "Ddlpage_selectedindexchanged" >
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>15</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
</asp:DropDownList>
<asp:label id= "Pagesizelabel" runat= "server" text= "Strip/page" ></asp:Label>

Background
Copy Code code as follows:

#region分页
protected void Bindfollowexaminfogridview (int PersonID)
{
int currentpage = Convert.ToInt32 (Lblpage.text);
DataTable dt = new DataTable ();
DT = FEIBF.  Getbypersonidfollowexaminfo (PersonID); Search for a designated person's follow-up information record
if (dt. Rows.Count > 0)
{
Followexaminfogridview.datasource = DT;
Followexaminfogridview.databind ();
PagedDataSource PS = new PagedDataSource ();
Ps. DataSource = dt. DefaultView;
Ps. AllowPaging = true;
Ps. PageSize = Convert.ToInt32 (Ddlpage.selectedvalue);
Lblpagecount.text = PS. Pagecount.tostring ();
This.lblPreButton.Enabled = true;
This.lblNextButton.Enabled = true;
Ps. CurrentPageIndex = currentpage-1;
if (currentpage = 1)
{
this.lblPreButton.Enabled = false;
this.lblFirstButton.Enabled = false;
}
Else
{
This.lblPreButton.Enabled = true;
This.lblFirstButton.Enabled = true;
}
if (currentpage = PS. PageCount)
{
this.lblNextButton.Enabled = false;
this.lblLastButton.Enabled = false;
}
Else
{
This.lblNextButton.Enabled = true;
This.lblLastButton.Enabled = true;
}
Followexaminfogridview.datasource = PS;
Followexaminfogridview.databind ();
}
    
}
protected void Lblprebutton_click (object sender, EventArgs e)
{
This.lblPage.Text = convert.tostring (Convert.touint32 (Lblpage.text)-1);
Bindfollowexaminfogridview (Convert.ToInt32 (request.querystring["PersonID"));
}
protected void Lblnextbutton_click (object sender, EventArgs e)
{
This.lblPage.Text = convert.tostring (Convert.touint32 (lblpage.text) + 1);
Bindfollowexaminfogridview (Convert.ToInt32 (request.querystring["PersonID"));
}
protected void Lblfirstbutton_click (object sender, EventArgs e)
{
This.lblPage.Text = "1";
Bindfollowexaminfogridview (Convert.ToInt32 (request.querystring["PersonID"));
}
protected void Lbllastbutton_click (object sender, EventArgs e)
{
This.lblPage.Text = Lblpagecount.text;
Bindfollowexaminfogridview (Convert.ToInt32 (request.querystring["PersonID"));
}
protected void Ddlpage_selectedindexchanged (object sender, EventArgs e)
{
Lblpage.text = "1";
Bindfollowexaminfogridview (Convert.ToInt32 (request.querystring["PersonID"));
}
#endregion

Sort
Allowsort = "true"
SortExpression = "ID"
DataView dv = sortbindgrid (DT);
#region排序
protected void Followexaminfogridview_sorting (object sender, GridViewSortEventArgs e)
{
viewstate["SortExpression"] = e.sortexpression;
if (viewstate["sortdirection"] = = null)
{
viewstate["sortdirection"] = "ASC";
}
Else
{
if (viewstate["SortDirection"]. ToString () = "ASC")
{
viewstate["sortdirection"] = "desc";
}
Else
{
viewstate["sortdirection"] = "ASC";
}
}
   
Bindfollowexaminfogridview (Convert.ToInt32 (Hiddenpersonid.value));
}
Public DataView Sortbindgrid (DataTable table)
{
if (table!= null)
{
DataView dv = table. DefaultView;
if (viewstate["SortExpression"]!= null && viewstate["sortdirection"]!= null)
{
Dv. Sort = viewstate["SortExpression"]. ToString () + "" + viewstate["SortDirection"]. ToString ();
}
return DV;
}
Else
{
return null;
}
}
#endregion

======= self-with pagination
#region自带分页

protected void Followexaminfogridview_pageindexchanging (object sender, Gridviewpageeventargs e)
{
Followexaminfogridview.pageindex = E.newpageindex;
Bindfollowexaminfogridview (Convert.ToInt32 (Hiddenpersonid.value));
}
#endregion


Implementation of selected Grid View
Copy Code code as follows:

#region实现选中行
<selectedrowstyle backcolor= "AliceBlue" forecolor= "Gray"/>
<asp:commandfield showselectbutton= "True"/>
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
E.row.attributes.add ("onclick", "This.cells[0].childnodes[0].click ()");
}
protected void Gridviewregiment_selectedindexchanged (object sender, EventArgs e)
{
GridViewRow row = Gridviewregiment.selectedrow;
int regimentid = Convert.ToInt32 (row. CELLS[1]. Text);
Response.Redirect ("Updateregimentation.aspx?") Regimentid= "+ Regimentid);
}
#endregion

Display color and delete
Copy Code code as follows:

protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
int i;
for (i = 0; i < GridViewRegiment.Rows.Count; i++)
//{
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
When there is an edit column, avoid error, the RowState judgment to add
if (e.row.rowstate = = Datacontrolrowstate.normal | | e.row.rowstate = = datacontrolrowstate.alternate)
{
((ImageButton) e.row.cells[2]. FindControl ("Ibtndelete")). Attributes.Add ("onclick", "Javascript:return Confirm" (' You are sure to delete: "" + e.row.cells[0]. Text + ""? ') ");
}
E.row.attributes.add ("onmouseover", "This.style.backgroundcolor= ' #00A9FF");
E.row.attributes.add ("onmouseout", "This.style.backgroundcolor= ' #E6F5FA");
}
//}
}

Processing of the GridView null
1 shows empty records without headers, emptydatatext= "no Records"
2 Display the blank record of the header
Copy Code code as follows:

DataTable dt = new DataTable ();
DT = FEIBF.  Getbypersonidfollowexaminfo (PersonID); Search for a designated person's follow-up information record
DataView dv = sortbindgrid (DT);
if (dt. Rows.Count > 0)
{
Followexaminfogridview.datasource = DV;
Followexaminfogridview.databind ();
}
Else
{
Add new row Display header
Dt. Rows.Add (dt. NewRow ());
Followexaminfogridview.datasource = DT;
Followexaminfogridview.databind ();
Working with new rows
int columnCount = Followexaminfogridview.rows[0]. Cells.count;
Clears all cells from the empty row
Followexaminfogridview.rows[0]. Cells.clear ();
New Cell Object
Followexaminfogridview.rows[0]. Cells.add (new TableCell ());
Merging cells
Followexaminfogridview.rows[0]. Cells[0]. ColumnSpan = ColumnCount;
Set cell hint content
Followexaminfogridview.rows[0]. Cells[0]. Style.value = "Text-align:center";
Followexaminfogridview.rows[0]. Cells[0]. Text = "This person has no follow-up information";
}

The derivation of the GridView
Enableeventvalidation= "false"
Copy Code code as follows:

#region导出
public override void Verifyrenderinginserverform (Control control)
{
}
protected void Btnprint_click (object sender, EventArgs e)
{
Response.Clear ();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.appendheader ("Content-disposition", "Attachment;filename=filename.xls");
If set to GetEncoding ("GB2312"), the exported file will appear garbled!!!
response.contentencoding = System.Text.Encoding.UTF7;
Response.ContentType = "Application/ms-excel";//Set Output file type to excel file.
System.IO.StringWriter ostringwriter = new System.IO.StringWriter ();
System.Web.UI.HtmlTextWriter ohtmltextwriter = new System.Web.UI.HtmlTextWriter (ostringwriter);
This. Affichegv.rendercontrol (Ohtmltextwriter);
Response.Output.Write (Ostringwriter.tostring ());
Response.Flush ();
Response.End ();
}
#endregion

Display of ToolTip GridView details
Front desk
<script type= "Text/javascript" >
function Tooltip (CELLA,CELLB)
{
document.getElementById ("DC"). InnerText = "Details:" +CELLB;
document.getElementById ("id"). innertext = "ID:" +cella;
X= Event.clientx+document.body.scrollleft;
y=event.clienty+document.body.scrolltop+20;
tooltiplayer.style.display= "inline";
Tooltiplayer.style.left=x;
Tooltiplayer.style.top=y;
}
</script>
<div id= "Tooltiplayer" style= "Position:absolute; Display:none;
Background-color:aqua; Border-color:blue; Border-style:solid;
Border-color:blue; border-width:1px; ">
<table>
<tr><td>Affiche</td></tr>
<TR><TD id = "DC" ></td></tr>
<TR><TD ID = "id" > </td></tr>
</table>
</div>


Background
Copy Code code as follows:

protected void Affichegv_rowdatabound (object sender, GridViewRowEventArgs e)
{
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
if (e.row.rowstate = = Datacontrolrowstate.normal | | e.row.rowstate = = datacontrolrowstate.alternate)
{
1 E.row.attributes.add ("onmouseover", "Tooltip" ("+e.row.cells[0"). Text.tostring () + "', '" +e.row.cells[1]. Text.tostring () + "')");
2 E.row.attributes.add ("onmouseover", "Javascript:tooltip" (' e.row.cells[0]. Text ');
3 E.row.attributes.add ("onmouseover", "Tooltip" (' e.row.cells[0]. Text ') ");
} }
}

#region自带编辑
protected void Gvaffiche_rowediting (object sender, GridViewEditEventArgs e)
{
Gvaffiche.editindex = E.neweditindex;
Bindgvaffiche ();
}
protected void Gvaffiche_rowdeleting (object sender, Gridviewdeleteeventargs e)
{
Gvaffiche.editindex =-1;
MYAFFICHE.DELAFFICEBF (Convert.ToInt32 (Gvaffiche.datakeys[e.rowindex). Value.tostring ()));
Bindgvaffiche ();
}
protected void Gvaffiche_rowupdating (object sender, Gridviewupdateeventargs e)
{
int id = Convert.ToInt32 ((TextBox) (Gvaffiche.rows[e.rowindex]. Cells[0]. Controls[0]). Text.tostring (). Trim ());
String dc = ((TextBox) (Gvaffiche.rows[e.rowindex]. CELLS[1]. Controls[0]). Text.tostring (). Trim ();
MYAFFICHE.UPDATEAFFICHEBF (ID,DC);
Gvaffiche.editindex =-1;
Bindgvaffiche ();
}
protected void Gvaffiche_rowcancelingedit (object sender, Gridviewcancelediteventargs e)
{
Gvaffiche.editindex =-1;
Bindgvaffiche ();
}
#endregion

#region样式的控制
protected void Gvaffiche_rowdatabound (object sender, GridViewRowEventArgs e)
{
First, determine if the data row
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
When there is an edit column, avoid error, the RowState judgment to add
if (e.row.rowstate = = Datacontrolrowstate.normal | | e.row.rowstate = = datacontrolrowstate.alternate)
{
(Button) e.row.cells[7]. FindControl ("Btndel")). Attributes.Add ("onclick", "Javascript:return Confirm" (' You Confirm deletion: "" + e.row.cells[1]. Text + "')");
Change the background color when the mouse hovers
E.row.attributes.add ("onmouseover", "Color=this.style.backgroundcolor;this.style.backgroundcolor= ' #00A9FF");
Restore background color When the mouse moves away
E.row.attributes.add ("onmouseout", "This.style.backgroundcolor=color");
GVAFFICHE.ATTRIBUTES.ADD ("Style", "word-break:keep-all;word-wrap:normal");
GVAFFICHE.ATTRIBUTES.ADD ("Style", "Word-break:break-all;word-wrap:break-word");
if (e.row.cells[1). Text = = "444")
{
E.ROW.CELLS[1]. BackColor = System.Drawing.Color.Red;
}
}
}
}
#endregion


The above is the GridView control some of the basic use of the encyclopedia, hope to be useful to everyone.

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.