GridView是VS2005中對VS2003的DataGrid的增強替代控件
下面展示一下它的基本常見應用
效果圖如下:

[查詢]按鈕:查詢數據庫 ,顯示信息Table 並 綁定GridView
//查詢按鈕
protected void btnQue_Click(object sender, EventArgs e)
{
this.tableInfo.Visible = true;
SqlConnection sqlconn = new SqlConnection("server=localhost;database=db;uid=uid;pwd=pwd;");
sqlconn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from table", sqlconn);
DataSet ds = new DataSet();
sda.Fill(ds);
this.grvInfo.DataSource = ds;
this.grvInfo.DataBind();
sda.Dispose();
ds.Dispose();
sqlconn.Close();
}[全選]按鈕:
//全選
protected void btnAllCh_Click(object sender, EventArgs e)
{
foreach(GridViewRow currow in grvInfo.Rows)
{
((CheckBox)currow.Cells[0].Controls[1]).Checked = true;
}
}
類似的[取消全選]的編碼為:
// 取消全選
protected void btnNoCh_Click(object sender, EventArgs e)
{
foreach (GridViewRow currow in grvInfo.Rows)
{
((CheckBox)currow.Cells[0].Controls[1]).Checked = false;
}
}
[明細]按鈕:
該按鈕的操作,要寫在GridView ROW按鈕事件--- RowCommand
// GridView ROW按鈕事件 RowCommand
protected void grvInfo_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName.Equals("mingxin"))
{
ImageButton lb = (ImageButton)e.CommandSource;
GridViewRow curgvr = (GridViewRow)lb.Parent.Parent;
string paraValue = curgvr.Cells[2].Text.ToString();
//RegisterClientScriptBlock("openmingxin", "<scriptlanguage='javascript'>window.open(src='mingxin.aspx?pihao="+paraValue+"','newwindow','');</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "aa","<script language='javascript'>alert("+paraValue+"');</script>");
}
}
[刪除]按鈕:
可以在HTML原始檔中加上
<asp:BoundField HeaderText="審核" />
<asp
emplateField HeaderText="刪除">
<ItemStyle Horiz />
<ItemTemplate>
<asp:ImageButt ImageUrl="~/image/del.JPG" ID="delid" CommandName="del" />
</ItemTemplate>
</asp
emplateField>
同時也可以如[明細]按鈕在GridView ROW按鈕事件--- RowCommand 對[刪除]點擊按鈕的事件進行服務器端處理!
============================
GridView中 样板列 加入 按钮
============================
前台:
<asp:GridViewID="GridView1" runat="server" AutoGenerateColumns="False"OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="personName" />
<asp:BoundField DataField="personAge" />
<asp
emplateField HeaderText="操作">
<ItemTemplate>
<asp:Button ID="btn_OK" runat="server" Text="确定"
CommandArgument='<%# Eval("personName") %>' CommandName="btn_OK" />
<asp:Button ID="btn_Cancel" runat="server" Text="取消" CommandName="btn_Cancel"
CommandArgument='<%# Eval("personName") %>' />
</ItemTemplate>
</asp
emplateField>
</Columns>
</asp:GridView>
后台:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//直接利用参数
string strName = e.CommandArgument.ToString();
//一种取得当前行的方法
GridViewRow currRow = (GridViewRow)((Button)e.CommandSource).Parent.Parent;
string strAge = currRow.Cells[1].Text.ToString();
if (e.CommandName == "btn_OK")
{
this.TextBox1.Text = "确定按钮 " + strName + " " + strAge;
}
if (e.CommandName == "btn_Cancel")
{
this.TextBox1.Text = "取消按钮 " + strName + " " + strAge;
}
} #region gv_showResult 数据绑定时的事件
/// <summary>
/// gv_showResult 数据绑定时的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gv_showResult_RowDataBound(object sender, GridViewRowEventArgs e)
{
//设定宽度
e.Row.Cells[0].Width = 240;
e.Row.Cells[1].Width = 340;
if (e.Row.RowIndex == -1)
return;
//加载鼠标单击事件
string strID = e.Row.Cells[0].Text.Trim();
string strName = e.Row.Cells[1].Text.Trim();
string strJS = "document.all.txt_ID.value='" + strID + "';";
strJS += "document.all.hidtxt_ID.value='" + strID + "';";
strJS += "var tmpName = escape('" + strName + "');";
strJS += "document.all.txt_Name.value=unescape(tmpName);";
//加载点击后 变色
strJS += "OnSelectBgColor(this,gv_showResult)";
e.Row.Attributes.Add("onclick", strJS);
e.Row.Attributes.Add("onmouseover", "mouseover(this);");
e.Row.Attributes.Add("onmouseout", "mouseout(this,gv_showResult);");
}
#endregion
一般情况下GridView 隐藏列
<Columns>
<asp

