关于DataGridView控件部分的使用

本文详细介绍了DataGridView控件的使用技巧,包括如何响应用户选择开始和结束时间的事件,通过遍历控件的行和列来改变单元格背景颜色,实现时间范围的视觉突出显示。此外,还涉及了如何检查和更新单元格状态,确保数据的一致性和准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于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; //只找开始时间最近的预约标记
                    }
                }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值