且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不使用数据源控件的情况下将新行添加到GridView?

更新时间:2022-10-23 11:47:22


这样会产生想法

 <   div  &gt ; 
       <   asp:GridView     ID   ="   runat   服务器"  AutoGenerateColumns   错误" 
                     span>        onrowdatabound    GridView1_RowDataBound " <   > 
               <   asp:TemplateField     HeaderText   ="  <   ItemTemplate  > 
                       <   asp:Label     ID   ="   runat   服务器" 文本   <%#Bind('  >  ><  /asp:Label  > 
                   <  /ItemTemplate  > 
               <  /asp:TemplateField  > 
               <   asp:TemplateField     HeaderText   ="  <   ItemTemplate  > 
                       <   asp:Label     ID   ="   runat   服务器" 文本   &%; span>#Bind('  >  ><  /asp:Label  > 
                   <  /ItemTemplate  > 
               <  /asp:TemplateField  > 
               <   asp:TemplateField     HeaderText   ="  <   ItemTemplate  > 
                       <   asp:Label     ID   ="   runat   服务器" 文本   <%#Bind(' 品牌')%>"  >  <  /asp:Label  > 
                   <  /ItemTemplate  > 
               <  /asp:TemplateField  > 
               <   asp:TemplateField     HeaderText   ="  <   ItemTemplate  > 
                       <   asp:Label     ID   ="   runat   服务器" 文本   <%#Bind('  >  <  /asp:Label  > 
                   <  /ItemTemplate  > 
               <  /asp:TemplateField  > 
           <  /列 > 
       <  /asp:GridView  > 
   <  /div  >  



背后的代码:

 受保护的 无效 Page_Load(对象发​​件人,EventArgs e)
   {
       如果(!IsPostBack)
       {
            var  con =  SqlConnection( @" 数据源= .;初始目录=测试;集成安全性= True" );
           字符串语句= " ;
           SqlDataAdapter da =  SqlDataAdapter(statement,con);
           DataTable dt =  DataTable();
           da.Fill(dt);
           GridView1.DataSource = dt;
           GridView1.DataBind();
       }
   }
   受保护的 无效 GridView1_RowDataBound(对象发​​件人,GridViewRowEventArgs e)
   {
       // 检查标题是否为
       如果(例如,Row.RowType == DataControlRowType.Header)
       {
                 // 在Gridview中创建新行
         GridViewRow row =  GridViewRow(-1,-1,DataControlRowType.DataRow,DataControlRowState.Insert);
             // 创建单元格,它将被添加到行中
          TableCell单元格=  TableCell();
           cell.Text = " ;
           row.Cells.Add(cell);


           TableCell cell1 =  TableCell();
           cell1.Text = " ;
           row.Cells.Add(cell1);

          TableCell cell2 =  TableCell();
           cell2.Text = " ;
           row.Cells.Add(cell2);

         TableCell cell3 =  TableCell();
           cell3.Text = " ;
           row.Cells.Add(cell3);
                // 将该行添加到Gridview和AddAT(该行将出现的位置的索引
            GridView1.Controls [ 0 ].Controls.AddAt( 1 ,row);
       }
   }



***的问候
M.Mitwalli


这可能对您有帮助

http://geekswithblogs.net/dotNETvinz/存档/2009/06/04/adding-dynamic-rows-in-gridview-with-textboxes.aspx [ http://***. com/questions/7864991/adding-new-rows-dynamically-in-a-grid-view-or-datatable-in-asp-net [ http://www.itprojectsforyou.com/gridview.php [解决方案