2017-01-31 42 views
0

我有兩種形式(Form1,Form2)。 Form1顯示對象的值。 Form2允許用戶修改Form1中某個值的某些屬性。C#更改DataGridViewCell值不起作用

側面說明:(在Form1,我編程方式創建標籤頁,並在每個標籤頁是一個DataGridView中)

我試圖從Form2的新字符串值傳送到一個DataGridView(即我只能從Form1獲得)。

我在Form1中創建了一個公共的Tab控件,可以訪問和搜索DataGridView。

我的代碼不會產生任何錯誤,但DataGridView中的單元格不會像它應該那樣更改。

我在做什麼錯?

//Form1 
//Create public access to the TabControl on Form1 
public TabControl xTabControl 
{ 
    get 
    { 
     return tabControl1; 
    } 
} 



//Form2 
private void btnSubmit_Click(object sender, EventArgs e) 
{ 
    string newText = txtDescription.Text.ToString(); //New Text 
    string tabName = txtTabName.Text.ToString(); //Name of the Tab to reference 

    int txtChanged = Int32.Parse(txtChanged.Text.ToString()); //0 = No Change, 1 = Change 
    int jobID = Int32.Parse(hidJobID.Text.ToString()); //Job ID 
    int rowIndex = Int32.Parse(hidRowIndex.Text.ToString()); //The row we must reference in our DataGridView 

    //Call the public method from Form1 to define the Tab Control 
    TabControl tabControl = Form1.xTabControl; 

    //Define the Tab Page 
    TabPage tbpage = new TabPage(); 
    tbpage = tabControl.TabPages[tabName]; 

    //If there is a Tab Page with that name then revise the cell 
    if (tbpage != null) 
    { 
     //Define a DataGridView object 
     DataGridView dgv = new DataGridView(); 

     //Find the DataGridView inside the TabPage (there is only 1 per Tab Page) 
     foreach (Control ctrl in tbpage.Controls) 
     { 
      dgv = (ctrl is DataGridView) ? (DataGridView)ctrl : dgv; 
     } 

     dgv.ReadOnly = false; 

     DataGridViewCell cellDescription = dgv.Rows[rowIndex].Cells[6]; 
     DataGridViewCell cellCatName = dgv.Rows[rowIndex].Cells[7]; 
     DataGridViewCell cellCat = dgv.Rows[rowIndex].Cells[8]; 

     if (txtChanged > 0) 
     { 
      MessageBox.Show(cellDescription.Value.ToString()); //<-- This returns the correct string on the DataGridView 
      dgv.CurrentCell = cellDescription; 
      cellDescription.ReadOnly = false; 
      cellDescription.Value = newText; //<-- This should update the Cell, but it isnt! 
     } 
     else 
     { 

     } 
    } 
} 
+0

3害怕SO用戶正試圖關閉此線程,但他們上市的原因絕對沒有意義。是的,我在我的標題中說過我的代碼無法正常工作 - 但是 - 我清楚地顯示了我的代碼,解釋了我所嘗試的內容,不工作的內容以及我期望的輸出內容。如果你有(1)甚至閱讀我的帖子,你會知道,(2)甚至讀完你選擇的選擇,你會發現你的選擇沒有價值。這些人應該被追究責任而不閱讀他們 – Sanya

回答

1

我不確定您當前的代碼不符合您的描述。編譯器,應抱怨的第一個問題是線以下:

int txtChanged = Int32.Parse(txtChanged.Text.ToString()); //0 = No Change, 1 = Change 

顯然可變txtChanged不能是stringint。我假設最初的txtChangedTextBox的名稱Form2,其中用戶鍵入0表示不更改,1表示更改。我只是改名爲inttxtChangedchangeValue和if語句改變了這個變量if (changeValue > 0).

最後,我不知道,如果你能爲你引用父窗體標籤控件。我把公共屬性作爲你已經發布的表單標籤控件,並且它沒有正確地返回標籤控件。再次出現編譯時錯誤,指示Form1不包含xTabControl的定義。

