如何使用C#从数据表绑定datagridview中的日历列? [英] How to bind a calendar column in datagridview from a datatable using C#?
本文介绍了如何使用C#从数据表绑定datagridview中的日历列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用存储过程从datable绑定datagridview,我的datagridview中有一个日历列,我尝试这个代码,它总是给我一个日历列中的最后一个值:
I'm trying to bind a datagridview from a datable using stored procedure and I have a calendar column in my datagridview, I try this code by it a gives me always the last value in the calendar column:
grdEmployees.Rows.Clear();
objGet = new GetClass();
DataTable dt = objGet.GetValues("GetVacationForStartWork", txtEmployeeCode.Text.Trim());
for (Int32 i = 0; i < dt.Rows.Count; i++)
{
grdEmployees.Rows.Add(Convert.ToDateTime(dt.Rows[i]["StartDate"]).ToString("MM/dd/yyyy")
, Convert.ToDateTime(dt.Rows[i]["EndDate"]).ToString("MM/dd/yyyy"),
dt.Rows[i]["IDVacation"]
);
}
foreach (DataGridViewRow row in grdEmployees.Rows)
{
for (Int32 i = 0; i < dt.Rows.Count; i++)
{
try
{
row.Cells["WorkStart"].Value = Convert.ToDateTime(row.Cells["EndDate"].Value).AddDays(1);
row.Cells["ConfirmedWorkStarted"].Value = Convert.ToDateTime(dt.Rows[i]["ConfirmedWorkStarted"]);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
我的存储过程输出:
My stored procedure output:
StartDate EndDate IDVacation ConfirmedWorkStarted
2015-06-06 2015-06-26 19 6/30/2015 12:00:00 AM
2015-07-20 2015-07-25 18 7/27/2015 12:00:00 AM
DataGridView输出:
DataGridView output:
StartDate EndDate IDVacation ConfirmedWorkStarted
2015-06-06 2015-06-26 19 7/27/2015
2015-07-20 2015-07-25 18 7/27/2015
有什么解决方案吗?
Any solution please ?
推荐答案
我不舒服但是替换
Im not shure but Replace
row.Cells["ConfirmedWorkStarted"].Value = Convert.ToDateTime(dt.Rows[i]["ConfirmedWorkStarted"]);
with:
with:
row.Cells["ConfirmedWorkStarted"].Value = Convert.ToDateTime(row.Cells["ConfirmedWorkStarted"])
问题是迭代循环。哪个不对。我做了一个样本。请查看以下内容。
DataTable dt = new DataTable();
dt.Columns.Add(StartDate);
dt.Columns.Add(EndDate);
dt.Columns.Add(IDVacation);
dt.Columns.Add(ConfirmedWorkStarted );
DataRow dr = dt.NewRow();
dr [StartDate] =2015-06-06 ;
dr [EndDate] =2015-06-26;
dr [IDVacation] =19;
dr [ConfirmedWorkStarted] =6/30/2015 12:00:00 AM;
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1 [StartDate] =2015-07-20;
dr1 [EndDate ] =2015-07-25;
dr1 [IDVacation] =18;
dr1 [ConfirmedWorkStarted] =2015/7/27 12:00:00 AM;
dt.Rows.Add(dr1);
grdEmployees.DataSource = dt;
grdEmployees.DataBind();
foreach(grdEmployees.Rows中的GridViewRow行)
{
row.Cells [3] .Text = Convert.ToDateTime( row.Cells [3] .Text).ToShortDateString();
}
你可以删除硬编码3并将自己的计数器放入其中。希望这有效。
The issue is with iterating the loops. Which is not correct. I did a sample on this. Check below.
DataTable dt = new DataTable();
dt.Columns.Add("StartDate");
dt.Columns.Add("EndDate");
dt.Columns.Add("IDVacation");
dt.Columns.Add("ConfirmedWorkStarted");
DataRow dr = dt.NewRow();
dr["StartDate"]="2015-06-06";
dr["EndDate"]="2015-06-26";
dr["IDVacation"]="19";
dr["ConfirmedWorkStarted"]="6/30/2015 12:00:00 AM";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1["StartDate"]="2015-07-20";
dr1["EndDate"]="2015-07-25";
dr1["IDVacation"]="18";
dr1["ConfirmedWorkStarted"]="7/27/2015 12:00:00 AM";
dt.Rows.Add(dr1);
grdEmployees.DataSource = dt;
grdEmployees.DataBind();
foreach (GridViewRow row in grdEmployees.Rows)
{
row.Cells[3].Text = Convert.ToDateTime(row.Cells[3].Text).ToShortDateString();
}
You can remove hard codes "3" and put your own counter i in it. Hope this works.
这篇关于如何使用C#从数据表绑定datagridview中的日历列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文