Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to get another DataTable from original DataTable?

How to get another DataTable from original DataTable?

Scheduled Pinned Locked Moved C#
helptutorialquestion
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nhatvhm
    wrote on last edited by
    #1

    Here, it is source code

    public DataTable SearchField(string strSearch, string strFieldName)
    {
    DataTable dt = new DataTable();

            foreach (DataRow dr in data.Rows)
            {
                string strT = dr\[strFieldName\].ToString();
                if (stringSearchMP(strSearch, strT) != -1)
                {
                    DataRow row = dr;
                    dt.ImportRow(row);
                }
            }
            return dt;
        }
    

    I don't know to cause why my function return null. Because, DataGridView.DataSource = dt can't show data. Anyone help me?:confused: Anyway, thanks so much!!!

    A M N 3 Replies Last reply
    0
    • N nhatvhm

      Here, it is source code

      public DataTable SearchField(string strSearch, string strFieldName)
      {
      DataTable dt = new DataTable();

              foreach (DataRow dr in data.Rows)
              {
                  string strT = dr\[strFieldName\].ToString();
                  if (stringSearchMP(strSearch, strT) != -1)
                  {
                      DataRow row = dr;
                      dt.ImportRow(row);
                  }
              }
              return dt;
          }
      

      I don't know to cause why my function return null. Because, DataGridView.DataSource = dt can't show data. Anyone help me?:confused: Anyway, thanks so much!!!

      A Offline
      A Offline
      AB7771
      wrote on last edited by
      #2

      What actually do u want to do? can u explain in detail.

      Thanks & Regards, Pramod "Everyone is a genius at least once a year"

      N 1 Reply Last reply
      0
      • N nhatvhm

        Here, it is source code

        public DataTable SearchField(string strSearch, string strFieldName)
        {
        DataTable dt = new DataTable();

                foreach (DataRow dr in data.Rows)
                {
                    string strT = dr\[strFieldName\].ToString();
                    if (stringSearchMP(strSearch, strT) != -1)
                    {
                        DataRow row = dr;
                        dt.ImportRow(row);
                    }
                }
                return dt;
            }
        

        I don't know to cause why my function return null. Because, DataGridView.DataSource = dt can't show data. Anyone help me?:confused: Anyway, thanks so much!!!

        M Offline
        M Offline
        Morven Huang
        wrote on last edited by
        #3

        maybe you need ot clone the table structure from source datatable,like 'dt=data.Clone()',then import rows.

        N 1 Reply Last reply
        0
        • M Morven Huang

          maybe you need ot clone the table structure from source datatable,like 'dt=data.Clone()',then import rows.

          N Offline
          N Offline
          nhatvhm
          wrote on last edited by
          #4

          Exactly, I think so,

          1 Reply Last reply
          0
          • A AB7771

            What actually do u want to do? can u explain in detail.

            Thanks & Regards, Pramod "Everyone is a genius at least once a year"

            N Offline
            N Offline
            nhatvhm
            wrote on last edited by
            #5

            I think of my explain very clearly for you. anyway, thank you so much. I get it and i get answer, have a good day!

            1 Reply Last reply
            0
            • N nhatvhm

              Here, it is source code

              public DataTable SearchField(string strSearch, string strFieldName)
              {
              DataTable dt = new DataTable();

                      foreach (DataRow dr in data.Rows)
                      {
                          string strT = dr\[strFieldName\].ToString();
                          if (stringSearchMP(strSearch, strT) != -1)
                          {
                              DataRow row = dr;
                              dt.ImportRow(row);
                          }
                      }
                      return dt;
                  }
              

              I don't know to cause why my function return null. Because, DataGridView.DataSource = dt can't show data. Anyone help me?:confused: Anyway, thanks so much!!!

              N Offline
              N Offline
              nhatvhm
              wrote on last edited by
              #6

              I think you mistake and omit line dt = data.Clone() before foreach(DataRow dr in data.Rows)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups