Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to fill DataGridView from DataGridViewRow
    text
    copied!<p>I'm trying to fill unbounded datagridview with two custom columns and some rows that filled from a datatable. first datagridview's column must fill from first datatable column and so on. first column is name of brand and second is picture of brand.</p> <p>i use this code and some linq to prepare two arrays </p> <pre><code>DataTable dt = new DataTable(); dt = clsTransmit.FillDataTable("SELECT brandName, brandPic FROM brands", false); int count = dt.Rows.Count; string[] names = dt.AsEnumerable().Select(row =&gt; row.Field&lt;string&gt;("brandName")).ToArray(); string[] pics = dt.AsEnumerable().Select(row =&gt; row.Field&lt;string&gt;("brandPic")).ToArray(); </code></pre> <p>now i want to fill my datagridview that i already try these codes :</p> <pre><code>DataGridViewColumn brandName = new DataGridViewColumn(); DataGridViewImageColumn imageCol = new DataGridViewImageColumn(); dgv.Columns.Add(brandName); dgv.Columns.Add(imageCol); dgv.Columns[0].HeaderText = "Brand Name"; dgv.Columns[1].HeaderText = "Brand Picture"; DataGridViewRow n = new DataGridViewRow(); DataGridViewRow p = new DataGridViewRow(); for (int i = 0; i &lt; count-1; i++) { n.Cells[i].Value = names[i]; p.Cells[i].Value = imgList.Images[i]; } dgv.Rows.AddRange(n); dgv.Rows.AddRange(p); </code></pre> <p>and i also try this:</p> <pre><code>dgv.Rows.Add(names); dgv.Rows.Add(pics); </code></pre> <p>after run an error message is displayed:</p> <blockquote> <p>index was out of range.</p> </blockquote> <hr> <p>ok update my code and got answer i use an image list and fill it by this :</p> <pre><code>foreach (var item in pics) { Image img = Image.FromFile(item); imgList.Images.Add(img); } </code></pre> <p>and use these code to fill gridview and got correct answer:</p> <pre><code>dgv.Columns.Add("brandName", "Brand Name"); for (int i = 0; i &lt; count; i++) { dgv.Rows.Add(new object[] {names[i]}); } DataGridViewImageColumn imageCol = new DataGridViewImageColumn(); dgv.Columns.Add(imageCol); dgv.Columns[1].HeaderText = "Brand Logo"; dgv.Rows.Add(); for (int i = 0; i &lt; count; i++) { dgv.Rows[i].Cells[1].Value = imgList.Images[i]; } </code></pre> <p>but i have another problem now the problem is the datagridview has extra empty row at bottom of all rows . i already uncheck enable adding, editing, deleting datagridview but still it has an extra row :(</p>
 

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