Search code examples
c#.netdatagridviewdatagridviewcolumn

How to access DataGridView data


Bit of a generic title, but I wasn't exactly sure how to summarize the situation:

I have a DGV with several rows and 6 columns. On the form with this DGV, I have a few buttons to Add, Edit, Delete, and Save the state of the DGV (Figure 1). When a user clicks the Edit button, a new window is opened with TextBoxes which should be filled with the data from the selected row (Figure 2).

My question is, what is the best method for passing the row's data (either through DataGridViewRow or DataGridViewCellCollection), and once it's been passed to the Edit class, how can I access the row's data. Once I can receive this, the rest should be cake for me, but I am unsure of how to access the cells' datas.

Code posted below images.

Figure 1: Main form with DGV & buttons

Figure 2: (Not sure why it's so huge, sorry!)

Edit window for cell data

CODE SNIPPETS:

Here is the code for the DGV's Edit Rail Car button:

private void buttonEditRailCar_Click(object sender, EventArgs e)
    {
        var singleCarSelected = this.dataGridViewSimulatedTrainEditor.CurrentRow;

        EditRailCar editNewRailCar = new EditRailCar(singleCarSelected);

        //To StackOverFlow: Feel free to ignore anything below this comment!

        var result = editNewRailCar.ShowDialog();

        if (result == DialogResult.OK)
        {                
            this.NewSimulatedTrain.Add(addNewRailCar.NewSimulatedTrain);
            this.WereChangesMade = true;
            this.buttonSaveXML.Enabled = true;
        }
    }

Here is the code for the Edit Rail Car FORM that pops up with the textboxes I will populate with the data from the row - I've added some pseudocode so you can see what I'm trying to achieve:

public partial class EditRailCar : Form
{
    public EditRailCar(DataGridViewRow t)
    {
        InitializeComponent();

        //Here, when the form opens, the local textboxes should be populated with data
            //from the DGV's cells
        this.textBoxName.Text = t.RailCarName;
        this.textBoxRailCarType.Text = t.RailCarType;
        this.textBoxVelocity.Text = t.Velocity;
        this.textBoxVelocityDistance.Text = t.VelocityDistance;
        this.textBoxIsDamaged.Text = t.IsDamage;
        this.textBoxIsForeignObject.Text = t.IsForeignObject;
    }

    private void buttonSaveEdits_Click(object sender, EventArgs e)
    {
        //This will save the changes to the current row in the DGV
        //StackOverFlow may ignore this :)
    }

    private void buttonCancelNewCar_Click(object sender, EventArgs e)
    {
        DialogResult = DialogResult.Cancel;
    }
}

Solution

  • First, use SelectedRows property instead of CurrentRow of datagridview like

    if (this.dataGridViewSimulatedTrainEditor.SelectedRows.Count > 0)
    {
        var singleCarSelected = this.dataGridViewSimulatedTrainEditor.SelectedRows[0];
    }
    

    Again the way you are passing is completely wrong. rather create a RetailCar type , fill it and pass it along like

    public class RetailCar
    {
      public string Name {get; set;}
      public string Type {get; set;}
    
      // rest propetties
    }
    
    DataGridViewRow rw = this.dataGridViewSimulatedTrainEditor.SelectedRows[0];
    RetailCar rc = new RetailCar
    {
      Name = rw.Cells["RetailCar Name"].Value.ToString();
      Type = rw.Cells["RetailCar Type"].Value.ToString();
    
      // fill rest properties
    };
    

    Pass it along

    EditRailCar editNewRailCar = new EditRailCar(rc);
    

    Change your form constructor accordingly

    public partial class EditRailCar : Form
    {
        public EditRailCar(RetailCar retailCar)
        { 
            // do whatever needed
        }
    }