none
moving to a specific record when a form is displayed RRS feed

  • Question

  • I have a single record form that is activated by double clicking a Datagridview. I pass it the Log_key and the Printer_ID. I need to display the record for the selected printer. If they select a new record it will just pass the Log_Key and a blank Printer_Id, I need in which case I would to show them a blank form to fill out. I am having trouble understanding how do get the form to go to the record specified since the key is a multi-part Key Log_Key and Printer_Id.  I am using dataset databinding. 



    Tuesday, April 9, 2019 3:39 PM

Answers

  • Hi Seeker of Wisdom,

    >> edit\update the record saving it before I return to the main form then just refreshing the DGV to show the new values.

    To refresh the data of DGV via child form, you can use "Event" to achieve it. Here is a simple demo of "Add" you can refer to:

    Form1 (with DGV):

        private void dgvShift_Printers_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvShift_Printers.CurrentCell.Value == DBNull.Value)
            {
                frmShiftPrinters frmShiftPrinters = new frmShiftPrinters();
                frmShiftPrinters.TransfEvent += RefreshData;
                frmShiftPrinters.ShowDialog();
            }
            else
            {
                DataGridViewRow row = this.dgvShift_Printers.Rows[e.RowIndex];
                string myPrinter = row.Cells[1].Value.ToString();
                int myShiftLogId = (int)row.Cells[0].Value;
                Form f = new frmShiftPrinters(myShiftLogId, myPrinter);
                f.ShowDialog();
            }
        }
    
        string constr = "connection string";
    
        private void Form1_Load(object sender, EventArgs e)
        {
            RefreshData();
        }
    
        public void RefreshData()
        {
            using (SqlConnection sqlConnection = new SqlConnection(constr))
            {
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(@"select * from Printer", sqlConnection);
                DataSet dataSet = new DataSet();
                sqlDataAdapter.Fill(dataSet);
                dgvShift_Printers.DataSource = dataSet.Tables[0];
            }
        }

    frmShiftPrinters (child form):

        public delegate void TransfDelegate();
        public event TransfDelegate TransfEvent;
    
        public frmShiftPrinters(int id, string printer)
        {
            InitializeComponent();
            tblogId.Text = id.ToString();
            tbPrinter.Text = printer;
        }
    
        string constr = "connection string";
    
        private void btEdit_Click(object sender, EventArgs e)
        { 
        }
    
        private void btAdd_Click(object sender, EventArgs e)
        {
            using (SqlConnection sqlConnection = new SqlConnection(constr))
            {
                SqlCommand sqlCommand = new SqlCommand($"insert into Printer values('{tblogId.Text}', '{tbPrinter.Text}')", sqlConnection);
                sqlConnection.Open();
                sqlCommand.ExecuteNonQuery();
            }
            TransfEvent();
        }

    Result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 12, 2019 1:57 AM
    Moderator

