2013-07-29 64 views
-2

我目前向我的winform應用程序中的多個datagridviews添加行,因爲當某些東西不存在時,索引超出範圍。我和我的團隊進行了一次實時會議,他們希望所有的項目都在一個單獨的datagridview中。我試圖用for和for each statement,但是如果第一項存在,第二項不存在,第三項存在,我的項目仍然超出範圍。下面我提供了我目前的IF聲明。任何可用於顯示行的方法,無論其索引如何?我當前的索引是硬編碼的,這是我通過多個datagridview工作的唯一方式。預先感謝任何協助其他程序員!添加行到datagridview沒有索引C#

//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; 
        } 
+0

你看過'MDSN's' example/help how to do this [DataGrid Row MSDN](http://msdn.microsoft.com/en-us/library /system.windows.forms.datagridview.rows.aspx) – MethodMan

回答

0

您每次都添加新行並使用行索引,因此它永遠不會超出範圍。如果有任何列信息丟失,你應該查找列,然後它會拋出索引超出範圍異常。檢查你所使用的所有數據gridviews

+0

有人可以解釋爲什麼這是downvoted?我問了一個非常詳細的問題,並提供了一個傑出的答案。只是好奇。謝謝! – clerktech