GridView(学习笔记)


GridView的DatasourcelID 和DataSource :

DataSourceID是用于专门搭配ASP.NET 2.0的DataSource 控件
DataSource 是用户绑定数据的,还要声明GridView.DataBind(),不支持分页,排序,更新,删除功能
GridView不可以同时声明上面的两个属性


////////////////////////////////////////////////////////////////////////////////////////////
GridView支持7个字段,如下:

BountField  (数据绑定)
BottonField(按钮字段)
CommandField(命令字段)  slecet edit ,update delete 
CheckBoxField(CheckBox字段)
HyperLinkField(超链接字段) 
TemplateField(模版字段)
ImageField(图像字段)


////////////////////////////////////////////////////////////////////////////////////////////////
用Field结尾的原因是因为这些字段都是由DataControlField派生而来的  故有Field 字眼

////////////////////////////////////////////////////////////////////////////////////////////////

在BoundField 字段的格式化必须将HtmlEncode属性设置为false否则格式化石起不了任何作用的

//////////////////////////////////////
如果在BottonField中要判断引发事件之数据行的索引,用CommandArgument 属性
        protected void GridView4_RowCommand(object sender, GridViewCommandEventArgs e)
        {
                int index = Convert.ToInt16(e.CommandArgument);
                GridViewRow selectrow = GridView4.Rows[index];

                TableCell productname = selectrow.Cells[1];
                switch (e.CommandName)
                {
                        case "Order": lborder.Items.Add(productname.Text);
                                break;
                        case "CancelOrder":
                                if (lborder.Items.Count > 0)
                                {
                                        int i = 0;
                                        while (i <= lborder.Items.Count - 1)
                                        {

                                                if (lborder.Items[i].Text == productname.Text)
                                                {
                                                        lborder.Items.Remove(lborder.Items[i]);
                                                        break;
                                                }
                                                else
                                                {
                                                        i++;
                                                }
                                        }
                                }

                                break;
                }
        }

/////////////////////////////////////////
如果GridView 的数据很多,当网页refresh后,页面常常会回到最端,有什么解决方法呢?
可以设置  : MaintainScrollPositionOnPostback=true
语法如下:
<%@ Page Language="C#"   MaintainScrollPositionOnPostback=true%>
OK!搞定了!没有问题了

/////////////////////////////////
CheckBoxField 默认情况下CheckBoxField是被Disabled,故呈现是灰色的只读状态,只有在编辑状态下才会变成Enabled,才能被修改

////////////////////////////////////////////////////////////
TemplateField模版字段

当自带的字段无法满足时,就用这个字段
ItemTemplate                     字段项目模版
AlternatingItemTemplate       字段间隔项目模版,若设置这个字段后,奇数行会显示ItemTemplate,偶数行会显示AlternatingItemTemplate   

EditItemTemplate 编辑模式模版 

HeaderTemplate  表头模版
FooterTemplate 表尾模版

/////////////////////////////////////////
GridView 的AJAX 异步Callback功能
只要将GridView的EnableSortingAndPagingCallbacks属性设置为true就可以了,随后,Callback异步功能就会立即产生作用。加了AJAX的Atlas UpdatePanel控件后GridView的异步功能不但支持分页、排序,连编辑删除,选取与更新都是AJAX异步的了!

////////////////////////////////////////////////////////
GridView 结合OnClientClick进行确认操作
OnClientClick="return confirm('确定删除?')"  
注意: onlineclick 只能对Button控件上才有效,

////////////////////////////////////////////
产生“光棒”效果   设置gridview 的onmouseover 事件的变换HighLight颜色效果  ,主要是设置GridView的RowDataBind事件
如:
 protected void gviewEmployees_RowDataBound(object sender, GridViewRowEventArgs e)
    {
            switch (e.Row.RowType)
            {
                    case DataControlRowType.Header:
                            e.Row.BackColor = Color.FromA#990000;
                            e.Row.ForeColor = Color.White;
                            break;
                    case DataControlRowType.DataRow:
                            //建立奇数行与偶数行的onmouseover及onmouseout的颜色变换
                            string rowstate= "this.style.backgroundColor='#C0C0FF';this.style.color='#ffffff'";
                            if (Convert.ToInt16(ViewState["LineNo"]) == 0)
                            {
                                    e.Row.BackColor = Color.FromA#fffbd6;
                                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFBD6';this.style.color='black'");
                                    e.Row.Attributes.Add("onmouseover", rowstate);

                                    ViewState["LineNo"] = 1;
                            }
                            else
                            {
                                    e.Row.BackColor = Color.White;
                                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF';this.style.color='black'");
                                    e.Row.Attributes.Add("onmouseover", rowstate);

                                    ViewState["LineNo"] = 0;
                            }

                            break;
            }
    }
