首页 > 其他 > 详细

移除GridView中的重复项

时间:2015-02-03 20:51:43      阅读:348      评论:0      收藏:0      [点我收藏+]

1. The HTML Markup

  <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White">
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
                    <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
                    <asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="150" />
                </Columns>
            </asp:GridView>
            <br />
            <asp:Button ID="btnRemove" runat="server" Text="Remove Duplicates" OnClick="btnRemove_Click" />
        </div>

 2. 绑定GridView的方法

 public void BindGridview()
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("City") });
            dt.Rows.Add(1, "John Hammond", "United States");
            dt.Rows.Add(1, "John Hammond", "United States");
            dt.Rows.Add(2, "Mudassar Khan", "India");
            dt.Rows.Add(2, "Mudassar Khan", "India");
            dt.Rows.Add(3, "Suzanne Mathews", "France");
            dt.Rows.Add(3, "Suzanne Mathews", "France");
            dt.Rows.Add(3, "Suzanne Mathews", "France");
            dt.Rows.Add(4, "Robert Schidner", "Russia");
            dt.Rows.Add(4, "Robert Schidner", "Russia");
            dt.Rows.Add(4, "Robert Schidner", "Russia");
            dt.Rows.Add(4, "Robert Schidner", "Russia");

            ViewState["dt"] = dt;
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
       

 3. 移除重复项的方法

 public void RemoveDuplicateReords()
        {
            DataTable dt = (DataTable)ViewState["dt"];
            dt = dt.DefaultView.ToTable(true, "Id", "Name", "City");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

 更多的详细信息,请访问下面的链接

http://www.aspsnippets.com/Articles/Remove-Delete-Duplicate-Rows-Records-from-DataTable-using-C-and-VBNet.aspx

 

移除GridView中的重复项

原文:http://www.cnblogs.com/songxia/p/4270812.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!