日期:2014-05-18  浏览次数:20754 次

加载DataGridView时可以同时加载样式么?
C/S结构: 通过数据绑定功能绑定数据,然后通过循环该dataGridView查看符合条件的行进行颜色变化,为什么在窗体第一次打开的时候不能按照要求把不[code=C#][/code]同颜色的行显示出来,而是统一无样式的。
 private void InitGrid(List<queryEntity> QueryEntity)
  {
  // dt = DataProvider.Instance().send_bill_Detail_list(QueryEntity);
  dt = DataProvider.Instance().sendBill_DetailList(QueryEntity);

  dt.DefaultView.Sort = " sprePlanNo desc";
  dt = dt.DefaultView.ToTable();

  gvList.Columns["id"].DataPropertyName = "spreid";
  gvList.Columns["swipecardCount"].DataPropertyName = "swipecardCount";

  gvList.Columns["lastszQty"].DataPropertyName = "lastszQty";
   

  gvList.Columns["topno"].DataPropertyName = "spreMainNo";
  gvList.Columns["no"].DataPropertyName = "sprePlanNo";
  gvList.Columns["createdate"].DataPropertyName = "CreateDate";
  gvList.Columns["ownername"].DataPropertyName = "fullname";

  gvList.Columns["productname"].DataPropertyName = "proName";
  gvList.Columns["sprenum"].DataPropertyName = "spreAmount";

  //gvList.Columns["sendBoardnum"].DataPropertyName = "BoardName";
  gvList.Columns["sendBoardnum"].DataPropertyName = "sendBoardNo";
  gvList.Columns["sendTankNum"].DataPropertyName = "TankName";
  gvList.Columns["dictunit"].DataPropertyName = "dictUnit";

  //gvList.Columns["dicttype"].DataPropertyName = "typeName";
  gvList.Columns["dicttype"].DataPropertyName = "dictType";

  gvList.Columns["carno"].DataPropertyName = "carNo";
  gvList.Columns["caruser"].DataPropertyName = "driveNo";

  gvList.Columns["startDate"].DataPropertyName = "startDate";
  gvList.Columns["endDate"].DataPropertyName = "endDate";
  //gvList.Columns["chae"].DataPropertyName = "chae";

   
  winformUtil.BindGridView(dt, gvList);

   
  navigator1.DataBanding();

  showcolor(ref gvList);
  }


//显示不同颜色的函数

  private void showcolor(ref DataGridView giv)
  {
  if (giv != null)
  {
  for (int i = 0; i < giv.Rows.Count; i++)
  {
  if (Convert.ToSingle(giv.Rows[i].Cells[1].Value) > 0)
  {
  giv.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
  }
  }
  }
  }

结果只有点查询进行重载dataGridView时可以,而在最开始打开该窗体的时候不能显示要改变行的颜色,请问下这是什么问题啊?

------解决方案--------------------
绑定数据 DataSourceChanged 判断该颜色程序放这里
------解决方案--------------------
for (int i = 0; i < giv.Rows.Count; i++)
{
if (Convert.ToSingle(giv.Rows[i].Cells[1].Value) > 0)
{
giv.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
}
} 直接写在DataSourceChanged 应该可以的