關於DataGridView控制元件部分的使用
if (e.ColumnIndex == 1)
{
m = e.RowIndex;
txt_startTime.Text = dgv_reserve.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
tc();
txt_endTime.Text = "";
dgv_reserve.Rows[e.RowIndex].Cells[1].Style.BackColor = Color.MediumOrchid;
//dgv_reserve.Rows[e.RowIndex].Cells[1].Style.SelectionBackColor = Color.Green;
for (int i = e.RowIndex; i >= 0; i--)//塗抹小於此開始時間的結束時間部分
{
if (DateTime.Parse(txt_startTime.Text) >= (DateTime)dgv_reserve.Rows[i].Cells[2].Value && dgv_reserve.Rows[i].Cells[2].Style.BackColor != Color.Red && dgv_reserve.Rows[i].Cells[1].Style.BackColor != Color.Red)
{
dgv_reserve.Rows[i].Cells[2].Style.BackColor = Color.Gray;
}
}
for (int i = e.RowIndex; i < dgv_reserve.RowCount; i++)//大於此開始時間的結束時間部分
{
if (dgv_reserve.Rows[i].Cells[2].Style.BackColor == Color.Red && dgv_reserve.Rows[i].Cells[1].Style.BackColor == Color.Red)//尋找被預約的標
{
DateTime tm = (DateTime)dgv_reserve.Rows[i].Cells[2].Value;
for (int j = e.RowIndex; j < dgv_reserve.RowCount; j++)//塗抹大於此開始時間的下一個區間的結束時間部分
{
if (tm < (DateTime)dgv_reserve.Rows[j].Cells[2].Value)
{
if (dgv_reserve.Rows[j].Cells[2].Style.BackColor != Color.Red)
{
dgv_reserve.Rows[j].Cells[2].Style.BackColor = Color.Gray;
}
}
}
break;//只找開始時間最近的預約標
}
}
//if (rowindex >= 0)
//{
//dgv_reserve.Rows[rowindex].Cells[2].Style.BackColor = Color.Green;
//}
//rowindex = e.RowIndex;
return;
}
tc();
if (e.ColumnIndex == 2)
{
n = e.RowIndex;
if (txt_startTime.Text == "")
{
MessageBox.Show("請先選擇開始時間");
return;
}
if (txt_startTime.Text != "")
{
txt_endTime.Text = dgv_reserve.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
//dgv_reserve.Rows[e.RowIndex].Cells[2].Style.BackColor = Color.Green;
//dgv_reserve.Rows[e.RowIndex].Cells[2].Style.SelectionBackColor = Color.Green;
for (int i = e.RowIndex; i >= 0; i--)//塗抹小於此開始時間的結束時間部分
{
if (DateTime.Parse(txt_startTime.Text) >= (DateTime)dgv_reserve.Rows[i].Cells[2].Value && dgv_reserve.Rows[i].Cells[2].Style.BackColor != Color.Red && dgv_reserve.Rows[i].Cells[1].Style.BackColor != Color.Red)
{
dgv_reserve.Rows[i].Cells[2].Style.BackColor = Color.Gray;
}
}
}
for (int i = m; i <= n; i++)
{
dgv_reserve.Rows[i].Cells[2].Style.BackColor = Color.MediumOrchid;
dgv_reserve.Rows[i].Cells[1].Style.BackColor = Color.MediumOrchid;
}
for (int i = e.RowIndex; i < dgv_reserve.RowCount; i++)//大於此開始時間的結束時間部分
{
if (dgv_reserve.Rows[i].Cells[2].Style.BackColor == Color.Red && dgv_reserve.Rows[i].Cells[1].Style.BackColor == Color.Red)//尋找被預約的標記
{
DateTime tm = (DateTime)dgv_reserve.Rows[i].Cells[2].Value;
for (int j = e.RowIndex; j < dgv_reserve.RowCount; j++)//塗抹大於此開始時間的下一個區間的結束時間部分
{
if (tm < (DateTime)dgv_reserve.Rows[j].Cells[2].Value)
{
if (dgv_reserve.Rows[j].Cells[2].Style.BackColor != Color.Red)
{
dgv_reserve.Rows[j].Cells[2].Style.BackColor = Color.Gray;
}
}
}
break; //只找開始時間最近的預約標記
}
}