////////////////////////////////////////////////////////////////
            codebehind下GridView 的分页排序程序的技巧:
            gviewProducts.AllowPaging = true; //设置分页
gviewProducts.AllowSorting = true; //设置排序
gviewProducts.Font.Size = 12; //设置字号大小
gviewProducts.GridLines = GridLines.Both; //设置网格线
gviewProducts.PageSize = 10;
//非同步Callback模式
gviewProducts.EnableSortingAndPagingCallbacks = true;
//分页位置
gviewProducts.PagerSettings.Position = PagerPosition.TopAndBottom;
//分页对齐
gviewProducts.PagerStyle.HorizontalAlign = HorizontalAlign.Center; 

gviewProducts.HeaderStyle.BackColor = Color.Tan;
gviewProducts.RowStyle.BackColor = Color.LightGoldenrodYellow;
gviewProducts.AlternatingRowStyle.BackColor = Color.PaleGoldenrod;
gviewProducts.HeaderStyle.ForeColor = Color.Black;
gviewProducts.PagerStyle.BackColor = Color.Goldenrod;

/////////////////////////////////////////////////////////
设置选择行背景颜色
gviewEmployees.SelectedRowStyle.BackColor = Color.LightBlue;

////////////////////////////////////////////////////
用DataControlField 来遍历GridView的每一列
foreach (DataControlField field in gviewEmployees.Columns)
{
if (!String.IsNullOrEmpty(field.HeaderText))
{
cbxlistSortColumns.Items.Add(new ListItem(field.HeaderText, i.ToString()));
}
i++;
}
////////////////////////////////////////////////
CommandField selectField = new CommandField();
selectField.ButtonType = ButtonType.Link;
selectField.ShowSelectButton = true; //显示选择按钮


////////////////////////////////
GridView 中删除多重字段组成的唯一识别键值是设置;
string []KeyName={"FristName","LastName"};
GridView.DataKeyName=KeyName;



