如何将值作为空的gridview的datetime列,它列为空 [英] How can I get the value as empty of a datetime column of the gridview , it column is empty
问题描述
嗨
当我检查 的价值时gridview列的空日期时间单元格,它始终显示'01 -01-0001',如何将gridview的空日期时间列的值设置为空而不是'01 -01-0001'请在c#中提供帮助。在给定的示例中,
如果MotDate为空,则ls值也应为空或为null。
$
var ls = Convert.ToDateTime(dt1.Rows [0] .Field< ; DateTime>(" MotDate"));
$
var ls1 = Convert.ToDateTime(dt1.Rows [0] .Field< DateTime>(" TaxDate"));
private void grvListVehicle_CellClick(object sender,DataGridViewCellEventArgs e)
{
if(e.ColumnIndex == 0)
{
DataTable dt = new DataTable();
for(int j = 0; j< grvListVehicle.Columns.Count; j ++)
{
if(j> = 1)
dt.Columns.Add(grvListVehicle .Columns [j] .Name,grvListVehicle.Columns [j] .CellType);
}
DataRow row = dt.NewRow();
dt.Rows.Add(row);
var dt1 =((DataTable)grvListVehicle.DataSource).Clone();
var currentRowData = grvListVehicle.CurrentRow.Cells.OfType< DataGridViewTextBoxCell>()。选择(cell => cell.Value).ToArray();
dt1.Rows.Add(currentRowData);
var _regno = dt1.Rows [0] .Field< string>(" VehicleType");
var ls = Convert.ToDateTime(dt1.Rows [0] .Field< DateTime>(" MotDate"));
var ls1 = Convert.ToDateTime(dt1.Rows [0] .Field< DateTime>(" TaxDate"));
frmVehicle frm = new frmVehicle(dt1);
this.Close();
frm.Show();
}
}
Pol
polachan
您好,
如果我们在临床上看一下,您就不应该触摸DataGridView来获取值,例如
DataTable dtSource = new DataTable();
dtSource.Columns.Add(new DataColumn(){ColumnName =" Event",DataType = typeof(string)});
dtSource.Columns.Add(new DataColumn(){ColumnName =" d1",DataType = typeof(DateTime)});
dtSource.Rows.Add(new object [] {" Jog",null});
dtSource.Rows.Add(new object [] {" Run",DateTime.Now});
dataGridView1.DataSource = dtSource;
var dMinDate = DateTime.MinValue;
dtSource.Rows.Add(new object [] {" walk",dMinDate});
dtSource.Rows.Add(new object [] {" rest",DateTime.Now.AddDays(2)});
DataTable dtTarget = dtSource.Clone();
dataGridView2.DataSource = dtTarget;
foreach(dtSource.Rows中的DataRow行)
{
dtTarget.ImportRow(row);
}如果您需要获取单行,请检查空日期,我们可以使用BindingSource,如下所示,从而不会触及DataGridView元素。 / p>
public partial class Form1:Form
{
public Form1()
{
InitializeComponent() ;
}
private void button1_Click(object sender,EventArgs e)
{
if(bsSource.Current!= null)
{
var row =((DataRowView)bsSource.Current).Row;
var test = row [" d1"]。GetType();
if(row [" d1"]!= DBNull.Value)
{
Console.WriteLine(row.Field< DateTime>(" d1"));
}
其他
{
//我们有一个空日期
}
}
}
BindingSource bsSource = new BindingSource();
private void Form1_Load(object sender,EventArgs e)
{
DataTable dtSource = new DataTable();
dtSource.Columns.Add(new DataColumn(){ColumnName =" Event",DataType = typeof(string)});
dtSource.Columns.Add(new DataColumn(){ColumnName =" d1",DataType = typeof(DateTime)});
dtSource.Rows.Add(new object [] {" Jog",null});
dtSource.Rows.Add(new object [] {" Run",DateTime.Now});
var dMinDate = DateTime.MinValue;
dtSource.Rows.Add(new object [] {" walk",dMinDate});
dtSource.Rows.Add(new object [] {" rest",DateTime.Now.AddDays(2)});
bsSource.DataSource = dtSource;
dataGridView1.DataSource = bsSource;
}
}
Hi
When I am checking the value of empty datetime cell of gridview column , it shows always '01-01-0001' , How can I get the value of empty datetime column of the gridview as empty rather than '01-01-0001' Please can you help in c#. In the given example,
if the MotDate is empty , the ls value should also be empty or null.
var ls = Convert.ToDateTime(dt1.Rows[0].Field<DateTime>("MotDate"));
var ls1 = Convert.ToDateTime(dt1.Rows[0].Field<DateTime>("TaxDate"));
private void grvListVehicle_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 0) { DataTable dt = new DataTable(); for (int j = 0; j < grvListVehicle.Columns.Count; j++) { if (j >= 1) dt.Columns.Add(grvListVehicle.Columns[j].Name, grvListVehicle.Columns[j].CellType); } DataRow row = dt.NewRow(); dt.Rows.Add(row); var dt1 = ((DataTable)grvListVehicle.DataSource).Clone(); var currentRowData = grvListVehicle.CurrentRow.Cells.OfType<DataGridViewTextBoxCell>().Select(cell => cell.Value).ToArray(); dt1.Rows.Add(currentRowData); var _regno = dt1.Rows[0].Field<string>("VehicleType"); var ls = Convert.ToDateTime(dt1.Rows[0].Field<DateTime>("MotDate")); var ls1 = Convert.ToDateTime(dt1.Rows[0].Field<DateTime>("TaxDate")); frmVehicle frm = new frmVehicle(dt1); this.Close(); frm.Show(); } }
Pol
polachan
Hello,
If we look at this clinically you should never have to touch the DataGridView to get values e.g.
DataTable dtSource = new DataTable(); dtSource.Columns.Add(new DataColumn() { ColumnName = "Event", DataType = typeof(string) }); dtSource.Columns.Add(new DataColumn() { ColumnName = "d1", DataType = typeof(DateTime) }); dtSource.Rows.Add(new object[] {"Jog",null}); dtSource.Rows.Add(new object[] {"Run", DateTime.Now }); dataGridView1.DataSource = dtSource; var dMinDate = DateTime.MinValue; dtSource.Rows.Add(new object[] {"walk", dMinDate }); dtSource.Rows.Add(new object[] { "rest", DateTime.Now.AddDays(2) }); DataTable dtTarget = dtSource.Clone(); dataGridView2.DataSource = dtTarget; foreach (DataRow row in dtSource.Rows) { dtTarget.ImportRow(row); }If you need to get at a single row, check for a null date we can use a BindingSource as shown below thus never touching the DataGridView elements.
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (bsSource.Current != null) { var row = ((DataRowView)bsSource.Current).Row; var test = row["d1"].GetType(); if (row["d1"] != DBNull.Value) { Console.WriteLine(row.Field<DateTime>("d1")); } else { // we have a null date } } } BindingSource bsSource = new BindingSource(); private void Form1_Load(object sender, EventArgs e) { DataTable dtSource = new DataTable(); dtSource.Columns.Add(new DataColumn() { ColumnName = "Event", DataType = typeof(string) }); dtSource.Columns.Add(new DataColumn() { ColumnName = "d1", DataType = typeof(DateTime) }); dtSource.Rows.Add(new object[] {"Jog",null}); dtSource.Rows.Add(new object[] {"Run", DateTime.Now }); var dMinDate = DateTime.MinValue; dtSource.Rows.Add(new object[] {"walk", dMinDate }); dtSource.Rows.Add(new object[] { "rest", DateTime.Now.AddDays(2) }); bsSource.DataSource = dtSource; dataGridView1.DataSource = bsSource; } }
这篇关于如何将值作为空的gridview的datetime列,它列为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!