在数据网格视图中显示选定的月份日期我试过但它不起作用帮助我 [英] display selected month dates in data grid view i tried but it not working help me
问题描述
我有一个datetimepicker。
我的代码如下:
i have one datetimepicker.
My code as follows:
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = MyDataTable();
}
private DataTable MyDataTable()
{
DataTable dt = new DataTable("MyDataTable");
//Create columns and add to DataTable;
DataColumn dcID = new DataColumn("Month");
dt.Columns.Add(dcID); //ID column created and add to DataTable
//Now Add some data to the DataTable
DataRow dr;
for (int count = 1; count <= 30; count++)
{
dr = dt.NewRow();
dr["Month"] = count;
dt.Rows.Add(dr);
}
return dt;
}
来自上面的代码我在数据网格视图中得到如下输出;
from the above code i get the below output as follows in data grid view;
Select Month
Checkbox 1
Checkbox 2
Checkbox 3
Checkbox 4
Checkbox 5
Checkbox 6
Checkbox 7
Checkbox 8
Checkbox 9
Checkbox 10
Checkbox 11
Checkbox 12
Checkbox 13
Checkbox 14
Checkbox 15
Checkbox 16
Checkbox 17
Checkbox 18
Checkbox 19
Checkbox 20
Checkbox 21
Checkbox 22
Checkbox 23
Checkbox 24
Checkbox 25
Checkbox 26
Checkbox 27
Checkbox 28
Checkbox 29
Checkbox 30
但是我想在datagridview中输出如下内容;
i有一个datetimepicker
当我点击datetimepicker时,partiuclar选择的月份日期会显示在datagridview,
在dateTimePicker1_ValueChanged事件中我要编写什么代码,以便所选的月份日期将显示在datagridview中。
我怎么能用csharp编写代码。
请帮帮我。 />
But want i output as follows in datagridview;
i have one datetimepicker
when i click the datetimepicker that partiuclar selected month date will be displayed in the datagridview,.
in the dateTimePicker1_ValueChanged event what code i have to written, so that selected month date will be displayed in the datagridview.
for that how can i write the code using csharp.
please help me.
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
//how can i write the code using c sharp here.
}
谢谢& Rgds,
Narasiman P
Thanks & Rgds,
Narasiman P
推荐答案
我已经覆盖/更改了你的MyDataTable函数来传递一个参数,该参数是天数选定的月份并修改循环以填充数据表...
I''ve overridden / changed your MyDataTable function to pass a parameter which is the number of days in the selected month and amended the loop to populate the datatable ...
private DataTable MyDataTable(int MaxCount)
{
DataTable dt = new DataTable("MyDataTable");
//Create columns and add to DataTable;
DataColumn dcID = new DataColumn("Month");
dt.Columns.Add(dcID); //ID column created and add to DataTable
//Now Add some data to the DataTable
DataRow dr;
for (int count = 1; count <= MaxCount; count++)
{
dr = dt.NewRow();
dr["Month"] = count;
dt.Rows.Add(dr);
}
return dt;
}
然后可以像这样写出ValueChanged函数
Then the ValueChanged function can be written like this
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
DateTime dt = dateTimePicker1.Value;
int maxDays = DateTime.DaysInMonth(dt.Year, dt.Month);
dataGridView1.DataSource = MyDataTable(maxDays);
}
计算出本月的天数可能会进入MyDataTable函数,但我并不热衷于将UI控件混合在一起通用功能。
...
现在再次阅读你的问题我不确定如果你只想将一个日期添加到dataGridView,那么我也要发布一个解决方案......
Working out the number of days in the month could have gone into the MyDataTable function but I''m not keen on mixing UI control stuff in with generic functions.
...
Now having read your question again I''m not sure if you want just a single date added to the dataGridView, so I''m also posting a solution to that ...
private DataTable NewDataTable(DateTime myDate)
{
DataTable dt = new DataTable("MyDataTable");
dt.Columns.Add(new DataColumn("Month"));
dt.Columns.Add(new DataColumn("Day"));
dt.Columns.Add(new DataColumn("Year"));
DataRow dr = dt.NewRow();
dr.ItemArray = new object[] {myDate.Month, myDate.Day, myDate.Year};
dt.Rows.Add(dr);
return dt;
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
dataGridView1.DataSource = NewDataTable(dateTimePicker1.Value);
}
这篇关于在数据网格视图中显示选定的月份日期我试过但它不起作用帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!