Sanya
Sanya

Reputation: 1280

C# Change DataGridViewCell value not working

I have two forms (Form1, Form2). Form1 displays the values of objects. Form2 allows the user to revise certain properties of a value from Form1.

Side Note: (In Form1, I programmatically create Tab Pages and in each Tab Page is one DataGridView)

I am trying to transfer the new string value from Form2 to a DataGridView (that I can only get by name) from Form1.

I created a public Tab Control in Form1 that I can access and search for the DataGridView.

My code does not produce any errors, but the cell in the DataGridView does not change like its supposed to.

What Am I doing wrong?

//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 
        {

        }
    }
}  

Upvotes: 0

Views: 527

Answers (1)

JohnG
JohnG

Reputation: 9479

I am not sure exactly why your current code does not work as you describe. The first problem that the compiler should be complaining about is the line below:

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

Obviously the variable txtChanged cannot be a string and an int. I assume that originally txtChanged is the name of a TextBox on Form2 where the user types a 0 for no change and 1 for a change. I simply renamed the int txtChanged to changeValue and changed this variable in the if statement to if (changeValue > 0).

Lastly, I am not sure if you are able to reference the parent forms tab control as you are. I put the public property to get the forms tab control as you have posted and it was not returning the tab control correctly. Again I get compile time error indicating that Form1 does not contain a definition for xTabControl.

To simplify things, instead of making the Tab control a public variable and accessing this public variable from Form2, simply pass the TabControl to Form2 This will allow for two way access, to either read from the parents tab control or write to it.

After these changes your code and appears to work as expected. I assume the text in the TextBox named txtDescription gets set into the parents DataGridView at row rowIndex column 6.

In Form2 we want to make a global TabControl variable that will point to Form1’s passed TabControl. Then in Form2’s constructor add a TabControl parameter to its signature to allow the parent to pass its TabControl to Form2. Like below:

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 {

      }
    }
  }

}

Hope this helps.

Upvotes: 1

Related Questions