我是编程和学习的新手。目前我在MS Visual Studio 2017中为我的日程安排程序(c#)编写了简单的表格。我已经编制了每月一次的网格(当你更改日期程序生成gridview时),但我有两个重要的问题:
这是我的代码:
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)
{
}
}
您有什么建议我该怎么办?
我找到了类似的东西,但我不知道如何循环它
dataGridView.CurrentCell.Value = newValue.ToString ();
答案 0 :(得分:0)
小心,你要求什么 ......
关于第一个请求:此代码将使用日期编号填充DataGridView,从星期设置日开始,不超过月中的天数,前提是您有足够的行列,从左到右填充(相反)自上而下)。我建议7列,除非你想要与日历有很多乐趣。 从你的名字猜测,你是波兰语,据我所知星期一在波兰开始。从星期日开始,您需要修改一周的代码。
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#(已转换):
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
}
}
}
至于保存,最好的选择肯定是数据库,即使结构化文本文件是可行的。如果你有很多时间...... 对于数据库,我使用MERGE语句来更新,删除或插入约会。