0

I have been searching for days all over the web, including this place for the answer to my question - all to no avail. At first I hesitated to ask here because last time I asked a question I got severely beaten for not asking it properly. So I'll try again. Here's the scenario:

I have built a Windows desktop application for our company to track customer orders. It uses a MySQL database hosted on my own server. It is fairly simple and it's working fine. My application has several different data bound controls and all is well. Really, it works just great.

There is a datagridview which displays all the orders a particular customer has placed. When the user clicks on the Order Number column in one of the rows a query is run which returns data specific to that order. One piece of data returned is the status of that order.

But I want to access that one piece of data WITHOUT using a data bound control - in this case it's a text box bound to one of the fields in a table in my database. I hide that control because it shows some rather cryptic data. That piece of data is a status code for the orders in the table. These codes are not self-apparent - they mean nothing unless you know what they refer to. You can see this in the code snippet below. I would rather not have this hidden text box on the form. I would rather obtain the value programmatically but I cannot for the life of me find the proper way to do this.

My code snippet:

private void getCustData2DataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string ordnum = null;
            string statusCode = null;
            
                if (e.RowIndex >= 0)
                {
                    DataGridViewRow row = getCustData2DataGridView.Rows[e.RowIndex];
                    ordnum = row.Cells[0].Value.ToString();   
                }

                try
                {
                    this.ecom_ordersTableAdapter.Fill(this.rBTCO_DBDataSet1.ecom_orders, ordnum);
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }

            
            statusCode = statusdata.Text; //gets the value from the hidden textbox which is bound to the "status" field in the ecom_orders table.

            switch (statusCode)
            {
                case "NS":
                    label5.Text = "Processing";
                    label5.ForeColor = Color.Green;
                    break;
                case "P":
                    label5.Text = "Pending Payment";
                    label5.ForeColor = Color.Blue;
                    break;
                case "X":
                    label5.Text = "Cancelled";
                    label5.ForeColor = Color.Red;
                    break;
                case "C":
                    label5.Text = "Completed";
                    label5.ForeColor = Color.Blue;
                    break;
                default:
                    label5.Text = null
                    label5.ForeColor = Color.Black;
                    break;

            }
        }

The status codes reside in the rbtcoDataSet1.ecom_orders table. You can see in the switch block where I assign strings to the different status codes so they have meaning to the user. Since all the database manipulation in this application has been generated by Visual Studio I really don't have a clue as how to get this one piece of data without using a bound control. Can it even be done in this case?

Let the beatings commence. Please be gentle!

  • Where do you want the input to come from? – jdweng Jun 23 '21 at 23:18
  • High level, you want to use a `viewmodel` (see: https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc). You read your 'raw' data in from the database, and then 'shape' it how you want to display it in your view. And then you bind to *THAT*. – Jonathan Jun 23 '21 at 23:20
  • I would like to obtain the data programmatically rather than from the text property of the bound text box. For no reason other than my own curiosity. Something along the lines of statusCode = rBTCO_DBDataSet1.ecom_orders.statusColumn . . .. The data is sitting right there but I can't figure out how to access it without using the bound control. Like I said before, this app is working fine. I just want to get that one piece of data a different way - more or less like I'd have to do it if it were a console application rather than just "drag-and-drop" like you do in Visual Studio. – Dave Skowron Jun 24 '21 at 01:41
  • How are you mapping which row to pick off the DataTable, do you have a `BindingSource`? – Charlieface Jun 24 '21 at 08:11
  • Each row is selected by the user in the datagrid. The Order Number (ordnum) column. That event fires a fill which populates the different fields in the application. I wish I could attach a screen shot. When the order number is clicked on, the data is retrieved. As I mentioned, my issue is with the code for "status". It is presented in a bound control but I want to have it placed into a variable instead. Then I'll use it in my switch statement as is shown in my code snippet. – Dave Skowron Jun 24 '21 at 13:11
  • BTW - yes there is a binding source between the dataset and the table in question. – Dave Skowron Jun 24 '21 at 14:08

0 Answers0