简体   繁体   中英

how to create Schedule in c#

I'm new into programing and still learning. Currently I'm coding simple form for my schedule program (c#) in MS Visual Studio 2017. I already program day in month grid ( when you change date program generate gridview for it) but I have two important problem:


  1. I would like to get day number in each cell, I tried use loop while and foreach but it doesn't work....
  2. Second thing is that I would like to save my appointment in cell. If its possible I would rather not use database, and I really don't want to use solutions that I would have to pay.

Here's my code:

namespace WindowsFormsApp1
{
public partial class Kalendarz : Form
{
    public Kalendarz()
    {
        InitializeComponent();
    }



    private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {
        DateTime date_start = this.dateTimePicker1.Value;
        this.textBox2.Text = date_start.ToString("hh-mm-dd-MM-yyyy"); // czas startu
        this.textBox1.Text = date_start.ToString("MMMM");
        int Month = date_start.Month;
        int Year = date_start.Year;
        int NoD = DateTime.DaysInMonth(Year, Month);

        Decimal NoWD = Decimal.Divide(NoD, 7.0m);


        if (NoWD > 4.1m)
        {
            int WN = 5;
            dataGridView1.RowCount = WN;
            int n0 = 1;

           foreach(DataGridCell cell in dataGridView1)
            {

                while (n0 = NoD; n0++)

            }

        }
        else
        {
            int WN = 4;
            dataGridView1.RowCount = WN;

        }




    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
        {
            dataGridView1.Columns["s"].Visible = true;
            dataGridView1.Columns["st"].Visible = true;
        }
        else if (!checkBox1.Checked)
        {
            dataGridView1.Columns["s"].Visible = false;
            dataGridView1.Columns["st"].Visible = false;

        }

    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }
}

Do you have any suggestions what can I do ? I found something like that but I don't have any idea how to loop it dataGridView.CurrentCell.Value = newValue.ToString ();

Be careful, what you whish for ...

Regarding 1st request: This code will fill a DataGridView with day numbers, starting on the set day in week and not exceeding number of days in month, provided you have enough rows an columns, filling it from left to right (as opposed to ie top-down). I'd advice 7 columns, unless you want to have a lot of fun with calendar. Guessing from your name, you're Polish and as far as I know week starts with Monday in Poland. You'd need a modification to the code for week starting with Sunday.

VB.NET:

    Dim DayCounter As Int16 = 1
    Dim DayInWeekStart As Int16 = 2   ' 1 = MON, 2 = TUE, 3 = WED, ...
    Dim DayInMonthTotal As Int16 = 31
    For ir = 0 To Me.dgv.rows.count - 1
        For ic = 0 To Me.dgv.columns.count - 1   ' you should have 7 columns for MON-SUN in your DataGridView
            If ir = 0 And (ic + 1) < DayInWeekStart And DayCounter < DayInMonthTotal Then
                ' skip / do nothing
            Else
                Me.dgv.rows(ir).cells(ic).value = DayCounter   ' set DGV cell value to the date
                DayCounter += 1   ' iterate day in month number
            End If
        Next
    Next

C# (converted):

    Int16 DayCounter = 1;
    Int16 DayInWeekStart = 2;
    // 1 = MON, 2 = TUE, 3 = WED, ...
    Int16 DayInMonthTotal = 31;
    For (ir = 0; ir <= this.dgv.rows.count - 1; ir++) {
        // you should have 7 columns for MON-SUN in your DataGridView
        For (ic = 0; ic <= this.dgv.columns.count - 1; ic++) {
            If (ir == 0 & (ic + 1) < DayInWeekStart & DayCounter < DayInMonthTotal) Then {
                // skip / do nothing
            } else {
                this.dgv.rows(ir).cells(ic).value = DayCounter;
                // set DGV cell value to the date
                DayCounter += 1;
                // iterate day in month number
            }
        }
    }

As for saving, the best option would be definitely a database, even though a structured text file is doable. If you have a lot of time... For database, I'd use MERGE statement to UPDATE, DELETE or INSERT appointments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM