2011年9月23日

[ASP.net][C#] - DataGrid取值

DataGrid有幾種Column,提供使用者作運用,
當使用各式各樣的Column,取值的方式也就不太一樣,

每次取值都要試很多次,索性就整理一下網路上的資料
設定Column有幾種模式,今天介紹幾種常見的。
(1)最基礎的用法


(2)在Item再加入一個物件

            
            
            
            

(3)直接做欄位的靜態設定

            
            <%#DataBinder.Eval(Container.DataItem, "BB")%>
            
            


對應的取值方法如下
(1)這個取值方法是最基本最直覺的
Grid.Items[i].Cells[0].Text;

(2)這個取值有兩種方法。
(Grid.Items[i].Cells[1].controls[0] as Label).Text.Trim();
或
(Grid.Items[i].Cells[1].controls[0] as DataBoundLiteralControl).Text.Trim();

(3)取得欄位靜態的值(這個比較不容易想到要轉型成這個)
(Grid.Items[i].Cells[1].controls[0] as DataBoundLiteralControl).Text.Trim();

以上是DataGrid取值的方式,若有錯誤或更棒的方法還請不吝指教

沒有留言:

張貼留言