emplateField>
<ItemTemplate>
<asp:TextBox ID="hidtxt_currID" runat="server" Text='<%#Bind("currID") %>' style="display:none"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
后台抓值
string strCurrID = ((TextBox)this.gv_Selected.Rows
.Cells[0].FindControl("hidtxt_currID")).Text.Trim();
ajax情况下GridView 隐藏列
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="hidtxt_currID" runat="server" Text='<%#Bind("currID") %>' style="display:none"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
注意<HeaderTemplate>也需要display:none
后台抓值
string strCurrID = ((TextBox)this.gv_Selected.Rows.Cells[0].FindControl("hidtxt_currID")).Text.Trim();
自定义数据绑定:
VS2003 DataGrid 的 ItemDataBound 事件:
e.Item.Cells[0].Text=((String)DataBinder.Eval(e.Item.DataItem, "Name"));
VS2005 GridView 的 RowDataBound 事件:
if (e.Row.RowIndex >= 0)
{
DropDownList tmpDrpSex = (DropDownList)e.Row.FindControl("drp_Sex");
tmpDrpSex.Items.Clear();
tmpDrpSex.Items.Add(new ListItem("女","0"));
tmpDrpSex.Items.Add(new ListItem("男","1"));
tmpDrpSex.SelectedValue = DataBinder.Eval(e.Row.DataItem, "ASex").ToString();
}
//某个字段的内容太长时
//在GridView上的该字段上显示截断前多少字的内容
//当鼠标放上时 弹出小提示框 显示完整内容
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == -1)
return;
string strAllContent = ((TextBox)e.Row.Cells[0].FindControl("hidtxt_AllContent")).Text.Trim();
e.Row.Cells[5].Attributes.Add("title", strAllContent);
}
/鼠标经过时
//显手形
e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand'");
e.Row.Attributes.Add("onmouseout", "this.style.cursor=''");
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" ID="gvbtn_Edit"
CommandName="gv_btn_Edit" Text="查看" CommandArgument="<%# gv_DataShow.Rows.Count %>" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DataFieldA" HeaderText="DataFieldA" >
<ItemStyle CssClass="hidden" />
<HeaderStyle CssClass="hidden" />
</asp:BoundField>
GridView自动序号
第一种方式,直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了.
<asp:TemplateField HeaderText="序号" InsertVisible="False">
<ItemStyle Horiz />
<HeaderStyle Horiz Width="5%" />
<ItemTemplate>
<%#Container.DataItemIndex+1%>
</ItemTemplate>
</asp:TemplateField>
第二种方式分页时进行了计算,这样会累计向下加.
<asp:TemplateField HeaderText="序号" InsertVisible="False">
<ItemStyle Horiz />
<HeaderStyle Horiz Width="5%" />
<ItemTemplate>
<asp
abel ID="Label2" runat="server" Text='<%#this.MyListGridView.PageIndex * this.MyListGridView.PageSize +this.MyListGridView.Rows.Count + 1%>'/>
</ItemTemplate>
</asp:TemplateField>
还有一种方式放在cs代码中,和第二种相似.
<asp:BoundField HeaderText="序号" >
<ItemStyle Horiz />
<HeaderStyle Horiz Width="5%" />
</asp:BoundField>
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{
int indexID = this.myGridView.PageIndex * this.myGridView.PageSize + e.Row.RowIndex + 1;
e.Row.Cells[0].Text = indexID.ToString();
}
}
都是非常简单的,其实原理都是一样的.实现同一种效果,方法很多,实际上读取数据的时候也可以实现.
为了不至于字数太多,给阅读带来不便,SQL的方法放在以下链接里面:其实都是非常简单的入门级,可是有的时候容易忘记.
Sql Server中自动序号的方法
第一种:使用identity函数增加临时表的方法
select id = identity(int,1,1),* into #tmp from table
select * from #tmp
drop table #tmp
在SQL2005中新增了ROW_NUMBER()函数,给我们带来了很多方便,使用方法如下:
SELECT id,ROW_NUMBER() OVER (order by id)as RowNumber FROM Table有一个方便,as后的别名可以在语句后面作为条件单独使用.