remove other DataRows from your DataTable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zhshqzyc
    New Member
    • Feb 2008
    • 9

    remove other DataRows from your DataTable

    remove other DataRows from your DataTable

    Hi, I want to display the top three rows of the table.
    So I just want to remove the other rows.
    Suppose the datas are not from a database.
    How to do it?

    Thanks
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Originally posted by zhshqzyc
    remove other DataRows from your DataTable

    Hi, I want to display the top three rows of the table.
    So I just want to remove the other rows.
    Suppose the datas are not from a database.
    How to do it?

    Thanks
    You could use a for loop and make the boundary 3 so that it will only loop through the 1st 3 rows of your DataTable and return the data for the 1st 3 rows.

    Nathan

    Comment

    • zhshqzyc
      New Member
      • Feb 2008
      • 9

      #3
      I need more details because I tried to deleted the extra lines but the deleted parts are still displayed. Why? Thanks for your help.



      Code:
      protected void Button1_Click(object sender, EventArgs e)
              {
                  DataTable t1 = new DataTable();
                  t1 = GetTable();
                  for (int i = 3; i < t1.Rows.Count; i++)
                      t1.Rows.RemoveAt(i);
                  DataView dv = new DataView(t1);
                  dv.Sort = "Temperature ASC";
                  GridView1.DataSource = dv;
                  GridView1.DataBind();
              }
      
              public DataTable GetTable()
              {
                  DataSet newSet = new DataSet();
                  DataTable Index = new DataTable("Index");
                  GenerateDataSet(newSet,Index);
                  return Index;
              }
      .

      Comment

      • nateraaaa
        Recognized Expert Contributor
        • May 2007
        • 664

        #4
        Originally posted by zhshqzyc
        I need more details because I tried to deleted the extra lines but the deleted parts are still displayed. Why? Thanks for your help.



        Code:
        protected void Button1_Click(object sender, EventArgs e)
        {
        DataTable t1 = new DataTable();
        t1 = GetTable();
        for (int i = 3; i < t1.Rows.Count; i++)
        t1.Rows.RemoveAt(i);
        DataView dv = new DataView(t1);
        dv.Sort = "Temperature ASC";
        GridView1.DataSource = dv;
        GridView1.DataBind();
        }
         
        public DataTable GetTable()
        {
        DataSet newSet = new DataSet();
        DataTable Index = new DataTable("Index");
        GenerateDataSet(newSet,Index);
        return Index;
        }
        .
        If you only want the 1st 3 records you need your for loop set up like this
        Code:
         for(int i = 0; i < 3; i++) 
        {
        //add each row to your datatable or dataview here
        }
        Nathan

        Comment

        Working...