Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just to extend this question, there's also another way to add a row to a <code>DataGridView</code>, especially if the columns are always the same:</p> <pre><code>object[] buffer = new object[5]; List&lt;DataGridViewRow&gt; rows = new List&lt;DataGridViewRow&gt;(); foreach (SaleItem item in this.Invoice.SaleItems) { buffer[0] = item.Quantity; buffer[1] = item.Part.Description; buffer[2] = item.Price; buffer[3] = item.Quantity * item.Price; buffer[4] = item.Part.Number; rows.Add(new DataGridViewRow()); rows[rows.Count - 1].CreateCells(gridViewParts, buffer); } gridViewParts.Rows.AddRange(rows.ToArray()); </code></pre> <p>Or if you like ParamArrays:</p> <pre><code>List&lt;DataGridViewRow&gt; rows = new List&lt;DataGridViewRow&gt;(); foreach (SaleItem item in this.Invoice.SaleItems) { rows.Add(new DataGridViewRow()); rows[rows.Count - 1].CreateCells(gridViewParts, item.Quantity, item.Part.Description, item.Price, item.Quantity * item.Price, item.Part.Number ); } gridViewParts.Rows.AddRange(rows.ToArray()); </code></pre> <p>The values in the buffer need to be in the same order as the columns (including hidden ones) obviously.</p> <p>This is the fastest way I found to get data into a <code>DataGridView</code> without binding the grid against a <code>DataSource</code>. Binding the grid will actually speed it up by a significant amount of time, and if you have more then 500 rows in a grid, I strongly recommend to bind it instead of filling it by hand.</p> <p>Binding does also come with the bonus that you can keep the Object in tact, f.e. if you want to operate on the selected row, you can do this is the DatagridView is bound:</p> <pre><code>if(gridViewParts.CurrentRow != null) { SaleItem item = (SalteItem)(gridViewParts.CurrentRow.DataBoundItem); // You can use item here without problems. } </code></pre> <p>It is advised that your classes which get bound do implement the <code>System.ComponentModel.INotifyPropertyChanged</code> interface, which allows it to tell the grid about changes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload