且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

日期时间选择器选择要在datagridview中显示的月份日期

更新时间:2023-01-29 08:28:21

根据我的理解,您希望在每月的日期列表中填写数据网格。

您可以在DateTime类中使用静态方法DaysInMonth(int year,int month)来获取请求月份的天数。

从那里您可以构建一个字符串集合(或例如:



  int  daysInMonth = DateTime.DaysInMonth( 2013  2 ); 
string [] daysCollection = new string [daysInMonth];
for int index = 0 ; index < daysInMonth; index ++)
{
daysCollection [index] =(index + 1 )。ToString()+ / 2/2013​​;
}







现在,您可以使用刚刚创建的集合填充数据网格数据源


如果你只需要从DateTimePicker中检索日期,那么你可以使用代码

 DateTime date = DateTimePicker.Value.Date; 



如果你只需要一个月就可以使用

 DateTime month = DateTimePicketr.Value.Month; 


Note: It is a windows applciation

i have one datetimepicker.when i select the month that month date will be displayed in dategridview.


Ouptut as follows in datagridview;


when i select the datetimepicker that month date will be displayed in datagridview.

for example i choose, March in datetime picker means in datagridview output as follows;


Datgridview


March
1/1/2013
2/1/2013
3/1/2013
4/1/2013
5/1/2013
6/1/2013
7/1/2013
8/1/2013
9/1/2013
10/1/2013
11/1/2013
12/1/2013
13/1/2013
15/1/2013
16/1/2013
17/1/2013
18/1/2013
19/1/2013
20/1/2013
21/1/2013
22/1/2013
23/1/2013
24/1/2013
25/1/2013
26/1/2013
27/1/2013
28/1/2013
29/1/2013
30/1/2013
31/1/2013

From what I understand you want to fill the datagrid in the list of days per month.
You can use the static method DaysInMonth(int year, int month) in the DateTime class to get the number of days the requested month has.
From there you can build a collection of strings (or whatever).
For example:

int daysInMonth = DateTime.DaysInMonth(2013, 2);
string[] daysCollection = new string[daysInMonth];
for (int index = 0; index < daysInMonth; index++)
{
    daysCollection[index] = (index + 1).ToString() + "/2/2013";
}




Now you can fill your datagrid data source with the collection you just created


If u need only to retrieve date from DateTimePicker then u can use the code
DateTime date = DateTimePicker.Value.Date;


if u need only month then u can use

DateTime month  = DateTimePicketr.Value.Month;