当前位置: 代码迷 >> C# >> datagridview單元格賦值問題解决办法
  详细解决方案

datagridview單元格賦值問題解决办法

热度:341   发布时间:2016-05-05 05:27:36.0
datagridview單元格賦值問題
try
            {
                sql = "select paste_name,qty,start_date,end_date,op from sajet.xx_paste where start_date>sysdate-2 and op='return'";
                DataSet ds = ClientUtils.ExecuteSQL(sql);
                dataGridView1.DataSource = ds.Tables[0];
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    string start_time = dataGridView1.Rows[i].Cells[3].Value.ToString();
                    int time = 0;
                    if (dataGridView1.Rows[i].Cells[5].Value.ToString() == "return")
                    {
                        sql = "select a.return_time from sajet.xx_paste_type a left join sajet.sys_part b on a.part_name=b.part_no left join sajet.g_material c on b.part_id=c.part_id where c.reel_no='" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "'";
                        DataTable dt = ClientUtils.ExecuteSQL(sql).Tables[0];
                        time = (Convert.ToInt32(start_time.Substring(0, 4)) - DateTime.Now.Year) * 365 * 24 * 60 + (Convert.ToInt32(start_time.Substring(5, 2)) - DateTime.Now.Month) * 30 * 24 * 60 + (Convert.ToInt32(start_time.Substring(8, 2)) - DateTime.Now.Day) * 24 * 60 + (Convert.ToInt32(start_time.Substring(11, 2)) - DateTime.Now.Hour) * 60 + (Convert.ToInt32(start_time.Substring(14, 2)) - DateTime.Now.Minute) + Convert.ToInt32(dt.Rows[0][0].ToString()) * 60;
                    }
                    if (time < 0)
                        this.dataGridView1.Rows[i].Cells["last_time"].Value = "0";
                    else
                        this.dataGridView1.Rows[i].Cells["last_time"].Value = time.ToString();
                }
                //set_lt();
            }
            catch (Exception ex)
            {
                MessageBox.Show("錯誤信息:" + ex.Message);
            }



代碼如上面,但是this.dataGridView1.Rows[i].Cells["last_time"].Value = time.ToString();
這句執行完後不顯示值.
請大家幫下忙解決下
------解决思路----------------------
数据库里取出来,转DateTime类型,然后用DateTime.AddYear,DateTime.AddMonth等方法实现加减,或者直接两个DateTime类型相减,不要自己截取字符串拼接字符串
------解决思路----------------------
怀疑控件问题之前,先把乱七八糟的代码都注释掉
就执行数据绑定,看数据能显示?
如果能显示,直接给dataGridView1.Rows[1].Cells[6]赋值,看能显示?
------解决思路----------------------
last_time这个列名有吗?在datagridview
  相关解决方案