DataGrid控件的四种取值方式

有的时候想取不是编辑状态的值,这个时候通常不会使用模板列中放置TextBox通过e.Item.FindControl取值,有的网友问为什么取不到?这要看你前台的列怎么写的了,下面分别写出四种情况和这四种情况下面的取值方式:

 1 DataGrid控件的四种取值方式 < asp:DataGrid  id ="DataGrid1"  runat ="server"  AutoGenerateColumns ="False"  AllowPaging ="True" >  
 2 DataGrid控件的四种取值方式                 < Columns >  
 3 DataGrid控件的四种取值方式                     < asp:BoundColumn  DataField ="vName"  HeaderText ="姓名0" ></ asp:BoundColumn >  
 4 DataGrid控件的四种取值方式                     < asp:TemplateColumn  HeaderText ="姓名1" >  
 5 DataGrid控件的四种取值方式                         < ItemTemplate >  
 6 DataGrid控件的四种取值方式                            姓名 
 7 DataGrid控件的四种取值方式                         </ ItemTemplate >  
 8 DataGrid控件的四种取值方式                     </ asp:TemplateColumn >  
 9 DataGrid控件的四种取值方式                     < asp:TemplateColumn  HeaderText ="姓名2" >  
10 DataGrid控件的四种取值方式                         < ItemTemplate >  
11 DataGrid控件的四种取值方式                             <% #DataBinder.Eval(Container.DataItem,"vName") %>  
12 DataGrid控件的四种取值方式                         </ ItemTemplate >  
13 DataGrid控件的四种取值方式                     </ asp:TemplateColumn >  
14 DataGrid控件的四种取值方式                     < asp:TemplateColumn  HeaderText ="姓名3" >  
15 DataGrid控件的四种取值方式                         < ItemTemplate >  
16 DataGrid控件的四种取值方式                             < asp:Label  ID ="name"  Runat ="server"  Text ='<%#DataBinder.Eval(Container.DataItem,"vName")% > '> 
17 DataGrid控件的四种取值方式                             </ asp:Label >  
18 DataGrid控件的四种取值方式                         </ ItemTemplate >  
19 DataGrid控件的四种取值方式                     </ asp:TemplateColumn >  
20 DataGrid控件的四种取值方式                     < asp:ButtonColumn  Text ="删除"  ButtonType ="PushButton"  CommandName ="del" ></ asp:ButtonColumn >  
21 DataGrid控件的四种取值方式                 </ Columns >  
22 DataGrid控件的四种取值方式             </ asp:DataGrid >  

有4种方式可能遇到的情况:
(1)绑定列
(2)模板列中直接放静态的字符串
(3)模板列中直接放绑定的字符串
(4)模板列中通过Label放绑定的字符串

下面说明在按下了删除按钮以后的四种取值的方式:
C#代码:
1 DataGrid控件的四种取值方式 if (e.CommandName == " del "
2 DataGrid控件的四种取值方式            
3DataGrid控件的四种取值方式                Response.Write(DataGrid1.Columns[0].HeaderText+":"+e.Item.Cells[0].Text+"<br>"); 
4DataGrid控件的四种取值方式                Response.Write(DataGrid1.Columns[1].HeaderText+":"+e.Item.Cells[1].Text.Trim()+"<br>"); 
5DataGrid控件的四种取值方式                Response.Write(DataGrid1.Columns[2].HeaderText+":"+((DataBoundLiteralControl)e.Item.Cells[2].Controls[0]).Text.Trim()+"<br>"); 
6DataGrid控件的四种取值方式                Response.Write(DataGrid1.Columns[3].HeaderText+":"+((Label)e.Item.Cells[3].FindControl("name")).Text+"<br>");                 
7DataGrid控件的四种取值方式            }
 

VB代码:
1 DataGrid控件的四种取值方式 If  e.CommandName  =   " del "   Then
2 DataGrid控件的四种取值方式            Response.Write(DataGrid1.Columns( 0 ).HeaderText  +   " : "   +  e.Item.Cells( 0 ).Text  +   " <br> " )
3 DataGrid控件的四种取值方式            Response.Write(DataGrid1.Columns( 1 ).HeaderText  +   " : "   +  e.Item.Cells( 1 ).Text.Trim  +   " <br> " )
4 DataGrid控件的四种取值方式            Response.Write(DataGrid1.Columns( 2 ).HeaderText  +   " : "   +   CType (e.Item.Cells( 2 ).Controls( 0 ), DataBoundLiteralControl).Text.Trim  +   " <br> " )
5 DataGrid控件的四种取值方式            Response.Write(DataGrid1.Columns( 3 ).HeaderText  +   " : "   +   CType (e.Item.Cells( 3 ).FindControl( " name " ), Label).Text.Trim  +   " <br> " )
6 DataGrid控件的四种取值方式         End   If

(1)第一种用绑定列的方式可以直接通过Cells[i].Text取
(2)第二种方式也直接可以取,但是讨厌的vs.net总是会在前台把列中的内容换行,所以这里还需要对输出的东西进行去除首尾的空格
(3)第三种方式是网友问的最多的,不能直接通过Cells[i].Text取到值,可以通过上面给出的方法来取,同样需要去除首尾的空格
(4)第四种方式也是最简单的方式,当然你可以直接写e.Item.FindControl("name")

你可能感兴趣的:(datagrid)