Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have one datetimepicker.

My code as follows:
C#
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.

C#
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
         {
           
              //how can i write the code using c sharp here.
          }



Thanks & Rgds,
Narasiman P
Posted
Updated 13-Feb-13 3:25am
v2

1 solution

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 ...
C#
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
C#
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);
}
 
Share this answer
 
Comments
Member 12455619 18-Apr-16 3:18am    
When i choose a month in combobox the dates of the months should be displayed in grid column..how to write code for this in windows form application using c#
CHill60 19-Apr-16 16:50pm    
If you have a question of your own you should really use the red "Ask a Question" link, however, you can still use the DateTime.DaysInMonth function as I have before, just note that you will also need to know what year it is.
For example I have two comboBoxes:
var year = new List<int>();
for (var i = 2000; i < 2020; i++)
year.Add(i);
cmbYear.DataSource = year;
cmbYear.Text = DateTime.Now.Year.ToString();

for (var i = 1; i < 13; i++)
cmbMonths.Items.Add(new DateTime(2000, i, 1).ToString("MMM", CultureInfo.InvariantCulture));
And then I can fill the column headers of the datagridview like this:
private void cmbMonths_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbMonths.SelectedItem == null || cmbYear.SelectedItem == null) return;
var m = DateTime.ParseExact("September", "MMMM", CultureInfo.CurrentCulture).Month;
var daysInMonth = DateTime.DaysInMonth(int.Parse(cmbYear.SelectedValue.ToString()), m);
for (var i = 1; i <= daysInMonth; i++)
dataGridView1.Columns.Add(i.ToString(), i.ToString());

}

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900