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

该字符串未被识别为有效的 DateTime。有一个从索引 0 处开始的未知字。

dataGridView1 外的 button按钮:

private void button6_Click(object sender, EventArgs e)
        {
            //string t = this.dataGridView1.CurrentRow.Cells[7].Value.ToString();

            DateTime t1 = DateTime.Parse(this.dataGridView1.CurrentRow.Cells[6].Value.ToString());
            DateTime t2 = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
      
            TimeSpan t3 = t2 - t1;
            int days = t3.Days;
            if (days <= 30)
            {

             
                MessageBox.Show("还有几天归还:" + days, "提示");
            }
         }
怎么一直提示:该字符串未被识别为有效的 DateTime。有一个从索引 0 处开始的未知字。 --------------------编程问答-------------------- winform 的问题……
--------------------编程问答-------------------- dataGridView1.CurrentRow.Cells[6].Value.ToString()的值無效或為空值 --------------------编程问答-------------------- 怎么会这样呢?为什么?又该怎么解决???
详细点吗?我是第一次问问题………… --------------------编程问答-------------------- dataGridView1.CurrentRow.Cells[6].Value.ToString()
这句有问题,没取到数据,确定索引对不对? --------------------编程问答-------------------- 索引我是一个一个试的,可还是不行…… --------------------编程问答-------------------- 可能是输入的时间格式不对。用selectedRows试试。
例:
for (int i = 0; i < this.dataGridView.SelectedRows.Count; i++)
{
    this.dataGridView.SelectedRows[i].Cells["NRT"].Value = "Yes";
    this.dataGridView.SelectedRows[i].Cells["Online"].Value = "Yes";
} --------------------编程问答-------------------- 跟踪下this.dataGridView1.CurrentRow.Cells[6].Value.ToString()出来的是个什么东西。
格式对吗?
“索引0处”意思是系统认为这个字符串从第一个字符开始就是非法的。 --------------------编程问答-------------------- this.dataGridView1.CurrentRow.Cells[6].Value.ToString()
检查这个数据。或者this.dataGridView1.CurrentRow.Cells[6].Value.ToString().Trim(); --------------------编程问答-------------------- 先不要转成时间格式嘛,可以选用字符串存下,看看是什么再说
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,