爲了簡化而不是使標籤控制公共變量和訪問來自Form2這個公共變量的事情,簡單地傳遞TabControlForm2這將允許雙向接入,無論是從父母標籤控件讀取或寫入它。

這些更改您的代碼後,似乎按預期工作。我認爲在TextBox命名txtDescription文本被在rowIndex行6列

設置到父母DataGridViewForm2,我們要建立一個全球性TabControl變量將指向Form1「傳遞TabControl秒。然後在Form2的構造函數中添加一個TabControl參數到其簽名,以允許父母通過其TabControlForm2。像如下:

public partial class Form2 : Form { 

    public TabControl tabControl; 

    public Form2(TabControl parentTC) { 
    InitializeComponent(); 
    tabControl = parentTC; 
    } 

    private void btnSubmit_Click(object sender, EventArgs e) { 
    string newText = txtDescription.Text.ToString(); //New Text 
    string tabName = txtTabName.Text.ToString(); //Name of the Tab to reference 

    //int txtChanged = Int32.Parse(txtChanged.Text.ToString()); <-- changed this variable name 

    int changeValue = Int32.Parse(txtChanged.Text.ToString()); //0 = No Change, 1 = Change 
    int jobID = Int32.Parse(hidJobID.Text.ToString()); //Job ID 
    int rowIndex = Int32.Parse(hidRowIndex.Text.ToString()); //The row we must reference in our DataGridView 

    //Call the public method from Form1 to define the Tab Control 
    //TabControl tabControl = Form1.xTabControl; <-- This parent tab control is already defined above 

    //Define the Tab Page 
    TabPage tbpage = new TabPage(); 
    tbpage = tabControl.TabPages[tabName]; 

    //If there is a Tab Page with that name then revise the cell 
    if (tbpage != null) { 
     //Define a DataGridView object 
     DataGridView dgv = new DataGridView(); 

     //Find the DataGridView inside the TabPage (there is only 1 per Tab Page) 
     foreach (Control ctrl in tbpage.Controls) { 
     dgv = (ctrl is DataGridView) ? (DataGridView)ctrl : dgv; 
     } 

     dgv.ReadOnly = false; 

     DataGridViewCell cellDescription = dgv.Rows[rowIndex].Cells[6]; 
     DataGridViewCell cellCatName = dgv.Rows[rowIndex].Cells[7]; 
     DataGridViewCell cellCat = dgv.Rows[rowIndex].Cells[8]; 

     if (changeValue > 0) { 
     MessageBox.Show(cellDescription.Value.ToString()); //<-- This returns the correct string on the DataGridView 
     dgv.CurrentCell = cellDescription; 
     cellDescription.ReadOnly = false; 
     cellDescription.Value = newText; //<-- This should update the Cell, but it isnt! 
     } 
     else { 

     } 
    } 
    } 

} 

希望這會有所幫助。

+0

這有助於。我會盡快進行測試。但是,對你的第一個評論,我將一個字符串轉換爲一個int。該控件是一個標籤。編譯器不會抱怨它 - 現在它可能效率低下 - 但這是我可以讓編譯器在字符串值爲null時不抱怨的唯一方法。 – Sanya

+0

我不確定我是否理解您的評論。該行:'int txtChanged = Int32.Parse(txtChanged.Text.ToString());'應該給出錯誤'使用未分配的局部變量txtChange',這是有道理的。在這一行中,通過嘗試解析這個新的未分配變量'txtChanged.Text.ToString()',創建一個名爲'txtChanged'的新'int'變量。 ints甚至沒有Text屬性。 – JohnG

+0

我沒有得到這個錯誤,因爲控制是一個標籤 - 因此它存儲一個字符串值。我不能將int存儲到標籤中 - 它將其作爲字符串讀取。我首先收集字符串值並將其轉換爲int。 – Sanya