سؤال

I am currently adding rows to multiple datagridviews in my winform application due to when something does not exist, the index is out of range. I did a live meeting with my team and they would like all of the items to be in one single datagridview. I have tried to use a for and for each statement but still my items were out of range if the 1st item existed, the second one did not and the third existed. I have provided my current IF statement below. Any methods available to display rows regardless of their index? My current indexes are hardcoded which is the only way I got it to work across multiple datagridviews. Thanks in advance for any assistance fellow programmers!!

 //add link to row for case selected
             #region "if statements for eFacts, History, Summary and Driving history"


                    //this row will always exist first
                    dataGridViewMiddle.Rows.Add(1);
                    dataGridViewMiddle[0, 0].Value = "eFacts";
                    dataGridViewMiddle[1, 0].Value = eFacts.Text;

                    //add link to History row for case selected
                    if (caseCat == "CF" || caseCat == "MM" || caseCat == "CT" || caseCat == "CJ")
                    {
                        dataGridViewMiddle.Rows.Add(1);
                        dataGridViewMiddle[0, 1].Value = "Arrest History";
                        dataGridViewMiddle[1, 1].Value = Arrest_History.Text;
                    }

                    //add link to Summary row for case selected
                    if (caseSummary.Contains(caseCat))
                    {
                        DGVsummary.Rows.Add(1);
                        DGVsummary[0, 0].Value = "Summary";
                        DGVsummary[1, 0].Value = Summary.Text;
                        DGVsummary.Visible = true;
                    }
                    else
                    {
                        DGVsummary.Visible = false;
                    }

                    //add link to Driving History row for case selected
                    if (license != "")
                    {
                        DGVdriverLicense.Rows.Add(1);
                        DGVdriverLicense[0, 0].Value = DLnumber.Text;
                        DGVdriverLicense[1, 0].Value = Driving_History.Text;
                        DGVdriverLicense.Visible = true;
                    }
هل كانت مفيدة؟

المحلول

You are adding new row each time and using the row index so it can never go out of range. You should look for the columns if any column info is missing then it would throw the index out of range exception. Check it for all data gridviews you're using

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top