2014-07-07 68 views
0

我遇到了一個非常奇怪的問題,動態構建DataGridView和一組單選按鈕。我有一組單選按鈕和一個Panel中的DataGridView。我遇到的問題是我試圖使用某些單選按鈕上的事件處理函數來獲取DataGridView中的行數。我使用的DataTable有3行。您可以手動將問題重新添加到DGV中,這並不重要。當我打電話rows.count後,我將其初始化工作按計劃進行,並返回3,但是當一個事件處理程序被觸發行計數恢復爲0.我的代碼如下DataGridView中:DataGridView行數0事件處理程序後

private DataSet testData; 
    private DataTable testDataTable; 

    public Form1(API _api) 
    { 
     InitializeComponent(); 
     api = _api; 
     businessLayer = new BusinessLayer(); 

     testData = api.getDataSet(); 
     testDataTable = businessLayer.getDataTable(testData); 

     buildDataGridView(); 

    } 

    // Build the DataGridView 
    private void buildDataGridView() 
    { 
     int numOfRows = testDataTable.Rows.Count; 

     DataGridView testDataGridView = new DataGridView(); 
     testDataGridView.Columns.Add("Description", ""); 
     testDataGridView.Columns.Add("Name", ""); 
     testDataGridView.Rows.Add(numOfRows); 
     testDataGridView.Location = new Point(150, 20); 

     for (int i = 0; i < numOfRows; i++) 
     { 
      RadioButton rbutton = new RadioButton(); 
      rbutton.Name = testDataTable.Rows[i].Field<string>("Name").ToString() + "RadioButton"; 
      rbutton.Text = testDataTable.Rows[i].Field<string>("Name"); 
      rbutton.Tag = testDataTable.Rows[i].Field<string>("SortOrder"); 
      rbutton.CheckedChanged += rbutton_CheckedChanged; 
      if (i == 0) 
      { 
       rbutton.Checked = true; 
      } 
      rbutton.Location = new Point(5, 20 * (i + 1)); 
      testPanel.Controls.Add(definition); 
     } 

     testPanel.Controls.Add(testDataGridView); 

     textBox1.Text = testDataGridView.Rows.Count.ToString(); 
    } 

    // RadioButton CheckChanged EventHandler 
    private void definition_CheckedChanged(object sender, EventArgs e) 
    { 
     RadioButton rb = (RadioButton)sender; 
     if(rb.Checked) 
     { 
      textBox1.Text = testDataGridView.Rows.Count.ToString(); 

     } 
    } 

這真的讓我困惑,但如果我弄清楚,我一定會在這裏學到一些東西。我不知道這是否與DGV和單選按鈕位於面板內部有關,我不知道......感謝任何幫助,並幫助我學習!

+0

'... laborLevelsDataGridView ... testDataGridView ...' – Steve

+0

錯字...忘了將labviewLevelDataGridView的實例重命名爲testDataGridView。如果您重新命名laborLevels進行測試,您會看到我描述的行爲仍然存在 – waltmagic

回答

0

由於Grant Winney在註釋中解釋了buildDataGridView()中引用的DataGridView和definition_CheckedChanged()中引用的DataGridView不一樣......內存堆和內存堆棧的示例。以下是我正在嘗試做的一個更簡單的例子:

public partial class TestingForm2 : Form 
{ 
    private DataTable testDataTable; 
    // Must Declare here so the value is stored in heap memory 
    // so other functions can access and use the value 
    private DataGridView testDataGridView; 

    public TestingForm2() 
    { 
     InitializeComponent(); 

     buildDataTable(); 
     buildDataGridView(); 
     buildRadioButtons(); 
    } 

    private void buildRadioButtons() 
    { 
     int numOfRadioButtons = testDataTable.Rows.Count; 

     for(int i = 0; i < numOfRadioButtons; i++) 
     { 
      RadioButton rb = new RadioButton(); 
      rb.Name = testDataTable.Rows[i].Field<string>("Name") + "RadioButton"; 
      rb.Text = testDataTable.Rows[i].Field<string>("Name"); 
      rb.Tag = testDataTable.Rows[i].Field<string>("Number"); 

      if(i == 0) 
      { 
       rb.Checked = true; 
      } 

      rb.Location = new Point(5, 20 * (i + 1)); 
      rb.CheckedChanged += rb_CheckedChanged; 
      panel1.Controls.Add(rb); 
     } 

    } 

    private void buildDataGridView() 
    { 
     // Get number of rows in testDataTable 
     int numOfRows = testDataTable.Rows.Count; 

     // Build the empty DGV 
     // If I were to declare the DGV here the value given would have 
     // been stored in the memory stack and could only be accessed by 
     // this function. This is where I went wrong and made a silly mistake 
     testDataGridView = new DataGridView(); 
     testDataGridView.Columns.Add("Name", ""); 
     testDataGridView.Columns.Add("Number", ""); 
     testDataGridView.Rows.Add(numOfRows); 
     testDataGridView.Location = new Point(150, 20); 
     testDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; 
     testDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; 
     testDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray; 
     testDataGridView.AllowUserToAddRows = false; 
     testDataGridView.AllowUserToDeleteRows = false; 
     testDataGridView.MultiSelect = false; 
     testDataGridView.ColumnHeadersVisible = false; 
     testDataGridView.RowHeadersVisible = false; 

     panel1.Controls.Add(testDataGridView); 
    } 

    private void buildDataTable() 
    { 
     testDataTable = new DataTable(); 

     // Add the Columns 
     testDataTable.Columns.Add("Name"); 
     testDataTable.Columns.Add("Number"); 

     // Create and add the rows 
     for (int i = 0; i < 3; i++) 
     { 
      // Create the new row 
      DataRow dr; 
      object[] rowItems = new object[testDataTable.Columns.Count]; 

      if (i == 0) 
      { 
       rowItems[0] = "Bob"; 
       rowItems[1] = "1"; 
      } 
      else if (i == 1) 
      { 
       rowItems[0] = "John"; 
       rowItems[1] = "2"; 
      } 
      else 
      { 
       rowItems[0] = "George"; 
       rowItems[1] = "3"; 
      } 

      // Add the row 
      dr = testDataTable.NewRow(); 
      dr.ItemArray = rowItems; 
      testDataTable.Rows.Add(dr); 
     } 
    } 

    private void TestingForm2_Load(object sender, EventArgs e) 
    { 
     textBox1.Text = testDataGridView.Rows.Count.ToString(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     textBox1.Text = testDataGridView.Rows.Count.ToString(); 
    } 

    private void rb_CheckedChanged(object sender, EventArgs e) 
    { 
     RadioButton rb = (RadioButton)sender; 
     int index = int.Parse(rb.Tag.ToString()) - 1; 
     if (rb.Checked) 
     { 
      testDataGridView.Rows[index].Selected = true; 
     } 

    } 
} 

對不起,每個人都有錯誤!謝謝您的幫助!