How to do custom pagination on a data gridview control with custom attributes mapped to a down grid

耗尽温柔 提交于 2021-01-29 20:13:30

问题


I have a windows application where on the click of a button it fetches data from a Oracle query and when binding the gridview datasource to a datatable it takes almost 5 mins to load for 3500 records .

The final gridview looks like this which is again dependent on data in the down form .on the click of any row in the first grid the second grid data should also change but now it takes huge time and seems like it is kind of doing any background task which am unable to get as am pretty new to winforms n its events.

So the only thing now left would be doing a pagination with may be 50 rows of data in first grid and same 50 rows for second grid which would then change according and a next button to fetch data until rowcount in datatable.

Attached the PIc of how the final datagridview now looks-

Initial code hit is here -

   [CommandMethod(Constants.CommandViewJobHistory, CommandFlags.Interruptible | CommandFlags.NoInternalLock | CommandFlags.Session | CommandFlags.Redraw)]
        public static void ViewJobHistoryCmd()
        {
            try
            {
                ViewJobData.ViewJobHistory(WorkspaceManager.map3DApplication);
            }
            catch (System.Exception ex)
            {
                Constants.LogError.Info(CommonResource.LoggerTitle, ex);
                throw;
            }

        }

Then it comes here to ViewJobHistory method-

 public static void ViewJobHistory(Autodesk.Map.IM.Forms.Application m3dApplication)
        {            
            try
            {
                if (m3dApplication != null && m3dApplication.Documents != null && m3dApplication.Documents.Count > 0 && m3dApplication.Documents.Active != null)
                {
                    Document imDocument = m3dApplication.Documents.Active;
                    if (imDocument.Connection.IsJobEnabled)
                    {
                        Job selectedJob = imDocument.Connection.Jobs.Selected;
                        if (selectedJob != null)
                        {
                            if (selectedJob.Name == "Live")
                                MessageBox.Show("Active job name is Live. Please select the job & Continue", JobResource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            else if (selectedJob.Name == "Pending")
                                MessageBox.Show("Active job name is Pending. Please select the job & Continue", JobResource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            else
                            {
                                ViewSelectedJobHistory(imDocument, selectedJob);                                
                            }                        
                        }
                    }
                    else
                        MessageBox.Show("Current model is not job enabled, Please enable job and Continue");
                }
            }
            catch (System.Exception ex)
            {
                ErrorHandlerDelegate.DisplayError(ex, logger);
            }            
        }

After this it hits the ViewSelectedJobHistory method-

 private static void ViewSelectedJobHistory(Document imDocument, Job selectedJob)
        {
            DataTable dataTable = new DataTable();
            try
            {
                if (imDocument != null && selectedJob != null)
                {
                    string query =JobResource.QueryJobHistory;             
                    using (Command imCommand = new Command(query, imDocument.Connection)) 
                    //using (Command imCommand = new Command(string.Format("SELECT FID,F_CLASS_NAME,JOB_OPERATION_TYPE,OPERATION_DATE FROM JOB_EXPLORER_FETCH WHERE JOB_ID={0}", selectedJob.ID), imDocument.Connection))
                    //using (Command imCommand = new Command(query, imDocument.Connection))
                    {
                        imCommand.Parameters.Add(new DataParameter("1", DbDataType.String, selectedJob.ID.ToString(), ParameterDirection.Input)); 
                            using (DataAdapter dataAdapter = new DataAdapter(imCommand))
                            {
                                dataAdapter.Fill(dataTable);
                                if (dataTable != null && dataTable.Rows.Count > 0)
                                {
                                    if (instanceJob == false)
                                    {
                                        ViewSelectedJobHistory viewSelectedJobHistory = new ViewSelectedJobHistory(imDocument, dataTable, selectedJob);
                                        viewSelectedJobHistory.Show();
                                        instanceJob = true;
                                    }
                                }
                                else
                                    MessageBox.Show("No data edited in selected job", JobResource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }                        
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if (dataTable != null)
                    dataTable.Dispose();
            }
        }

Now it comes to to this method from (ViewSelectedJobHistory viewSelectedJobHistory = new ViewSelectedJobHistory(imDocument, dataTable, selectedJob);) which sets the data table to a object and checks connection -

 public ViewSelectedJobHistory(Document im3DDocument, DataTable dataTable, Job selectedJob)
        {
            //ViewSelectedJobHistory viewSelectedJobHstry = SCE.TDBU.Map3D.JobManagement.ViewSelectedJobHistory.insCreation(imApplication);
                InitializeComponent();
                dataTableJobHistory = dataTable;
                imJob = selectedJob;
                imCurrentDocument = im3DDocument;
                GetLiveConnection(imCurrentDocument);

        }

After this this comes to viewSelectedJobHistory.Show(); it hits different method but am not sure how-

private void ViewSelectedJobHistory_Load(object sender, EventArgs e)
        {      
            string path = @"c:\Temp\MyTest.txt";
            DateTime time1 = DateTime.Now;
            try
            {
                this.Cursor = Cursors.WaitCursor;


                if (dataTableJobHistory != null && dataTableJobHistory.Rows.Count > 0)
                {
                    dgvJobEditsHistory.DataSource = null;
                    //dgvJobEditsHistory.AutoSizeColumnsMode = 0;
                    //dgvJobEditsHistory.AutoSizeRowsMode = 0;
                    dgvJobEditsHistory.ColumnHeadersVisible = false;                    
                    dgvJobEditsHistory.DataSource = dataTableJobHistory;
                    dgvJobEditsHistory.ColumnHeadersVisible = true;
                    dgvJobEditsHistory.RowHeadersWidth = 30;
                    dgvJobEditsHistory.Columns[1].Width = 130;
                    dgvJobEditsHistory.Columns[2].Width = 140;
                    dgvJobEditsHistory.Columns[3].Width = 130;
                    jobId.Text += imJob.ID.ToString();
                    jobName.Text += imJob.Name.ToString();
                    CreatedBy.Text += imJob.UserCreated;
                    CreatedDate.Text += imJob.DateCreated.ToString();
                    this.Cursor = Cursors.Default;
                }
            }
            catch (System.Exception ex)
            {
                this.Cursor = Cursors.Default;
                ErrorHandlerDelegate.DisplayError(ex, logger);               
            }

Here when assigning the datasourse of gridview to the datable (dgvJobEditsHistory.DataSource = dataTableJobHistory;) , this is where it takes so much time to execute and while i pause the solution and take a look on "dataTableJobHistory" it throws the below error-

Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because the code is optimized.

I am not sure if this is the issue or not but when it hits dgvJobEditsHistory.DataSource = dataTableJobHistory; when i press f11 it comes here -

private void dgvJobEditsHistory_SelectionChanged(object sender, EventArgs e)
        {

            try
            {

                if (dgvJobEditsHistory != null && dgvJobEditsHistory.Rows.Count > 0 && dgvJobEditsHistory.SelectedRows != null)
                {

                    int index = dgvJobEditsHistory.CurrentCell.RowIndex;
                    DataGridViewRow drgRow = dgvJobEditsHistory.Rows[index];
                    if (drgRow != null)
                    {
                        if (dgvJobEditsHistory.SelectedRows.Count > 1)
                        {
                            Console.WriteLine(DateTime.UtcNow + " entered if rowcount grtr than 1");
                            dicCollection = new Dictionary<long, string>();
                            foreach (DataGridViewRow row in dgvJobEditsHistory.SelectedRows)
                            {
                                dicCollection.Add((Convert.ToInt64(row.Cells[JobResource.FieldFID].Value)), (row.Cells[JobResource.FieldFClassName].Value.ToString()));
                            }
                            Console.WriteLine(DateTime.UtcNow + " exited if rowcount grtr than 1");
                        }
                        else
                        {
                            Console.WriteLine(DateTime.UtcNow + " entered if rowcount not grtr than 1");
                            dicCollection = new Dictionary<long, string>();
                            dicCollection.Add((Convert.ToInt64(drgRow.Cells[JobResource.FieldFID].Value)), (drgRow.Cells[JobResource.FieldFClassName].Value.ToString()));
                            Console.WriteLine(DateTime.UtcNow + " exited if rowcount not grtr than 1");
                        }
                        oldValue = index;
                        fidFeature = drgRow.Cells[JobResource.FieldFID].Value.ToString();
                        string featureClassName = drgRow.Cells[JobResource.FieldFClassName].Value.ToString();
                        string jobOperationType = drgRow.Cells[JobResource.FieldJobOperationType].Value.ToString();//INSERT, UPDATE, DELETE
                        activeJob = imJob;
                        Job liveJob = imCurrentDocument.Connection.Jobs[JobResource.JobStateLive];
                        Console.WriteLine(DateTime.UtcNow + " entered if feature edits not null");
                        using (dtFeatureEditHistory = GetFeatureEditHistory(liveJob, activeJob, fidFeature, featureClassName, jobOperationType))
                        {
                            if (dtFeatureEditHistory != null)
                            {
                                dataGridViewFeatureEdits.DataSource = dtFeatureEditHistory;
                            }
                        }
                        Console.WriteLine(DateTime.UtcNow + " exited if feature edits not null");
                        if (dataGridViewFeatureEdits.Rows.Count > 0)
                        {

                            lst = new ArrayList();
                            attributeCount = new ArrayList();
                            Console.WriteLine(DateTime.UtcNow + " entered for loop featureedits greater than 0");
                            for (int iRow = 0; iRow < dataGridViewFeatureEdits.Rows.Count; iRow++)
                            {
                                attributeCount.Add(dataGridViewFeatureEdits.Rows.Count);
                                DataGridViewCell activeJobValue = dataGridViewFeatureEdits.Rows[iRow].Cells[1];
                                DataGridViewCell liveJobValue = dataGridViewFeatureEdits.Rows[iRow].Cells[2];
                                if (activeJobValue.Value.ToString() != liveJobValue.Value.ToString())
                                {
                                    dataGridViewFeatureEdits.Rows[iRow].Cells[1].Style.ForeColor = Color.Red;

                                    lst.Add(iRow);
                                    activeJobValue.Style.ForeColor = Color.Red;
                                    liveJobValue.Style.ForeColor = Color.Red;

                                }
                            }
                            Console.WriteLine(DateTime.UtcNow + " exited for loop featureedits greater than 0");
                            Console.WriteLine(DateTime.UtcNow + " entered setting column width");
                            dataGridViewFeatureEdits.RowHeadersWidth = 30;
                            dataGridViewFeatureEdits.Columns[0].Width = 130;
                            dataGridViewFeatureEdits.Columns[1].Width = 157;
                            dataGridViewFeatureEdits.Columns[2].Width = 157;
                            Console.WriteLine(DateTime.UtcNow + " exited setting column width");
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                ErrorHandlerDelegate.DisplayError(ex, logger);
            }
            finally
            {
                //multipleFids.Clear();
            }

        }

Finally to Summerize, i tried finding the issue but couldn't find the issue why it takes that much time and now i want to know if i want to make a custom pagination for this how would i do it as i am not sure where to start n where to do changes.

Any help would be highly appreciated.

来源:https://stackoverflow.com/questions/61016895/how-to-do-custom-pagination-on-a-data-gridview-control-with-custom-attributes-ma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!