asp.NetのGridViewコントロールの使い方大全
バックグラウンド
#region
protected void BindFollowExamInfoGridView(int PersonID)
{
int currentpage = Convert.ToInt32(lblPage.Text);
DataTable dt = new DataTable();
dt = feibf.GetByPersonIDFollowExamInfo(PersonID); //
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
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
=======
#region
protected void FollowExamInfoGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
FollowExamInfoGridView.PageIndex = e.NewPageIndex;
BindFollowExamInfoGridView(Convert.ToInt32(HiddenPersonID.Value));
}
#endregion
Grid Viewの実装を選択
#region
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
色と削除の表示
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//int i;
//for (i = 0; i < GridViewRegiment.Rows.Count; i++)
//{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// , , RowState
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((ImageButton)e.Row.Cells[2].FindControl("IBtndelete")).Attributes.Add("onclick", "javascript:return confirm(' :"" + e.Row.Cells[0].Text + "" ?')");
}
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#00A9FF'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E6F5FA'");
}
//}
}
GridView空の処理
1ヘッダーのない空のレコードを表示します.EmptyDataText=「レコードなし」
2ヘッダーの空のレコードを表示
DataTable dt = new DataTable();
dt = feibf.GetByPersonIDFollowExamInfo(PersonID); //
DataView dv = SortBindGrid(dt);
if (dt.Rows.Count > 0)
{
FollowExamInfoGridView.DataSource = dv;
FollowExamInfoGridView.DataBind();
}
else
{
//
dt.Rows.Add(dt.NewRow());
FollowExamInfoGridView.DataSource = dt;
FollowExamInfoGridView.DataBind();
//
int columnCount = FollowExamInfoGridView.Rows[0].Cells.Count;
//
FollowExamInfoGridView.Rows[0].Cells.Clear();
//
FollowExamInfoGridView.Rows[0].Cells.Add(new TableCell());
//
FollowExamInfoGridView.Rows[0].Cells[0].ColumnSpan = columnCount;
//
FollowExamInfoGridView.Rows[0].Cells[0].Style.Value = "text-align:center";
FollowExamInfoGridView.Rows[0].Cells[0].Text = " ";
}
GridViewのエクスポート
EnableEventValidation="false"
#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");
// GetEncoding("GB2312"); !!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";// excel 。
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
ToolTip GridView
<br> function Tooltip(cella,cellb) <br> { <br> document.getElementById("dc").innerText = " :"+cellb; <br> document.getElementById("id").innerText = "ID:"+cella; <br> x= event.clientX+document.body.scrollLeft; <br> y=event.clientY+document.body.scrollTop+20; <br> toolTipLayer.style.display="inline"; <br> toolTipLayer.style.left=x; <br> toolTipLayer.style.top=y; <br> } <br>
バックグラウンド
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)
{
//
if (e.Row.RowType == DataControlRowType.DataRow)
{
// , , RowState
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((Button)e.Row.Cells[7].FindControl("btnDel")).Attributes.Add("onclick","javascript:return confirm(' :"" + e.Row.Cells[1].Text + ""')");
//
e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//
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
以上はGridViewコントロールの基本的な使用大全であり、皆さんに役に立つことを望んでいます.