Search code examples
c#datatabledatagridviewselectionchanged

Converting value from datagridview to a text box


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.

My Code

    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);
            }

        }

Solution

  • 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:

    • add a new file to your project, of type DataSet
    • open the dataset, right click the surface and add a datatable, call it Product (you look like you're making some kind of product related system)
    • right click the table and add a new column, call it Description
    • add another column for Cost(type decimal?) Diameter(int? Decimal?) etc
    • switch to your form designer
    • open the data sources window (view menu, other windows) - if you're on .net core it doesn't work at the moment, so here's hoping you're on framework. There are other ways to do this process on core, it's just more of a nuisance
    • expand all the nodes in Data Sources
    • you can see parent node, Product, with a little grid looking icon. It has children whose icons look more like textboxes etc
    • drag product node onto the form; a DataGridView appears, along with a dataset, binding source and navigator (in the bottom tray)
    • drag a few of the child nodes to the form too, textboxes appear but you'll note that they reuse the existing products binding source
    • if you inspect the DGV and textboxes you'll find that they have a data source set to the binding source, and the binding source has a data source of the dataset (and data member of products table)

    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