顯示具有 Gridview 標籤的文章。 顯示所有文章
顯示具有 Gridview 標籤的文章。 顯示所有文章

2013年9月11日 星期三

{C#} Gridview 動態建立ITemplate

 class LinkColumn : ITemplate
        {
            public void InstantiateIn(System.Web.UI.Control container)
            {
                LinkButton link = new LinkButton();
                link.ID = "link";
                container.Controls.Add(link);
            }
        }
        private void CreateWoLinkColumn(string headerText)
        {
            var column = new TemplateField() { HeaderText = headerText };
            column.ItemTemplate=new LinkColumn();
            column.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            column.HeaderStyle.Wrap = false;
            MainDG.Columns.Add(column);
        }
//Page_init叫用
protected void Page_Init(object sender, EventArgs e)
        {
            CreateWoLinkColumn("aa");
        }

*動態生成Template 因每次Postback時,會重跑Page_Load function,故在第一次生成動態Template時,需寫在Page_Init 以保存控制項,爾後要取控制項的值才FindControl的到

*Page 生命週期:PreInit=>Init=>InitComplete=>PreLoad=>Load.....


參考資料:
http://blog.sina.com.cn/s/blog_8038620001012s9d.html

2012年11月9日 星期五

{C#} Gridview 加一欄為流水號欄位


<asp:TemplateField HeaderText="No" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <%# (Container.DataItemIndex+1).ToString()%>
    </ItemTemplate>
</asp:TemplateField>


參考資料:
http://www.dotblogs.com.tw/petedotnet/archive/2009/01/11/6733.aspx

2012年9月17日 星期一

{C#} Gridview 欄位合並


//結果圖:

//針對前三欄如上一欄資料與下一欄資料相同,則合並
protected void MainDG_PreRender(object sender, EventArgs e)
        {
                //分格Row
                int i = 1;
                foreach (GridViewRow item in MainDG.Rows)
                {
                    if (item.RowIndex != 0)
                    {
                        if (item.Cells[0].Text.Trim() == MainDG.Rows[item.RowIndex - i].Cells[0].Text.Trim() && item.Cells[1].Text.Trim() == MainDG.Rows[item.RowIndex - i].Cells[1].Text.Trim()
                            && item.Cells[2].Text.Trim() == MainDG.Rows[item.RowIndex - i].Cells[2].Text.Trim() && item.Cells[3].Text.Trim() == MainDG.Rows[item.RowIndex - i].Cells[3].Text.Trim())
                        {
                            MainDG.Rows[(item.RowIndex - i)].Cells[0].RowSpan += 1;
                            item.Cells[0].Visible = false;
                            MainDG.Rows[(item.RowIndex - i)].Cells[1].RowSpan += 1;
                            item.Cells[1].Visible = false;
                            MainDG.Rows[(item.RowIndex - i)].Cells[2].RowSpan += 1;
                            item.Cells[2].Visible = false;
                            MainDG.Rows[(item.RowIndex - i)].Cells[3].RowSpan += 1;
                            item.Cells[3].Visible = false;
                            i++;
                        }
                        else
                        {
                            MainDG.Rows[(item.RowIndex)].Cells[0].RowSpan += 1;
                            MainDG.Rows[(item.RowIndex)].Cells[1].RowSpan += 1;
                            MainDG.Rows[(item.RowIndex)].Cells[2].RowSpan += 1;
                            MainDG.Rows[(item.RowIndex)].Cells[3].RowSpan += 1;
                            i = 1;
                        }
                    }
                    else
                    {
                        item.Cells[0].RowSpan = 1;
                        item.Cells[1].RowSpan = 1;
                        item.Cells[2].RowSpan = 1;
                        item.Cells[3].RowSpan = 1;
                    }
                }
           
        }


參考資料:
http://www.dotblogs.com.tw/mis2000lab/archive/2008/04/24/3451.aspx
http://epaper.blueshop.com.tw/board/show.asp?subcde=BRD20061225110536B4G&fumcde=FUM20050124192253INM
http://pramaire.pixnet.net/blog/post/28100706-gridview%E6%A8%99%E9%A1%8C%28%E8%A1%A8%E9%A0%AD%29%E5%90%88%E4%BD%B5

2012年8月30日 星期四

{GridView} 當無資料時,顯示標題與無資料字眼

Gridview 當無資料時,顯示「沒資料」設定

ShowHeaderWhenEmpty="true"
EmptyDataText="沒資料"


參考資料:
http://jenmingisme.blogspot.tw/2009/02/gridview.html

2012年8月6日 星期一

{HTML} Gridview數據格式設定:千分位、小數點

數值千分位,且取小數第二位
方法一、DataFormatString="{0:N2}"

方法二、DataFormatString="{0:#,##0.00}"
               HtmlEncode="false"
方法二的寫法,必設定屬性 HtmlEncode="false",使可執行方法二DataFormatString 格式
ps.當欄位標題設定「HeaderText="CPU <br> Landed Cost"」時,也要將屬性 設定HtmlEncode="false" ,以可解讀成html語法不被編碼。

參考資料:
http://www.allenkuo.com/EBook5/view.aspx?TreeNodeID=82&id=486

2012年7月12日 星期四

{HTML}GridView BoundField、TemplateField 千分位

在BoundField之下
DataFormatString="{0:N0}"

在TemplateField之下
'<%# string.Format("{0:N0}",Eval("Qty")) %>'

'<%# Eval("Qty","{0:N0}") %>'

以上設定僅千分位,不會有小數點之設定


參考資料:
http://kuanglian2000.wordpress.com/2009/10/23/templatefield-add-number-format/
http://topic.csdn.net/u/20071130/11/3C3D2D26-5D0D-4DCB-BF3A-F00438209A6C.html

2012年6月21日 星期四

{C#} Gridview Style 設定 更新時間:6/21

1.內文依資料改變欄寬
<ItemStyle Wrap="false" />

2.內文置中
<asp:TemplateField HeaderText="abc" HeaderStyle-HorizontalAlign="Center"  ItemStyle-HorizontalAlign="Center">
HeaderStyle-HorizontalAlign="Center"---標題置中
ItemStyle-HorizontalAlign="Center"------內文置中


參考資料:http://msdn.microsoft.com/zh-tw/library/ms178296(v=vs.80).aspx

2012年6月19日 星期二

{C#} Gridview 修改標題方法

方法有二,如下:
一、在Gridview1_OnRowDataBound事件下---常常起不了作用

if (e.Row.RowType==DataControlRowType.Header)
{
     Gridview1.Columns[1].HeaderText="abc";
}

二、在Bind()後
Gridview1.HeaderRow.Cells[1].Text ="abc";


參考資料
http://kb.cnblogs.com/a/1571267/
http://social.msdn.microsoft.com/Forums/zh-TW/236/thread/7a99bffa-b430-4538-8cb8-30b9fc2ad43f

2012年6月13日 星期三

{C#} GridView 欄位值某條件改變字色

當Gridview中資料符合某資料時,帶出的值變字色

在Gridview OnRowDataBound事件下,當值小於0,則變紅色字體(系統設定之紅色)


if(float.Parse(e.Row.Cells[i].Text)<0)
{
    e.Row.Cells[i].ForeColor = System.Drawing.Color.Red;
}

附:改變其值

if (e.Row.Cells[6].Text == "-1")
    e.Row.Cells[6].Text = "No Forecast";

參考資斗:
http://www.blueshop.com.tw/board/show.asp?subcde=BRD200610031156181Y8

2012年5月16日 星期三

{C#}GridView 閃光棒效果、隔行不同色


GridView_RowDataBound事件:
if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#f8eedf'");
                if (e.Row.RowIndex % 2 == 0)
                {
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'");
                }
                else
                {
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#f4f4f4'");

                }
             }

{C#}Gridview 內按鈕觸發事件

當在Gridview欄位內放置按鈕需要定義觸發後的工作,則使用Gridview_RowCommand事件

事件定義:
按一下 GridView 控制項中的按鈕時,會引發 RowCommand 事件。這可讓您提供事件處理方法,用於每次發生這個事件時執行自訂常式。


步驟:
1.在按鈕內aspx設定 CommandName屬性, CommandName="exportToExcel"
2.建立RowCommand事件
3.事件內判斷
if (e.CommandName=="exportToExcel")
{
    //放置觸發後工作
}

附:
如需要從Bing資料時,取得資訊,以利Command事件使用之引數
方法:
GridView_RowDataBound事件中,取得按鈕控制項,給于CommandArgument值

if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgButton = (ImageButton)e.Row.FindControl("exportToExcel");
                desc = DataBinder.Eval(e.Row.DataItem, "Id").ToString();
                imgButton.CommandArgument = desc;
            }


參考資料:http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.gridview.rowcommand(v=vs.80).aspx

{C#} Gridview使用分頁產生「資料來源不支援伺服器端的資料分頁」錯誤

在使用Gridview分頁時,會產生「資料來源不支援伺服器端的資料分頁」錯誤
原因:Gridview Datasource 為Datatable之相關格式
以遇到的錯誤範例來說,是 xxx[] 格式,轉為.ToList()即可


參考網址:http://www.blueshop.com.tw/board/show.asp?subcde=BRD20100226002017FOI