-1

I'm having multiple gridview in my form and one of gridview rows is painted based on data.

enter image description here

But it looks weird when I load the form, it's like controls not having proper rendering on form.

Please look at this video for better understanding:

halfer
  • 19,824
  • 17
  • 99
  • 186
Nirav Parsana
  • 155
  • 1
  • 17
  • If possible, can u switch to a `DatagridView`? – Software Dev Mar 25 '18 at 16:13
  • I'm using datagridview only. :) – Nirav Parsana Mar 25 '18 at 16:14
  • what is the problem? What looks weird? – haku Mar 25 '18 at 16:14
  • @NoSaidTheCompiler please see video i've mentioned in link. – Nirav Parsana Mar 25 '18 at 16:15
  • I see *(or better to say I think I see)* only a single error on the video, This error I see on rendering of almost all elements of all application taht use a Intel CPU build in graphics card. Until I changed my (home and work) computer to use a Intel build-in graphics card, I did not see this error *(or at least, it was very rare)*. Now I see this "duplication of displayed data" on LibreOffice, Opera, Chrome, Firefox, everywhere (when scrolling). – Julo Mar 25 '18 at 16:21
  • @Julo So did you mean that there nothing we can do with windows application, right? I'm confused! – Nirav Parsana Mar 25 '18 at 16:26
  • Show your loading code, not your pictures. – LarsTech Mar 25 '18 at 16:38
  • @NiravParsana, if this 'effect' is all the time, the it is probably something different. The problem I have seen is (as far as I know) limited to HW *(probably drivers)* and has not 100 % probability. In this case, there is a chance that you can do something with it. Try invalidate the area, to request redraw. When it is the error I have describe, it should change when the controls are redrawn *(e.g. mouse over effect should change the state, when you browse mouse over controls)* – Julo Mar 25 '18 at 16:43
  • 2
    https://stackoverflow.com/a/89125/17034 – Hans Passant Mar 25 '18 at 17:59
  • @HansPassant I've found your solution working. It keeps UI thread busy for sometime but still far better than previous results. Very Impressed! Thank You :) – Nirav Parsana Mar 26 '18 at 04:18
  • If a question relies on a "see video" link then it is off-topic, since when the video is moved/deleted, the question will not be useful to future readers. Please convert that to an embeddable GIF, or show the problem in still images instead. – halfer Mar 26 '18 at 10:03
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Mar 26 '18 at 10:03
  • To me it looks like your UI is stretched and positioned according to the DataGridViews. When your form loads, it tries to place the controls, but keeps shifting them around as each added row in your DataGridView changes the whole UI? Try disabling the queries temporarely and see how the UI behaves. Add a button to test executing the queries After the UI has loaded, if it makes a difference. I'd also try fixed sizes and positions to controls if nothing else helps. – W0lfw00ds Mar 26 '18 at 11:00

2 Answers2

0

From the video what I could understand is it is taking time to Load,

If you have written the ADO.NET(to get data from DB) on Load Method, it will take a moment to fetch and display them.

I would advise you to use individual threads(for each Grid) to fetch data during the Load, to Optimize the performance.

0

I am not sure if this will solve your problem entirely but here are some tips/tricks :

! . If you are updating the Datagridview source, try setting AutoSizeColumnsMode to none before updating the datasource.This has been proven to work.

2 . There's a property called DoubleBuffer.I am not gonna dig into the description of it but in easy words , it will help u to improve your performance significantly :)

static class ExtensionMethods
{
public void DoubleBuffered(DataGridView dgv, bool setting)
{
    Type dgvType = dgv.GetType();
    PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
    pi.SetValue(dgv, setting, null);
}
}
  ExtensionMethods.DoubleBuffered(mydgvw, True);

3 . Last but not the least, when displaying data in dgvw from a datasource, sometimes it is helpful to use collections like ObserveableCollection or if possible,try to reduce the amount of displayed data through filtering.

4 . Using a DataReader over a DataAdapter is significantly faster.Create a class to manage your data,use a DataReader instead of a DataAdapter/DataTable. E.g.

 public class Employees
{
    private int age;

    public int Age
    {
        get { return age; }
        set { age = value; }
    }
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private int id;

    public int Id
    {
        get { return id; }
        set { id = value; }
       } 
    }

  ///create an array

 ArrayList myData = new ArrayList();

 ///Now,let ur datareader do the job for you .

 while (reader.Read())
           {
               Employees emp = new Employees();
               emp.Id = (int)reader[0];
               emp.Name = reader[1].ToString();
               emp.Age = (int)reader[2];
               myData.Add(m);
           }
           dataGridView1.DataSource = myData;

4 . Another best bet is,if u use a Datareader and follow the above code,make sure to run it in the background on the main form load, then on button clicks , just set the dataGridView.DataSource to the array....

These are the basics but hope they'll help you :)

Software Dev
  • 5,368
  • 5
  • 22
  • 45
  • I tried what you suggested, but sadly i don't see it working. Still the same issue. – Nirav Parsana Mar 25 '18 at 16:36
  • i don't really see any issue in ur video...can u explain a bit ? – Software Dev Mar 25 '18 at 16:37
  • Did you notice when i click on 'Lessons', how much time it takes to show the form and once form starts loading, one by one controls appears. – Nirav Parsana Mar 25 '18 at 16:54
  • If u are using a datatable,that is a bad choice,are u using it ? – Software Dev Mar 25 '18 at 16:58
  • Yes, that's where i load data from database – Nirav Parsana Mar 25 '18 at 17:09
  • @NiravParsana there is still one more thing you can do, to speed up 'loading'. You can disable control rendering when data are changed. I'm not so sure how this function is used/activated in C# *(perhaps `SuspendLayout`)*, but I saw this function used few years ago in Borland. Sorry I newer really did research this in C#, but as from my experience, GUI rendering in C# is slow, when many controls are used *(tested on form with 120+ controls, mostly check boxes and radio buttons)*. And one more, updating `DataGird` *(or similar control)* with more than 1000 elements took about a 5 seconds. – Julo Mar 25 '18 at 17:09
  • Using a datareater over a dataadpter is significantly faster... I will update my answer :) – Software Dev Mar 25 '18 at 17:10
  • @zackraiyan Thanks for all the solution. I've implemented some of them as I've too complex code to work with. So, I can't apply all of them but solution provided by you and HansPassant helped me to reduce issue. – Nirav Parsana Mar 26 '18 at 15:04
  • Please mark the answer as answer and uovite it if ti helped – Software Dev Mar 26 '18 at 15:05