0

I am trying my first project using linq query. I stuff on adding new row into datagridview which is already existed column names.

Actually, it can be done by

grid.DataSource = qry.ToList(); 

but due to some reasons of unicode character for column HeaderText. Thus I will get only each row into the DataGridView with existing column names.

I need to display the HeaderText with local language. However, I can change the HeaderText in several way as need but base on experience I think designing HeaderText in UI site is the best way for me.

Below is some code I had practiced.

var qry = (from c in db.TBLCOMPANY 
           where (c.STATUSFLAG == 1) 
           select c).OrderByDescending(c => c.LASTUPDATE);
// grid.DataSource = qry.ToList();

int i = 0;

foreach (MCOMPANY item in qry.ToList())
{
    i = +1;

    DataGridViewRow dr = new DataGridViewRow();

    dr.Cells[0].Value = item.OID.ToString();// Error start from this line
    dr.Cells[1].Value = i.ToString();
    dr.Cells[2].Value = item.COMPANY.ToString();
    dr.Cells[3].Value = item.PROVINCE.ToString();
    dr.Cells[4].Value = item.DISTRICT.ToString();
    dr.Cells[5].Value = item.VILLAGE.ToString();
    dr.Cells[6].Value = item.ESTABLISH.ToString("dd/MM/yyyy");
    dr.Cells[7].Value = item.TEL.ToString();
    dr.Cells[8].Value = item.MOBILE.ToString();
    dr.Cells[9].Value = item.EMAIL.ToString();
    dr.Cells[10].Value = item.ACC1.ToString();
    dr.Cells[11].Value = item.ACC2.ToString();
    dr.Cells[12].Value = item.STATUSFLAG.ToString();
    dr.Cells[13].Value = item.UPDATEBY.ToString();
    dr.Cells[14].Value = item.LASTUPDATE.ToString();

    grid.Rows.Add(dr);
}

Here is the error message

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Detail of error message is here

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index

at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.DataGridViewCellCollection.get_Item(Int32 index)

Actually, I double checked the existign column names in UI grid is 15 and in coding I was declare from 0 to 14. I have no idea.

Please, can anyone suggest how to do this? It is my first Linq project.

Thank you in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mai
  • 13
  • 4
  • Until you add the DataGridRow to the DataGrid, it has no relation to it and thus does not know that you have 15 columns. See also https://stackoverflow.com/questions/47497211/add-new-row-to-datagridview-programmatically – Klaus Gütter Mar 21 '21 at 07:02
  • Also: Instead of `i = +1;` you probably mean `i += 1;` – Klaus Gütter Mar 21 '21 at 07:02
  • Thank you sir, I has already been fix bug you recommended but still throw the same error. – Mai Mar 21 '21 at 07:25
  • i += 1; DataGridViewRow dr = (DataGridViewRow)grid.Rows[0].Clone(); – Mai Mar 21 '21 at 07:25
  • Please [edit] your question to include new information as this is easily missed when writing it in the comments. – Klaus Gütter Mar 21 '21 at 07:26
  • Is there some reason you do not use the returned collection `qry` from the query as a `DataSource` to the grid? Like… `grid.DataSource = qry` … Adding the rows manually to the grid is only going to create more work for you. Using a `DataSource` for the grid will eliminate the current code. – JohnG Mar 21 '21 at 07:27
  • I add each value directly into the grid.Rows.Add(1,2,3,4,5,...) -> It's worked for me. – Mai Mar 21 '21 at 07:28
  • I don't use grid.DataSource = qry.ToList(); because some unicode character trouble. I has already add into the question description. – Mai Mar 21 '21 at 07:36
  • You need to further explain when using `qry` as a `DataSource` is not working for you… you state… _”..but due to some reasons of unicode charater for column HeaderText.”_ … ? Whatever reason the header text is not correct… you can change the header text to anything you want. – JohnG Mar 21 '21 at 07:39
  • yes, I added more explaining in the description. – Mai Mar 21 '21 at 07:51
  • If your experience leans toward adding the row manually to the grid… then have you tried to simply “add” a new row like… `grid.Rows.Add(item.OID.ToString(), i.ToString(), item.COMPANY.ToString(), item.PROVINCE.ToString(),…` …? – JohnG Mar 21 '21 at 08:03

1 Answers1

0

Try adding the row by getting the grids "new" row index and add the item properties to that row in the grid... Something like...

foreach (MCOMPANY item in qry.ToList()) {
  int newRowIndex = grid.Rows.Add();
  grid.Rows[newRowIndex].Cells[0].Value = item.OID.ToString();
  grid.Rows[newRowIndex].Cells[1].Value = i.ToString();
  grid.Rows[newRowIndex].Cells[2].Value = item.COMPANY.ToString();
  grid.Rows[newRowIndex].Cells[3].Value = item.PROVINCE.ToString();
  grid.Rows[newRowIndex].Cells[4].Value = item.DISTRICT.ToString();
  // ....
}

As noted already in the comments… The reason your current code fails starts on the line…

DataGridViewRow dr = new DataGridViewRow();

This creates a “generic” new DataGridViewRow, however, it has no columns/cells. Therefore, when the code tries to set the value in column/cell [0] of that row…

dr.Cells[0].Value = item.OID.ToString();

The index out of bounds exception is thrown for obvious reasons.

JohnG
  • 9,259
  • 2
  • 20
  • 29