0

I am a beginner in C#. I have a Windows Forms named ClientTransaction and ServiceDetails. I made a DataGridView and it has a view button in a row where when you click it, it will generate a servicedetail form. I want the Values in Client Transaction from the datagridview to be passed in a service detail.

Client Transaction Form:

Client Transaction Form

Once it click the view, will proceed to Service Details Form

Service Details Form mock up only

Service Details Form

I try this codes

private void ClientTransaction_Load(object sender, EventArgs e)
    {

        DataGridViewButtonColumn viewBtn = new DataGridViewButtonColumn();
        viewBtn.HeaderText = "Action";
        viewBtn.Text = "View";
        viewBtn.Name = "btnView";
        viewBtn.UseColumnTextForButtonValue = true;
        reservedGrid.Columns.Add(viewBtn);


    }
private void reservedGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        var senderGrid = (DataGridView)sender;

        if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
            e.RowIndex >= 0)
        {
            //TODO - Button Clicked - Execute Code Here 
            ServiceDetail viiew = new ServiceDetail();
            viiew.Show();
            }
    }

It works on proceeding on another form but I still finding its ways on passing the data in another form from the value of the every row from client transaction in datagridview will be pass in a service detail form as you can see in the screenshots above. Thank you!

Client Transaction Table

id INT AUTO_INCREMENT PRIMARY KEY, client_id INT NOT NULL, status VARCHAR(20) NOT NULL, total_amt DOUBLE (7,2), trans_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

Service Detail Table

id INT AUTO_INCREMENT PRIMARY KEY, client_transaction_id INT NOT NULL, barber_id INT NOT NULL, service_id INT NOT NULL, added_on timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

boldsinas101
  • 300
  • 2
  • 5
  • 22
  • 1
    you can pass selected id into second form construster. And second way is you can declare a public variable into second form, and set value from first form. – Durgesh Pandey Jul 19 '18 at 03:00
  • 1
    Possible duplicate of [Send values from one form to another form](https://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form) – Hermanto Jul 19 '18 at 03:11

2 Answers2

0

Method 1:

Service Details Form

class ServiceDetail
{

 public ServiceDetail()
 {
  InitializeComponent();
 }

 public ServiceDetail(int serviceDetailId)
 {
      //here you have selected id now you can get details from db..
 } 
}

Client Transaction Form

   private void reservedGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
   {
            var senderGrid = (DataGridView)sender;

            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
                e.RowIndex >= 0)
            {
int selectedId=Convert.ToInt(senderGrid.Rows[e.RowIndex].Cells["Service_Detail_ID"].Value));
                //TODO - Button Clicked - Execute Code Here 
                ServiceDetail viiew = new ServiceDetail(selectedId);
                //viiew.ref_branch1 = this;
                viiew.Show();
             }
   }

Method 2:

Service Details Form

class ServiceDetail
{
 public int serviceDetailId=0;

 public ServiceDetail()
 {
  InitializeComponent();
 }

 public ServiceDetail()
 { 
      //here you have selected id now you can get details from db..
 } 
}

Client Transaction Form

   private void reservedGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
   {
            var senderGrid = (DataGridView)sender;

            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
                e.RowIndex >= 0)
            {
int selectedServiceDetailId=Convert.ToInt(senderGrid.Rows[e.RowIndex].Cells["Service_Detail_ID"].Value));
                //TODO - Button Clicked - Execute Code Here 
                ServiceDetail viiew = new ServiceDetail();
                viiew.SelectedId=selectedServiceDetailId;
                viiew.Show();
            }
    }
Durgesh Pandey
  • 2,314
  • 4
  • 29
  • 43
  • In reservedGrid_CellContenClick, why there is a service_detail_id? Because the one I should pass is the values from the client transaction from the datagridview? – boldsinas101 Jul 19 '18 at 09:32
  • I supposed the column name "service_detail_id" of "Service Detail ID" in grid of Client Transaction Form. – Durgesh Pandey Jul 19 '18 at 09:40
  • There is no service_detail_id in Client Transaction Form grid instead there is client_transaction_id in that grid form and the client_transaction_id, client name(which is a inner join in a client table) are want to pass in service_detail form. So instead the service_detail_id, I shall change it to id(primary key of client transaction? I'll add the tables from db in the question – boldsinas101 Jul 19 '18 at 10:59
0

Pass information to Service Detail form via constructor when viewBtn is clicked if there is no constructor in service detail form then construct the constructor for your information in service detail form.

ServiceDetail viiew = new ServiceDetail(.....);//ID,client,total,etc
viiew.Show();

when you add service Detail form then add parameterized constructor there

 class ServiceDetail
 {
    public ServiceDetail()
    {
      InitializeComponent();
    }
    public ServiceDetail(..................)//ID,client,total,etc
    {
      ............................
      ............................
    }
  }
Arslan Ali
  • 450
  • 4
  • 12