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