Share via

Datagridview value error

rahul kumar 605 Reputation points
2023-06-28T13:20:02.2666667+00:00

For my project i have two forms

FORM2 , STOCKITEMCDA
FORM2 has a custom datagridview with the following method

      public partial class CustomControl1 : DataGridView   

  {
      protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            const int WM_KEYDOWN = 0x0100;
            const int WM_SYSKEYDOWN = 0x0104;
            int rowIndex = this.CurrentCell.RowIndex;
            int columnIndex = this.CurrentCell.ColumnIndex;

            // Check for the desired shortcut and perform the appropriate action
            if (columnIndex == 0 && (msg.Msg == WM_KEYDOWN || msg.Msg == WM_SYSKEYDOWN) && keyData == (Keys.Alt | Keys.C))
            {
                StockitemCDA messForm = new StockitemCDA();

                Point PositionX = this.PointToScreen(Point.Empty);
                int LeftScreenX = PositionX.X;

                Point PositionY = this.PointToScreen(Point.Empty);
                int TopScreenY = PositionY.Y;

                messForm.StartPosition = FormStartPosition.Manual;
                messForm.Location = new Point(LeftScreenX, TopScreenY);
                messForm.ShowInTaskbar = false;
                messForm.ShowDialog();
                if (messForm.ShowDialog() == DialogResult.Yes)
                {
                    this.Rows[rowIndex].Cells[columnIndex].Value = messForm.textbox1data;
                }      
                
                return true; // return true means the shortcut key has been processed
            }

            // If it is not the desired shortcut key, call the method of the base class for default processing
            return base.ProcessCmdKey(ref msg, keyData);
        }

by using the above code , i am getting the value of textbox1data from the StockitemCDA but Now the problem is when the control returns to the Form2 , I am not getting any value back to the datagridview. Please suggest that what is causing this behaviour .

Developer technologies | Windows Forms
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,606 Reputation points Volunteer Moderator
    2023-06-28T16:01:04.3+00:00

    The following is conceptual which can be plugged into your code with of course modifications.

    Here in the child form, in this case we want a value in a TextBox and only close the form if the TextBox has a value.

    We setup a delegate/event in the child form.

    public delegate void OnPassInformation(string sender);
    public event OnPassInformation PassInformation;
    

    Button code

    private void SubmitButton_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrWhiteSpace(FirstNameTextBox.Text))
        {
            PassInformation?.Invoke(FirstNameTextBox.Text);
            DialogResult = DialogResult.OK;
        }
        else
        {
            MessageBox.Show("Need a first name");
        }
    }
    

    Then

    The following is in a button click event, would be done in your current code.

    private void DisplayButton_Click(object sender, EventArgs e)
    {
        using (ChildForm form = new ChildForm())
        {
            form.PassInformation += delegate(string s)
            {
                Debug.WriteLine(s);
            };
            form.ShowDialog(this);
        }
    }
    

    The benefits are, no need to access a form control and should be easy to following in the debugger with breakpoints.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.