How to create and attach rows to this DataGridView?

I'm beginner in .Net ,so maybe my question will seem naive to some of you.

I have DataGridView table in WinForm project:

It contain three columns(image,combobox and textBox columns).

Any idea how to create and attach rows to this table?

Thank you in advance!


You create a data source, then bind the data source to the grid's DataSource property. You then add a record to your data source.

// create data source
BindingList<Shape> dataSource = new BindingList<Shape>();

// add record to data source
dataSource.Add(new Shape("Some Contour", "Circle", "Some Name"));

// bind data source
yourDataGridView.DataSource = typeof(BindingList<Shape>);
yourDataGridView.DataSource = dataSource;

Set the DataPropertyName of each column to matches the names of the fields in your Shape class.

DataGridViewTextBoxColumn colName = new DataGridViewTextBoxColumn();
colName.DataPropertyName = "Name";

yourDataGridView.Columns.Add(colName );

However, I recommend you use Virtual Mode instead to keep your data separate and decoupled.

If you wish to accept inputs from user, you have to create a form on this page using which the user can provide inputs. Take those values and add them to a DataTable. Following is a sample snippet showing it:

DataTable dt = new DataTable();
dt.Columns.Add("Contour",typeof(string));  //I am assuming that you will store path 
                                           //of image in the DataTable

Keep adding new rows to the DataTable as you receive inputs from the user:

DataRow row = dt.NewRow();
row["Contour"] = txtContourPath.Text;
row["Shape"] = ddlShape.SelectedValue;
row["Name"] = txtName.Text;

Assign above DataTable to DataSource property of the GridView.

dgv.DataSource = dt;

You can use method:

  1. dataGridView1.Rows.Insert(...)
  2. dataGridView1.Rows.Add(...)
  3. Jay's answer : use dataGridView1.DataSource = dataSource;

Hope I can help you.

Need Your Help

Rails InvalidURIError on redirect_to with a valid url

ruby-on-rails ruby url redirect

I am getting the ERROR URI::InvalidURIError: bad URI(is not URI?) on doing redirect_to. below the full error mgs

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.