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

c#datagridview

时间:2015-09-09 21:10:52      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

//保证显示当前活动单元格
this.Invoke(new Action(() =>
{
 dataGridView1.CurrentCell = dataGridView1.Rows[index].Cells[0]; 
}));
//添加新行
index=dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells[
0].Value = (index+1).ToString();//添加内容 dataGridView1.Rows[index].Cells[1].Value = ip;//添加内容 dataGridView1.Rows[index].Cells[2].Value = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss"); //添加内容
    //鼠标右键弹出菜单
  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];
                    }
                    //弹出操作菜单
                    contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
  }  
 //表头自动序号
private void dataGridView1_RowPostPaint_1(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X, e.RowBounds.Location.Y + 4);

        }

 

c#datagridview

标签:

原文地址:http://www.cnblogs.com/milest/p/4795764.html

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