///////////////////////////////////////////////////
GridView 中的多重表头表尾
//创建表头表尾
protected void gviewEmployee_RowCreated(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
case DataControlRowType.Header:
//第一行表头
TableCellCollection tcHeader = e.Row.Cells;
tcHeader.Clear();
tcHeader.Add(new TableHeaderCell());
tcHeader[0].Attributes.Add("rowspan", "3"); //跨Row
tcHeader[0].Attributes.Add("bgcolor", "LightCyan");
tcHeader[0].Text = "员工项目";
tcHeader.Add(new TableHeaderCell());
tcHeader[1].Attributes.Add("colspan", "6"); //跨Column
tcHeader[1].Text = "员工基本资料</th></tr><tr>";

//第二行表头
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Attributes.Add("bgcolor", "Thistle");
tcHeader[2].Text = "员工ID";
tcHeader.Add(new TableHeaderCell());
tcHeader[3].Attributes.Add("bgcolor", "LightBlue");
tcHeader[3].Attributes.Add("colspan", "2");
tcHeader[3].Text = "员工姓名";
tcHeader.Add(new TableHeaderCell());
tcHeader[4].Attributes.Add("bgcolor", "Thistle");
tcHeader[4].Text = "担任职务";
tcHeader.Add(new TableHeaderCell());
tcHeader[5].Attributes.Add("bgcolor", "LightBlue");
tcHeader[5].Attributes.Add("colspan", "2");
tcHeader[5].Text = "通讯地址</th></tr><tr>";

//第三行表头
tcHeader.Add(new TableHeaderCell());
tcHeader[6].Attributes.Add("bgcolor", "LightSalmon");
tcHeader[6].Text = "员工代号";
tcHeader.Add(new TableHeaderCell());
tcHeader[7].Attributes.Add("bgcolor", "LightSalmon");
tcHeader[7].Text = "名字";
tcHeader.Add(new TableHeaderCell());
tcHeader[8].Attributes.Add("bgcolor", "LightSalmon");
tcHeader[8].Text = "姓氏";
tcHeader.Add(new TableHeaderCell());
tcHeader[9].Attributes.Add("bgcolor", "LightSalmon");
tcHeader[9].Text = "职称";
tcHeader.Add(new TableHeaderCell());
tcHeader[10].Attributes.Add("bgcolor", "LightSalmon");
tcHeader[10].Text = "县市";
tcHeader.Add(new TableHeaderCell());
tcHeader[11].Attributes.Add("bgcolor", "LightSalmon");
tcHeader[11].Text = "地址";
break;
case DataControlRowType.Footer:
//第一行表尾
TableCellCollection tcFooter = e.Row.Cells;
tcFooter.Clear();
tcFooter.Add(new TableHeaderCell());
tcFooter[0].Attributes.Add("bgcolor", "LightBlue");
tcFooter[0].Text = "======";
tcFooter.Add(new TableHeaderCell());
tcFooter[1].Text = "注一";
tcFooter[1].Attributes.Add("bgcolor", "Thistle");
tcFooter.Add(new TableHeaderCell());
tcFooter[2].Attributes.Add("bgcolor", "LightBlue");
tcFooter[2].Text = "注二";
tcFooter.Add(new TableHeaderCell());
tcFooter[3].Attributes.Add("bgcolor", "LightBlue");
tcFooter[3].Text = "注三";
tcFooter.Add(new TableHeaderCell());
tcFooter[4].Attributes.Add("bgcolor", "Thistle");
tcFooter[4].Text = "注四";
tcFooter.Add(new TableHeaderCell());
tcFooter[5].Attributes.Add("bgcolor", "LightBlue");
tcFooter[5].Attributes.Add("colspan", "3");
tcFooter[5].Text = "注五</th></tr><tr>";
//第二行表尾
tcFooter.Add(new TableHeaderCell());
tcFooter[6].Attributes.Add("bgcolor", "Tan");
tcFooter[6].Attributes.Add("colspan", "7");
tcFooter[6].Text = "GridView多重表尾</th></tr><tr>";
break;
}
}
基本上GridView每个GridViewRow 只会有一个<tr></tr>区段

//////////////////////////////////////////////////////////////////////////////
GridView 设置背景图片
//Row创建事件,在此添加表头与表尾背景图片
protected void gviewEmployee_RowCreated(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
//若Row为Header,则在此添加图片
case DataControlRowType.Header:
TableCellCollection tc1 = e.Row.Cells; //取得Row的所有Cells
tc1.Clear(); //清除所有的Cells
//创建TableCell
TableHeaderCell gviewHeader = new TableHeaderCell();
System.Web.UI.WebControls.Image headerImage1 = new System.Web.UI.WebControls.Image();
headerImage1.ImageUrl = "~/Images/HeaderImage.jpg";
gviewHeader.Controls.Add(headerImage1);
tc1.Add(gviewHeader);
tc1[0].Attributes.Add("colspan", "7"); //设置跨行
break;
//若Row为Footer,则在此添加图片
case DataControlRowType.Footer:
TableCellCollection tc2 = e.Row.Cells;
tc2.Clear();
//创建TableCell
TableHeaderCell gviewFooter = new TableHeaderCell();
System.Web.UI.WebControls.Image footerImage2 = new System.Web.UI.WebControls.Image();
footerImage2.ImageUrl = "~/Images/FooterImage.jpg";
gviewFooter.Controls.Add(footerImage2);
tc2.Add(gviewFooter);
tc2[0].Attributes.Add("colspan", "7"); //设置跨行
break;
}
}
////////////////////////////////////////////////////////////////
GridView结束语:
GridView是.NET 里很强大的一个控件,尤其是和二维表的交互方面,做得相当的出色!



本文所有权归作者,欢迎转载,但是请指明出处 :http://www.cnblogs.com/fly871117 ,严谨用于商业用途
原文地址:https://www.cnblogs.com/fly871117/p/1507159.html