My code is below, I cannot for the life of me get the value from a Data table to display in a text box on my main form. I recently came to a realization that DataGridView is not the same as DataTable. Which I think got me much closer. I was originally having issues getting to the specific index as datatable doesn't work that way. Now that I'm using datagridview I feel like I'm pretty much there but missing something silly.
private void dt_SelectionChanged(object sender, EventArgs e)
{
DataGridView dgv = new DataGridView();
dgv.DataSource = dt;
if (dgv.SelectedCells.Count > 0)
{
active_Description.Text =
Convert.ToString(dgv.CurrentRow.Cells["Description"].Value);
active_Cost.Text = Convert.ToString(dgv.CurrentRow.Cells["Cost"].Value);
active_Diameter.Text = Convert.ToString(dgv.CurrentRow.Cells["Diameter"].Value);
active_Last_Used.Text = Convert.ToString(dgv.CurrentRow.Cells["CareTaker"].Value);
active_Location.Text = Convert.ToString(dgv.CurrentRow.Cells["Location"].Value);
active_OAL.Text = Convert.ToString(dgv.CurrentRow.Cells["OAL"].Value);
active_Vendor.Text = Convert.ToString(dgv.CurrentRow.Cells["Vendor"].Value);
}
}
True; a DataGridView is a UI device that shows data in a tabular fashion, something similar to how Excel appears. A DataTable is a device that stores data in a tabular fashion. If you had data to store you put it in a datatable, and if you wanted to show it you can tell a datagridview tonuse the table as a source of data but they are very distinct things
DataGridView shows many rows of data at once, whereas text boxes can only show one value. Generally when we mix the two we end up using the DGV as a navigation device (it shows eg 20 rows and we can click one of them to make it "the current row") and any text boxes also using the same data source typically show the current row data
The code you're trying to put together manually there can be much more simply done using data binding where both the DataGridView and the TextBoxes are bound to the same BindingSource; the BS takes care of maintaining notion of "the current row"
To make your life easy when doing this, follow these steps:
That's really all you need to do; you could put some code into your constructor that fills your dataset with data or you can do it at runtime. do it at runtime for now; run the app, enter 3 or so rows worth of data and then click up and down between them; you'll note that the textboxes change depending on what is the active row in the DGV
If you want to see the code that visual studio wrote to wire all this up it's in the Designer.cs file
If you're getting this data out of a database take a look at my answer to Michal here - actually it has relevance to what is posted above; there Michal refs data out of a db, so a lot of the answer is geared towards pulling that data out and into a dataset, showing it in DGV and textboxes and then sending it back, but the screenshots and other relevant parts about showing and manipulating data (that is in a data set) will help you out too