GridView是如何使用的,请详细些!谢了啊

 我来答
似献祎AI
2011-11-05 · TA获得超过279个赞
知道小有建树答主
回答量:313
采纳率:0%
帮助的人:214万
展开全部
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>
希望对你有帮助!
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式