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. get data in background for datagridview

get data in background for datagridview

Scheduled Pinned Locked Moved C#
cssdatabasequestion
3 Posts 2 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.
  • S Offline
    S Offline
    Seishin
    wrote on last edited by
    #1

    Hi! I'm using a dataGridView (dgv) bound to dataSet through bindingSource. one method (fired by button.click) fills the dataSet using dataAdapter. the dgv displays loaded rows. now what my goal is: when a row is displayed additional data for it is loaded in a background thread, and the row is refreshed (i'm using grid's overriden paint event to display the additional data) the reason for this is that I want to get and display the additional data only if the row is ever displayed, to avoid downloading (from sqldb) data that is not needed.. my current solution is: public void GetPricesForRow(DataGridViewRow row) { if(backgroundThread != null) while(backgroundThread.ThreadState == System.Threading.ThreadState.Running) Thread.Sleep(3); backgroundThread = new Thread(new ThreadStart(delegate() { try { int colCt = row.DataGridView.Columns.Count; string[] vals = GetAdditionalData(row.Cells[1].Value.ToString().Trim()); row.Cells[colCt - 2].Value = vals[0]; row.Cells[colCt - 1].Value = vals[1]; row.Refresh(); } catch { } })); backgroundThread.Start(); } row.Refresh(); fires: public void Refresh() { this.DataGridView.InvalidateRow(this.Index); } well it works.. but pretty slow.. when i set backgroundThread.IsBackground to true the row doesn't refresh (at least not always) anybody knows a better solution?

    life is study!!!

    H S 2 Replies Last reply
    0
    • S Seishin

      Hi! I'm using a dataGridView (dgv) bound to dataSet through bindingSource. one method (fired by button.click) fills the dataSet using dataAdapter. the dgv displays loaded rows. now what my goal is: when a row is displayed additional data for it is loaded in a background thread, and the row is refreshed (i'm using grid's overriden paint event to display the additional data) the reason for this is that I want to get and display the additional data only if the row is ever displayed, to avoid downloading (from sqldb) data that is not needed.. my current solution is: public void GetPricesForRow(DataGridViewRow row) { if(backgroundThread != null) while(backgroundThread.ThreadState == System.Threading.ThreadState.Running) Thread.Sleep(3); backgroundThread = new Thread(new ThreadStart(delegate() { try { int colCt = row.DataGridView.Columns.Count; string[] vals = GetAdditionalData(row.Cells[1].Value.ToString().Trim()); row.Cells[colCt - 2].Value = vals[0]; row.Cells[colCt - 1].Value = vals[1]; row.Refresh(); } catch { } })); backgroundThread.Start(); } row.Refresh(); fires: public void Refresh() { this.DataGridView.InvalidateRow(this.Index); } well it works.. but pretty slow.. when i set backgroundThread.IsBackground to true the row doesn't refresh (at least not always) anybody knows a better solution?

      life is study!!!

      H Offline
      H Offline
      Hayder Marzouk
      wrote on last edited by
      #2

      Hi, Use VirtualMode option of the datagridview. U can find examples in the MSDN HTH. Hayder Marzouk

      1 Reply Last reply
      0
      • S Seishin

        Hi! I'm using a dataGridView (dgv) bound to dataSet through bindingSource. one method (fired by button.click) fills the dataSet using dataAdapter. the dgv displays loaded rows. now what my goal is: when a row is displayed additional data for it is loaded in a background thread, and the row is refreshed (i'm using grid's overriden paint event to display the additional data) the reason for this is that I want to get and display the additional data only if the row is ever displayed, to avoid downloading (from sqldb) data that is not needed.. my current solution is: public void GetPricesForRow(DataGridViewRow row) { if(backgroundThread != null) while(backgroundThread.ThreadState == System.Threading.ThreadState.Running) Thread.Sleep(3); backgroundThread = new Thread(new ThreadStart(delegate() { try { int colCt = row.DataGridView.Columns.Count; string[] vals = GetAdditionalData(row.Cells[1].Value.ToString().Trim()); row.Cells[colCt - 2].Value = vals[0]; row.Cells[colCt - 1].Value = vals[1]; row.Refresh(); } catch { } })); backgroundThread.Start(); } row.Refresh(); fires: public void Refresh() { this.DataGridView.InvalidateRow(this.Index); } well it works.. but pretty slow.. when i set backgroundThread.IsBackground to true the row doesn't refresh (at least not always) anybody knows a better solution?

        life is study!!!

        S Offline
        S Offline
        Seishin
        wrote on last edited by
        #3

        ok. i got it.. public bool GetPricesForRow(DataGridViewRow row) { ManualResetEvent doneEvent = new ManualResetEvent(false); PricesForRow pfr = new PricesForRow(row.Cells[1].Value.ToString().Trim(), doneEvent); bool x = ThreadPool.QueueUserWorkItem(pfr.ThreadPoolCallback); WaitHandle.WaitAll(new WaitHandle[] { doneEvent }); int colCt = row.DataGridView.Columns.Count; row.Cells[colCt - 2].Value = pfr.OutBold; row.Cells[colCt - 1].Value = pfr.OutNormal; row.Refresh(); return x; } and an additional class: class PricesForRow { ManualResetEvent _doneEvent; string _param; string _outBold; public string OutBold { get { return _outBold; } set { _outBold = value; } } string _outNormal; public string OutNormal { get { return _outNormal; } set { _outNormal = value; } } public PricesForRow(string param, ManualResetEvent doneEvent) { _param = param; _doneEvent = doneEvent; } public void ThreadPoolCallback(Object threadContext) { try { string[] vals = DataBase.Instance.GetKtmsPrices(_param); _outBold = vals[0]; _outNormal = vals[1]; _doneEvent.Set(); } catch { } } } this works just fine!! doesn't slowdown the grid or anything..

        life is study!!!

        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