GridView使用

来源:互联网 发布:windows ad域控 编辑:程序博客网 时间:2024/05/01 04:36
VS2008中GridView小结:
一:列字段类型:
1 列类型:
BoundField:绑定列,将数据库中的数据以字符形式绑定显示
CheckBoxField:复选框列,一般用来绑定数据库中的Bit型数,以复选框的形式显示在GridView中
HyperLinkField:超链接列,可以用数据源中的数据作超链接文本也可以把所有超链接文本设为统一的文本
ImageField:图片列,绑定数据源中的图片路径,并把图片显示出来
CommandField:命令列,常用的“选择”,“删除”,“编辑、更新、取消”
ButtonField:按钮列,其它做用的按钮
TemplateField:模板列,可以更灵活地自定义显示格式
2 在 Aapx页面中主要做几项工作:
(1) 列类型的选择
(2) 列样式选择(也可再RowDataBound事件中设置)
(3) 列格式化显示
(4) 列参数设置
(5) 隐藏列的设置(也可再RowDataBound事件中设置)

二:不同事件中,如何取得:主键值,列值,列控件
三:在模板列中可加入各种服务器控件,在模板列中的命令按钮,首先触发GridView1_RowCommand,其次触发Button_Click事件 
1 在模板列中取得控件,在Button_Click事件 
(1) ((Label)(((LinkButton)sender).Parent.FindControl("Label1"))).Text = "change me";
(2) 在TextBox 的TextChanged事件中:
TextBox t = (TextBox)sender; 
GridViewRow drv = (GridViewRow)t.NamingContainer; 
int rowIndex = drv.RowIndex; 
string coid = ((Label)gdvList.Rows[drv.RowIndex].FindControl("lblCoId")).Text; 

(1) protected void lbtnViewRole_Click(object sender, EventArgs e) 

int rowIndex = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex; 
RoleModel role = RoleBLL.GetRoleInfo(Int32.Parse(gvRoleList.Rows[rowIndex].Cells[0].Text)); 
txtRoleNameEdit.Text = role.RoleName; 
txtRoleDescEdit.Text = role.RoleDesc; 
cbIsAuth.Checked = role.IsUser; 
cbIsSysEdit.Checked = role.IsSystem; 

2 在模板列中取得控件,在GridView1_RowCommand事件
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
     {
        if (e.CommandName == "lbtn")
        {
//获取被点击的linkButton所在的GridViewRow
            GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); 
            int index = gvrow.RowIndex; //获取到行索引 RowIndex
            //获取当前行的某列值
            string userid=GridView1.Rows[index].Cells[列索引].Text.Trim();
            ......
        }      
     }
3 在模板列中取得控件,在GridView1_RowCommand事件
int index = Convert.ToInt32(e.CommandArgument); //获得该按钮在gridview中的位置,即第几行
GridViewRow row = GridView1.Rows[index]; //通过索引返回该行 
string i= row.Cells[0].Text.ToString().Trim(); //获得该行第1列的数据项

四:在<asp:ButtonField>中的命令按钮只能在GridView1_RowCommand事件中触发
1 <asp:ButtonField ButtonType=Button ButtonType=Image ButtonType=Link...有三种按钮类型。
2 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "aa")//aa是关联的事件名
{
int index = Convert.ToInt32(e.CommandArgument);//获取命令的参数
int iLeagueID = Convert.ToInt32(this.GridView1.DataKeys[index].Value);//获取当前点击列的ID号
Response.Write("<script>alert('" + iLeagueID + "')</script>");
}
}

五:在模板列中非命令字段,<%# Eval("字段名").ToString( ).Trim( ) %>
<%# Eval("PublishDate", "{0:dd/MM/yyyy}") %>
或者用Bind 方法支持读/写功能
六:BoundField字段:属性:DataFormatString,可设置显示字段的格式 {0:C}格式为货币类型
      注意,当HtmlCode属性设置为false DataFormatString才有效

七:HyperLinkField字段

DataNaVigateUrlFields 绑定数据库字段,多个就用 , 分隔
DatanaVigateUrlFormatstring 超链接到的页面
DatanaVigateUrlFormatstring="default.aspx?name={0}&address={1}&city={2}&state={3}"
DataNaVigateUrlFields="name,address,state,zip"

八:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
ChangeDate(e);
HighLightCar(e);
GetAvgPrice(e);////////////////////////////
}
private void GetAvgPrice(GridViewRowEventArgs e)
{
//累加当前页中的汽车的价格总和(_PriceSum)
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
string strprice = e.Row.Cells[4].Text;//带有RMB字符串,如:RMB50.47
strprice = strprice.Substring(strprice.LastIndexOf(">") + 1);
double price = double.Parse(strprice);
_PriceSum += price;
}
}
//根据_PriceSum计算平均价格,并在页脚显示
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[4].Text = "<font color=red>RMB</font>"+(_PriceSum / GridView1.PageSize).ToString();
e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
}
}

1 GridView的数据源DataTabel或者是AccessDataSource控件
2 控件的AutoGenerateColumns属性如果是True,则自动绑定数据源
3 重点是GridView控件的<Columns>属性的设置
<1>主键列的隐藏和取值
a:在GridView的属性中设置DataKeyNames="ID字段的名称",在<Columns>中不出现ID列
<2>主键列的取值
GridViewEntity.DataKeys[RowIndex]["ColumsName"]
或者
GridViewEntity.Rows[RowIndex].Cell[Index].Text
或者
GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
或者
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked 
// by the user from the Rows collection.
GridViewRow row = CustomersGridView.Rows[index]; 
item.Text = Server.HtmlDecode(row.Cells[2].Text);
<2>添加序号
<asp:TemplateField HeaderText="序号">
<ItemTemplate>
<%#this.ctrlPager.CurrentPageIndex * this.ctrlPager.PageSize + Container.DataItemIndex + 1%>
</ItemTemplate>
</asp:TemplateField>
0 0
原创粉丝点击