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