-3
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
        try
        {
            if ((dataGridView1.Focused) && (dataGridView1.CurrentCell.ColumnIndex == 0))
            {
                dtpInstallment.Location = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location;
                dtpInstallment.Visible = true;
                if (dataGridView1.CurrentCell.Value != DBNull.Value)
                {
                   // dtpInstallment.Value = DateTime.Today;
                   dtpInstallment.Value = (DateTime)dataGridView1.CurrentCell.Value;

               //     DateTime date = (DateTime)dataGridView1.CurrentCell.Value;
                 //   dtpInstallment.Value = DateTime.Parse(date.ToString("dd/MM/yyyy"));


                }
                else
                {
                    dtpInstallment.Value = DateTime.Today;
                }
            }
            else
            {
                dtpInstallment.Visible = false;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

このdatetimeでは例外がスローされています... dataGridView1.CurrentCell.Valueに値があります..しかし、DateTimePickerであるdtpInstallment.valueに変換できません

4

4 に答える 4

2

これは、解析している値の形式が正しくないためです。使ってみてParseExact

string poop = "2005-12-14 23:12:34";
string currentFormat = "yyyy-MM-dd HH:mm:ss";
DateTime poo = DateTime.ParseExact(poop, currentFormat, System.Globalization.CultureInfo.InvariantCulture);
// yyyy-MM-dd HH:mm:ss ==> you can change the format that matches the current
//                         value of your dataGridView1.CurrentCell.Value
于 2012-09-10T06:47:32.443 に答える
1

セルの値は有効な日付ではありません。おそらく、代わりにDateTime.TryParseを試してください。そうすれば、有効な形式である場合は DateTime が取得され、そうでない場合も例外はありません。

于 2012-09-10T06:48:42.723 に答える
1

CellContentClick イベントを使用してみてください

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
 DateTime dtpInstallment = DateTime.Parse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}

次に dataGridView1_CellBeginEdit イベントを使用します

于 2012-09-10T07:35:16.677 に答える
0

DBNull.Value をチェックするので、SqlDataSource を使用してデータを取得すると思いますが、datetime 値は実際にはSqlDateTime型です。

于 2012-09-10T06:53:05.437 に答える