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. DataTable / DataRow / someDataTable.Select() Issue

DataTable / DataRow / someDataTable.Select() Issue

Scheduled Pinned Locked Moved C#
databasecsharphelpcssquestion
5 Posts 3 Posters 0 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.
  • H Offline
    H Offline
    Harvey Saayman
    wrote on last edited by
    #1

    Hey Guys i need to filter the data in my DataGridView, when the window opens i query my db and bind the data to a data table and the to the data grid... this works just fine.. now the user must be able so filter these rows, here is my current code...private void btnSearch_Click(object sender, EventArgs e) { DataTable filterTable = new DataTable(); string filterString = null; string action = cmbAction.SelectedItem.ToString(); switch(action) { case"All": filterString = ""; break; case"Add": filterString = "Action='add'"; break; case"Edit": filterString = "Action='edt'"; break; case"Delete": filterString = "Action='del'"; break; } DataRow[] filteredRowsArray = table.Select(filterString); MessageBox.Show("Rows = " + filteredRowsArray.Length); if (filteredRowsArray.Length == 0) { //moan } else { filterTable.Clear(); foreach (DataRow newRow in filteredRowsArray) { filterTable.ImportRow(newRow); //this seems to be the problem... } dataGridView.DataSource = filterTable; } }
    everything is fine up untill i call filterTable.ImportRow(newRow), i think its adding BLANK rows... if i foreach filterTable.Rows the rows are empty :confused: i also tried filterTable.Rows.Add(newRow); but then it moans "This row already belongs to another table." anybody hav some ideas? Thanx

    Harvey Saayman - South Africa Junior Developer .Net, C#, SQL think BIG and kick ASS

    you.suck = (you.passion != Programming)
    
    S S 2 Replies Last reply
    0
    • H Harvey Saayman

      Hey Guys i need to filter the data in my DataGridView, when the window opens i query my db and bind the data to a data table and the to the data grid... this works just fine.. now the user must be able so filter these rows, here is my current code...private void btnSearch_Click(object sender, EventArgs e) { DataTable filterTable = new DataTable(); string filterString = null; string action = cmbAction.SelectedItem.ToString(); switch(action) { case"All": filterString = ""; break; case"Add": filterString = "Action='add'"; break; case"Edit": filterString = "Action='edt'"; break; case"Delete": filterString = "Action='del'"; break; } DataRow[] filteredRowsArray = table.Select(filterString); MessageBox.Show("Rows = " + filteredRowsArray.Length); if (filteredRowsArray.Length == 0) { //moan } else { filterTable.Clear(); foreach (DataRow newRow in filteredRowsArray) { filterTable.ImportRow(newRow); //this seems to be the problem... } dataGridView.DataSource = filterTable; } }
      everything is fine up untill i call filterTable.ImportRow(newRow), i think its adding BLANK rows... if i foreach filterTable.Rows the rows are empty :confused: i also tried filterTable.Rows.Add(newRow); but then it moans "This row already belongs to another table." anybody hav some ideas? Thanx

      Harvey Saayman - South Africa Junior Developer .Net, C#, SQL think BIG and kick ASS

      you.suck = (you.passion != Programming)
      
      S Offline
      S Offline
      Sandeep Kumar
      wrote on last edited by
      #2

      hi... what your thinking is rite..... If the DataRow that is passed as a parameter is in a detached state, it is ignored, and no exception is thrown while importing the datarow...

      Regards, Sandeep Kumar.V

      H 1 Reply Last reply
      0
      • S Sandeep Kumar

        hi... what your thinking is rite..... If the DataRow that is passed as a parameter is in a detached state, it is ignored, and no exception is thrown while importing the datarow...

        Regards, Sandeep Kumar.V

        H Offline
        H Offline
        Harvey Saayman
        wrote on last edited by
        #3

        okay... then how do i get my desired result?

        Harvey Saayman - South Africa Junior Developer .Net, C#, SQL think BIG and kick ASS

        you.suck = (you.passion != Programming)
        
        1 Reply Last reply
        0
        • H Harvey Saayman

          Hey Guys i need to filter the data in my DataGridView, when the window opens i query my db and bind the data to a data table and the to the data grid... this works just fine.. now the user must be able so filter these rows, here is my current code...private void btnSearch_Click(object sender, EventArgs e) { DataTable filterTable = new DataTable(); string filterString = null; string action = cmbAction.SelectedItem.ToString(); switch(action) { case"All": filterString = ""; break; case"Add": filterString = "Action='add'"; break; case"Edit": filterString = "Action='edt'"; break; case"Delete": filterString = "Action='del'"; break; } DataRow[] filteredRowsArray = table.Select(filterString); MessageBox.Show("Rows = " + filteredRowsArray.Length); if (filteredRowsArray.Length == 0) { //moan } else { filterTable.Clear(); foreach (DataRow newRow in filteredRowsArray) { filterTable.ImportRow(newRow); //this seems to be the problem... } dataGridView.DataSource = filterTable; } }
          everything is fine up untill i call filterTable.ImportRow(newRow), i think its adding BLANK rows... if i foreach filterTable.Rows the rows are empty :confused: i also tried filterTable.Rows.Add(newRow); but then it moans "This row already belongs to another table." anybody hav some ideas? Thanx

          Harvey Saayman - South Africa Junior Developer .Net, C#, SQL think BIG and kick ASS

          you.suck = (you.passion != Programming)
          
          S Offline
          S Offline
          sarvesh upadhyay
          wrote on last edited by
          #4

          I think instead of using DataRow[] filteredRowsArray = table.Select(filterString); and adding the above row to another table. You should use DataView objdv = table.DefaultView; and then Use objdv.RowFilter = filterstring. It is more easy and fast compared to your above steps. You can get more example on codeproject and google on it. I think it will help u and may solve ur problem.

          Sarvesh Upadhyay Senior Software Engineer Birlasoft India Ltd. Microsoft Certified Professional Developer in Dotnet 2.0 Enterprise Application

          H 1 Reply Last reply
          0
          • S sarvesh upadhyay

            I think instead of using DataRow[] filteredRowsArray = table.Select(filterString); and adding the above row to another table. You should use DataView objdv = table.DefaultView; and then Use objdv.RowFilter = filterstring. It is more easy and fast compared to your above steps. You can get more example on codeproject and google on it. I think it will help u and may solve ur problem.

            Sarvesh Upadhyay Senior Software Engineer Birlasoft India Ltd. Microsoft Certified Professional Developer in Dotnet 2.0 Enterprise Application

            H Offline
            H Offline
            Harvey Saayman
            wrote on last edited by
            #5

            Hey sarvesh i came across the DataView class just after i posted my messages and did infact use it the way you just said and it worked :) im gona try and use multiple conditions in the filterstring today, ill leave a post if i get stuck again thanx for the reply...

            Harvey Saayman - South Africa Junior Developer .Net, C#, SQL think BIG and kick ASS

            you.suck = (you.passion != Programming)
            
            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