码迷,mamicode.com
首页 > Windows程序 > 详细

c# datagridview表格控件常用操作

时间:2016-04-11 10:06:50      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:

1) 行右键菜单

  private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right){          
                if (e.RowIndex >= 0){              
                    //若行已是选中状态就不再进行设置              
                    if (dataGridView1.Rows[e.RowIndex].Selected == false)
                    {                  
                        dataGridView1.ClearSelection();                  
                        dataGridView1.Rows[e.RowIndex].Selected = true;             
                    }              

                    //只选中一行时设置活动单元格              
                    if (dataGridView1.SelectedRows.Count == 1){
                        dataGridView1.CurrentCell =  dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];             
                    }


                    if (dataGridView1.CurrentRow != null)
                    {
                        int idx = dataGridView1.CurrentRow.Index;
                        if (idx >= 0)
                        {
                            string isexport = mDataTableCun.Rows[idx]["ISEXPORT"].ToString();
                            if (isexport == "已导出")
                            {
                                //弹出操作菜单              
                                contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);  
                            }
                        }
                      
                    }

                        
                }     
            } 
        }

2) 行自动编号

  private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
            e.RowBounds.Location.Y,
            dataGridView1.RowHeadersWidth - 4,
            e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
            dataGridView1.RowHeadersDefaultCellStyle.Font,
            rectangle,
            dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
            TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

3) 单元格取值设值

  private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                dataGridView1.EndEdit();
                string ischeck = dataGridView1.Rows[e.RowIndex].Cells["visible"].Value.ToString();
                if (ischeck == "0")
                {
                    dataGridView1.Rows[e.RowIndex].Cells["edit"].Value = "0";
                }
            }
        }

  

c# datagridview表格控件常用操作

标签:

原文地址:http://www.cnblogs.com/janehlp/p/5377147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!