如何在不使用数据源控件的情况下将新行添加到GridView? [英] How to Add New Row to GridView without using datasource controls?

查看:142
本文介绍了如何在不使用数据源控件的情况下将新行添加到GridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.我想通过GridView添加新的一行数据.到目前为止,我在网上找到的文章都在使用数据源控件.但是我的要求是不使用数据源控件来做到这一点.因此,请提供帮助.

解决方案


这样会产生想法

 <   div  > ; 
       <   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://stackoverflow. com/questions/7864991/adding-new-rows-dynamically-in-a-grid-view-or-datatable-in-asp-net [ http://www.itprojectsforyou.com/gridview.php [ 解决方案

Hi ,
This will give idea

<div>
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

           onrowdatabound="GridView1_RowDataBound">
           <Columns>
               <asp:TemplateField HeaderText="item code">
                   <ItemTemplate>
                       <asp:Label ID="lblID" runat="server" Text="<%# Bind('item_code') %>"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Item name">
                   <ItemTemplate>
                       <asp:Label ID="lblItemName" runat="server" Text="<%# Bind('Item_name') %>"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="brand">
                   <ItemTemplate>
                       <asp:Label ID="Label2" runat="server" Text="<%# Bind('brand') %>"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="size">
                   <ItemTemplate>
                       <asp:Label ID="lblSize" runat="server" Text="<%# Bind('size') %>"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
           </Columns>
       </asp:GridView>
   </div>



Code Behind:

protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           var con = new SqlConnection(@"Data Source=.;Initial Catalog=test;Integrated Security=True");
           string statment = "select item_code, Item_name, brand, size  from Items ";
           SqlDataAdapter da = new SqlDataAdapter(statment, con);
           DataTable dt = new DataTable();
           da.Fill(dt);
           GridView1.DataSource = dt;
           GridView1.DataBind();
       }
   }
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       //Check if it's header
       if (e.Row.RowType == DataControlRowType.Header)
       {
                 //Create new Row in Gridview
         GridViewRow  row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Insert);
             //create Cell and it will be added to row
          TableCell   cell = new TableCell();
           cell.Text = "0";
           row.Cells.Add(cell);


           TableCell cell1 = new TableCell();
           cell1.Text = "Item Name Test";
           row.Cells.Add(cell1);

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

         TableCell  cell3 = new TableCell();
           cell3.Text = "ssss";
           row.Cells.Add(cell3);
                //add the row to Gridview and AddAT(Index of where it will be appear
            GridView1.Controls[0].Controls.AddAt(1, row);
       }
   }



Best regards
M.Mitwalli


This may help you

http://geekswithblogs.net/dotNETvinz/archive/2009/06/04/adding-dynamic-rows-in-gridview-with-textboxes.aspx[^]

http://stackoverflow.com/questions/7864991/adding-new-rows-dynamically-in-a-grid-view-or-datatable-in-asp-net[^]


See here may help you

http://www.itprojectsforyou.com/gridview.php[^]


这篇关于如何在不使用数据源控件的情况下将新行添加到GridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