C#winform datagridview单元格的单击处理

首先看看效果图:

需求:要求是的在datagridview里面绑定数据后,可以任意点击想要点击的某列的单元格进行改变数据。需要在datagridview里面写3个事件

1.RowPrePaint事件:主要设置要点击的某单元对应的某列显示的颜色

   private void dgv_Data_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            try
            {
                FontStyle newStyle = this.dgv_Data.DefaultCellStyle.Font.Style;
                newStyle |= FontStyle.Underline;
                Font font = new Font(this.dgv_Data.DefaultCellStyle.Font, newStyle);
                foreach (DataGridViewRow dr in this.dgv_Data.Rows)
                {
                    if (dr.Cells["ID"].Value.ToString() == "0")
                    {
                        dr.DefaultCellStyle.ForeColor = Color.Red;
                    }
                    dr.Cells["馆藏重复"].Style.Font = font;
                    dr.Cells["馆藏重复"].Style.ForeColor = Color.Blue;
                    dr.Cells["是否采购"].Style.Font = font;
                    dr.Cells["是否采购"].Style.ForeColor = Color.Blue;                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

2.CellMouseMove事件:主要是鼠标指针移动到单元格时候的样式

private void dgv_Data_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == -1 || e.RowIndex == -1)
            {
                return;
            }
            DataGridViewCell cell = this.dgv_Data.Rows[e.RowIndex].Cells[e.ColumnIndex];
            if (cell == null || cell.Value.ToString() == "" || cell.Value.ToString() == "0")
            {
                return;
            }
            string headText = this.dgv_Data.Columns[e.ColumnIndex].HeaderText;
            if (headText == "馆藏重复" || headText == "是否采购")
            {
                this.Cursor = Cursors.Hand;
            }
            else
            {
                this.Cursor = Cursors.Default;
            }
        }

3.CellClick事件:主要是对单元格进行单击的数据库操作

private void dgv_Data_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == -1 || e.RowIndex == -1)
                {
                    return;
                }
                DataGridViewCell cell = this.dgv_Data.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if (cell == null || cell.Value.ToString() == "" || cell.Value.ToString() == "0")
                {
                    return;
                }
                Stay_PurchaseData bll = new Stay_PurchaseData();
                string headText = this.dgv_Data.Columns[e.ColumnIndex].HeaderText;                
                switch (headText)
                {
                    case "馆藏重复":
                        if (cell.Value.ToString().Trim() == "")
                        {
                            bll.ChangeGCCF(dgv_Data.Rows[e.RowIndex].Cells["ID"].Value.ToString(), true);
                        }
                        else
                        {
                            bll.ChangeGCCF(dgv_Data.Rows[e.RowIndex].Cells["ID"].Value.ToString(), false);
                        }
                        break;
                    case "是否采购":
                        if (cell.Value.ToString().Trim() == "")
                        {
                            bll.ChangeSFCG(dgv_Data.Rows[e.RowIndex].Cells["ID"].Value.ToString(), true);
                        }
                        else
                        {
                            bll.ChangeSFCG(dgv_Data.Rows[e.RowIndex].Cells["ID"].Value.ToString(), false);
                        }
                        break;                                          
                }
                //刷新
                btn_Query_Click(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

 

posted @ 2016-08-23 16:46  我的名称很霸气  阅读(13145)  评论(0编辑  收藏  举报