(开发篇) 控件研究~ 删除gridview中的一行(不与数据库交互,利用datatable删除)

前台代码:

 1         <asp:GridView ID="gv_demonstrate_for_planning_item" runat="server" 

 2             AutoGenerateColumns="False" OnRowDeleting="gv_RowDeleting">

 4             <RowStyle BackColor="#EFF3FB" />

 5             <Columns>

 6                 <asp:BoundField HeaderText="施工单位" DataField="施工单位" />

 7                 <asp:BoundField HeaderText="工程性质" DataField="工程性质" />

 8                 <asp:BoundField HeaderText="本周计划数" DataField="本周计划数" />

 9                 <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />

10             </Columns>

11         </asp:GridView>

后台代码:

 1     /// <summary>

 2     /// 删除某一条记录

 3     /// </summary>

 4     protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)

 5     {

 6         DataTable dt = new DataTable();

 7         dt.Columns.Add(new DataColumn("施工单位",typeof(string)));

 8         dt.Columns.Add(new DataColumn("工程性质", typeof(string)));

 9         dt.Columns.Add(new DataColumn("本周计划数", typeof(string)));

10 

11         if (gv_demonstrate_for_planning_item.Rows.Count != 0)

12         {

13             for (int i = 0; i < gv_demonstrate_for_planning_item.Rows.Count; i++)

14             {

15                 if(e.RowIndex==i)

16                     continue;

17                 DataRow dr = dt.NewRow();

18                 dr["施工单位"] = gv_demonstrate_for_planning_item.Rows[i].Cells[0].Text;

19                 dr["工程性质"] = gv_demonstrate_for_planning_item.Rows[i].Cells[1].Text;

20                 dr["本周计划数"] = gv_demonstrate_for_planning_item.Rows[i].Cells[2].Text;

21                 dt.Rows.Add(dr);

22             }

23         }

24 

25         gv_demonstrate_for_planning_item.DataSource = dt;

26         gv_demonstrate_for_planning_item.DataBind();

27     }

特别说明:


  1.gridview控件不能根据列名定位到某一个单元格,但是可以通过索引定位,从本质上来说,gridview是一个显示的工具

  2.删除列需要在控件代码上添加 OnRowDeleting="gv_RowDeleting" 方法

 

 

效果:

 

  删除前~

 

  删除后~

 

你可能感兴趣的:(Datatable)