当前位置:编程学习 > C#/ASP.NET >>

winfrom中dataGridView绑定的问题!问题很奇怪!~~~高手来!~~~给100分

问题1:我有一个dataGridView触发了一个RowPrePaint事件,为什么我在RowPrePaint中加了一个断点,调试的时候总是会跳到这个断点,好像是一个死循环?(数据库中只有一条数据)这是为什么呢。代码如下

private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
     if (e.RowIndex >= dataGridView1.Rows.Count - 1)
     {
         return;
     }
     DataGridViewRow dgr = dataGridView1.Rows[e.RowIndex];
     try
     {
         DateTime jsri = Convert.ToDateTime(dgr.Cells["Column7"].Value);

     if (DateTime.Now >= jsri.AddDays(-3))
     {
              dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
              if (DateTime.Now > jsri)
              {
                   dgr.Cells["Column1"].Style.ForeColor = Color.Blue;
                   dgr.Cells["Column2"].Style.ForeColor = Color.Blue;
                   dgr.Cells["Column3"].Style.ForeColor = Color.Blue;
                   dgr.Cells["Column4"].Style.ForeColor = Color.Blue;
                   dgr.Cells["Column5"].Style.ForeColor = Color.Blue;
                   dgr.Cells["Column6"].Style.ForeColor = Color.Blue;
                   dgr.Cells["Column7"].Style.ForeColor = Color.Blue;
                   dgr.Cells["Column8"].Style.ForeColor = Color.Blue;
                   dgr.Cells["Column9"].Style.ForeColor = Color.Blue;
                   dgr.Cells["Column10"].Style.ForeColor = Color.Blue;
                   dgr.Cells["Column11"].Style.ForeColor = Color.Blue;
                }
           }
       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.Message);
       }
}


问题2:我的一个界面上有一个插入按钮,插入之后dataGridView重新绑定,但是为什么在插入后绑定不上呢?其实数据已经插入了,无奈!我写了个刷新按钮,重新绑定dataGridView,当插入后点刷新的话新插入的数据就会绑定上。我用的是access数据库。本人认为是access数据库的问题,为什么我有sql写同样的功能就不会出现这种情况呢。代码如下

//绑定方法
private void BangDing()
{
     SqlDal sql = new SqlDal();
     DataTable dt = new DataTable();
     dt = sql.GetAll_T_User_Info_ASC();
     this.dataGridView1.DataSource = dt;
}
//插入方法
private void button1_Click(object sender, EventArgs e)
{
      string userName = this.txtUserName.Text.Trim();
      string userAdd = this.txtAdd.Text.Trim();
      string userZhangHao = this.txtUserZangHao.Text.Trim();
      string userTel = this.txtTel.Text.Trim();
      string kaiTongRiQi = this.datKaiTime.Value.ToShortDateString();
      string jieShuRiQi = this.datJieTime.Value.ToShortDateString();
      string Money = this.txtMomey.Text.Trim();
      string Zhao = this.txtZhao.Text.Trim();
      string BeiZhu = this.txtBeiZhu.Text.Trim();
      //插入成功后绑定
      if (SqlDal.InsertUser_Info(userName, userAdd, userZhangHao, userTel, kaiTongRiQi, jieShuRiQi, 
      Money,Zhao, BeiZhu, SheBeiDuanKou))
      {
           BangDing();
      }
}
//刷新按钮
private void button2_Click(object sender, EventArgs e)
{
      BangDing();
}

--------------------编程问答-------------------- 只有帮顶了~~~ --------------------编程问答-------------------- 那你就试试 不加断点,它还死循环吗?我感觉 应该不是死循环,只是加载的样式很多,导致不停的执行这个事件而已....  出现这个问题你可以用断点的输出控制要执行的步数。
--------------------编程问答-------------------- 帮顶 --------------------编程问答-------------------- RowPrePaint
在绘制 DataGridViewRow 之前发生。
RowPostPaint 在绘制 DataGridViewRow 后发生。
先清除datagridview数据,再绑定数据或用BindingSource看看 --------------------编程问答--------------------
引用 4 楼 wuyq11 的回复:
RowPrePaint
在绘制 DataGridViewRow 之前发生。
RowPostPaint 在绘制 DataGridViewRow 后发生。
先清除datagridview数据,再绑定数据或用BindingSource看看


我试过先清除在绑定,但是好像是没用的 --------------------编程问答-------------------- 我也有同样的重复循环的问题,到底有哪些因素会触发RowPrePaint事件,望高手支招 --------------------编程问答--------------------  永远不要怀疑编译器    ···· --------------------编程问答-------------------- BangDing(); 你把它放在page_Load里就会刷新了
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,