We help IT Professionals succeed at work.

Better way to add rows to DataGridView?

deleyd
deleyd asked
on
Medium Priority
507 Views
Last Modified: 2013-12-16
I'm new to C#. What would be a better way to add a new row to the end of my DataGridView in method dgvaddrow below?
using System;
using System.Windows.Forms;
using System.Threading;

namespace dgvtest1
{
  public partial class Form1 : Form
  {
     delegate void AddRow(Cdata d);
     AddRow dmeth = null;

     public Form1()
     {
        InitializeComponent();
     }

     protected override void OnShown(EventArgs e)
     {
        ThreadStart Y = new ThreadStart(DoStuff);
        Thread tid1 = new Thread(Y);
        tid1.Start();
     }

     void DoStuff()
     {
        dmeth = new AddRow(dgvaddrow);
        Cdata d = new Cdata();
        d.C1 = "Hello";
        d.C2 = "Goodbye";
        d.C3 = "Three";
        this.Invoke(dmeth,d);
     }

     public void dgvaddrow(Cdata d)
     {
        int index = dataGridView1.Rows.Add();
        dataGridView1["Column1", index].Value = d.C1;
        dataGridView1["Column2", index].Value = d.C2;
     }

     public class Cdata
     {
        private string c1;
        private string c2;
        private string c3;

        public string C1
        {
          get { return c1; }
          set { c1 = value; }
        }
        public string C2
        {
          get { return c2; }
          set { c2 = value; }
        }
        public string C3
        {
          get { return c3; }
          set { c3 = value; }
        }
     }
  }
}

Open in new window

Comment
Watch Question

I don't know why you are doing this in a separate thread? You could change your code to this:
 
using System;
using System.Windows.Forms;
using System.Threading;

namespace dgvtest1
{
  public partial class Form1 : Form
  {
     public Form1()
     {
        InitializeComponent();
     }

     protected override void OnShown(EventArgs e)
     {
        Cdata d = new Cdata();
        d.C1 = "Hello";
        d.C2 = "Goodbye";
        d.C3 = "Three";

        dgvaddrow(d);
     }

     public void dgvaddrow(Cdata d)
     {
        int index = dataGridView1.Rows.Add();
        dataGridView1["Column1", index].Value = d.C1;
        dataGridView1["Column2", index].Value = d.C2;
     }

     public class Cdata
     {
        private string c1;
        private string c2;
        private string c3;

        public string C1
        {
          get { return c1; }
          set { c1 = value; }
        }
        public string C2
        {
          get { return c2; }
          set { c2 = value; }
        }
        public string C3
        {
          get { return c3; }
          set { c3 = value; }
        }
     }
  }
}

Open in new window

You may also want to lost the event handler and just put it straight in the constructor (after InitializeComponent). There are various other ways to attach your object to the grid (like data binding) but your code should work well for simply adding another row. The help on this shows a couple of other ways but I think you're on the right track.
deleydSoftware Engineer

Author

Commented:
It ended up being in a separate thread so I could add a whole bunch of rows that would take awhile to do without hanging the rest of the program interface.

So I guess dataGridView1.Rows.Add is necessary to add a row, though I seem to end up with two rows when I run this demo—the row I added plus a blank row at the bottom.

So
1. Add blank row
2. Fill in columns of new row

Is there a way I could do it the other way, something like
DataGridViewRow ds = new DataGridViewRow();

Open in new window

followed by somehow filling in the columns of ds, then adding ds to the dataGridView?
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.