All replies

  • You need to provide more information, such as how you are accessing the data. Are you using Entity Framework?


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, April 9, 2019 4:03 PM
  • I updated the question
    Tuesday, April 9, 2019 4:28 PM
  • See DbSet.Find(Object[]) Method. It supports multiple parameters. If that does not apply to you then you need to specify what you are using to access the data.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, April 9, 2019 6:02 PM
  • Hi,

    Do you mean you want to pass some values to record form when one record (one row) is selected?

    What is "Log_Key"? I didn't find anything about "Log_Key" in the form you provided.

    >> I am having trouble understanding how do get the form to go to the record specified since the key is a multi-part Key Log_Key and Printer_Id.

    Could you explain this sentence in detail?

    To handle the issue more efficiently, it would be better if you can provide more useful information.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 10, 2019 5:38 AM
    Moderator
  • I think you have it Kyle. I want to have a single record forms to edit\add records in a child table. It seems like this would be easy and I still have to look into Sam's solution. I was kind of hoping to find example so I didn't have to reinvent the wheel. If I was passing a single field key it would be simple but the examples I have seen to not work well for multiple field key's.  

            private void dgvShift_Printers_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
            {
                shift_PrintersBindingSource.EndEdit();
    
                    if (dgvShift_Printers.NewRowIndex >= 0)
                    {
                        DataGridViewRow row = this.dgvShift_Printers.Rows[e.RowIndex];
                        string myPrinter = row.Cells[1].Value.ToString();
                        int myShiftLogId = (int)row.Cells[0].Value;
                        Form f = new frmShiftPrinters(myShiftLogId, myPrinter);
                        f.ShowDialog();
                        
                    }

    Wednesday, April 10, 2019 1:38 PM
  • Note that my suggestion assumes you are using Entity Framework. You have not said if you are or are not using EF. If not EF then we can only guess at what you are using for the database.


    Sam Hobbs
    SimpleSamples.Info

    Wednesday, April 10, 2019 2:46 PM
  • Hi Seeker of Wisdom,

    >> but the examples I have seen to not work well for multiple field key's

    You can handle it via creating a parameter constructor, just like this:

        public frmShiftPrinters(int id, string printer)
        {
            InitializeComponent();
            tblogId.Text = id.ToString();
            tbPrinter.Text = printer;
        }

    And here is the code of "dgvShift_Printers_CellDoubleClick":

        if (dgvShift_Printers.CurrentCell.Value == DBNull.Value)
        {
            frmShiftPrinters frmShiftPrinters = new frmShiftPrinters();
            frmShiftPrinters.ShowDialog();
        }
        else
        {
            DataGridViewRow row = this.dgvShift_Printers.Rows[e.RowIndex];
            string myPrinter = row.Cells[1].Value.ToString();
            int myShiftLogId = (int)row.Cells[0].Value;
            Form f = new frmShiftPrinters(myShiftLogId, myPrinter);
            f.ShowDialog();
        }

    Result:

    >> edit\add records in a child table

    As to edit or add the record in a table, here is a document about "ADO.NET" you can refer to.

    SqlCommand Class.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 11, 2019 1:57 AM
    Moderator
  • Kyle, your solution is to pass the data from the Datagridview (DGV) to the form. Actually I found this on YouTube it is a version of what you are suggesting.  https://www.youtube.com/watch?v=b37H9G8WkrQ. In the attached example he passes the record values to the child form in a object. Updates the values on the child form. Then he returns to the main form, I assume he updates the values in the DGV from the object. Then has to do some kind of save operation to update the database. I was hoping to pass a key, then using a dataset bond to the controls edit\update the record saving it before I return to the main form then just refreshing the DGV to show the new values. I originally wanted to do the edits right on the main form but the editing events on the DGV are just plain screwy.

    Thursday, April 11, 2019 2:27 PM
  • MY RANT, A friend said he uses a third party DGV control he purchased because the winforms one is incomplete, I believe him. I have yet to convince my company they need to buy additional software to supplement the hefty annual subscription fee they are paying for all of the people in our programming department to use VS. It just doesn't make since to develop a network application when this is a single department FE application that resides on one or two computers with a Database backend. Win forms has been around since the 90's one would think it would be complete by now. Too bad Microsoft has chosen to diversify with WPF, Universal windows, and so many other development platforms and not maintain the base business platform Windows forms. With MS, It seems they take the stand whatever development platform is the flavor of the month that's what you need to use and we will just leave the others incomplete because no one should be using them. 



    Thursday, April 11, 2019 2:28 PM
  • Hi Seeker of Wisdom,

    >> edit\update the record saving it before I return to the main form then just refreshing the DGV to show the new values.

    To refresh the data of DGV via child form, you can use "Event" to achieve it. Here is a simple demo of "Add" you can refer to:

    Form1 (with DGV):

        private void dgvShift_Printers_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvShift_Printers.CurrentCell.Value == DBNull.Value)
            {
                frmShiftPrinters frmShiftPrinters = new frmShiftPrinters();
                frmShiftPrinters.TransfEvent += RefreshData;
                frmShiftPrinters.ShowDialog();
            }
            else
            {
                DataGridViewRow row = this.dgvShift_Printers.Rows[e.RowIndex];
                string myPrinter = row.Cells[1].Value.ToString();
                int myShiftLogId = (int)row.Cells[0].Value;
                Form f = new frmShiftPrinters(myShiftLogId, myPrinter);
                f.ShowDialog();
            }
        }
    
        string constr = "connection string";
    
        private void Form1_Load(object sender, EventArgs e)
        {
            RefreshData();
        }
    
        public void RefreshData()
        {
            using (SqlConnection sqlConnection = new SqlConnection(constr))
            {
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(@"select * from Printer", sqlConnection);
                DataSet dataSet = new DataSet();
                sqlDataAdapter.Fill(dataSet);
                dgvShift_Printers.DataSource = dataSet.Tables[0];
            }
        }

    frmShiftPrinters (child form):

        public delegate void TransfDelegate();
        public event TransfDelegate TransfEvent;
    
        public frmShiftPrinters(int id, string printer)
        {
            InitializeComponent();
            tblogId.Text = id.ToString();
            tbPrinter.Text = printer;
        }
    
        string constr = "connection string";
    
        private void btEdit_Click(object sender, EventArgs e)
        { 
        }
    
        private void btAdd_Click(object sender, EventArgs e)
        {
            using (SqlConnection sqlConnection = new SqlConnection(constr))
            {
                SqlCommand sqlCommand = new SqlCommand($"insert into Printer values('{tblogId.Text}', '{tbPrinter.Text}')", sqlConnection);
                sqlConnection.Open();
                sqlCommand.ExecuteNonQuery();
            }
            TransfEvent();
        }

    Result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 12, 2019 1:57 AM
    Moderator
  • Hi,

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer.So that it will help other members to find the solution quickly if they face the similar issue.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 18, 2019 6:40 AM
    Moderator