Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
 
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.
          }
 

Thanks & Rgds,
Narasiman P
Posted 13-Feb-13 3:51am
Edited 13-Feb-13 4:25am
CHill6074.3K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
}
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);
}
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);
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mika Wendelius 161
1 Abhinav S 115
2 Kornfeld Eliyahu Peter 100
3 Suvendu Shekhar Giri 100
4 Peter Leow 100
0 Sergey Alexandrovich Kryukov 8,513
1 OriginalGriff 6,526
2 Peter Leow 3,657
3 Zoltán Zörgő 3,426
4 Richard MacCutchan 2,417


Advertise | Privacy | Mobile
Web04 | 2.8.150123.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100