zoukankan      html  css  js  c++  java
  • winform——绑定DataGridView

    ========================================================================================
    ==重点需要掌握==
    AutoSizeColumnsMode 设置为File 列填充效果
    RowHeadersVisible 设置false 第一列不显示
    SelectionModel 设置FullRowSelect 行被选中
    AllowUserToResizeColumns 设置False DataGridView是否可以调整列的大小
    AllowUserToResizeRows 设置False DataGridView是否可以调整行的大小:

    绑定DataGridView控件的方法:
    this.datagridview.DataSource=DBHelper.ExecuteTable("sql语句").DefaultView;

    取DataGridView的值绑定到文本框
    实现方式:在CellClick事件中实现如下代码
    方法1
    int rowIndex=this.dataGridView控件.CurrentCell.RowIndex;
    this.文本框名.Text = this.dataGridView控件[0,rowIndex].Value.ToString();
    this.文本框名.Text = this.dataGridView控件[1,rowIndex].Value.ToString();
    ========================================================================================


    注意:下列代码中的i是定义成全局变量
    ///[第一行] 代码如下:
    //获取选中行的索引值
    this.i = this.dataGridView1.SelectedCells[0].RowIndex;
    if (this.i <= 0)
    {
    MessageBox.Show("已经是第一行了!",
    "提示",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
    }
    else
    {
    this.dataGridView1.Rows[i].Selected = false;
    this.dataGridView1.Rows[0].Selected = true;
    }
    ///[下一行] 代码如下:
    //获取选中行的索引值
    this.i = this.dataGridView1.SelectedCells[0].RowIndex;
    if (this.i < this.dataGridView1.RowCount - 1)
    {
    this.dataGridView1.Rows[i].Selected = false;
    this.dataGridView1.Rows[++i].Selected = true;
    }
    else
    {
    MessageBox.Show("已经是最后一行了!",
    "提示",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
    }
    ///[上一行] 代码如下:
    //获取选中行的索引值
    this.i = this.dataGridView1.SelectedCells[0].RowIndex;
    if (this.i <= 0)
    {
    MessageBox.Show("已经是第一行了!",
    "提示",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
    }
    else
    {
    this.dataGridView1.Rows[i].Selected = false;
    this.dataGridView1.Rows[--i].Selected = true;
    }
    ///[最后行] 代码如下:
    //获取选中行的索引值
    this.i = this.dataGridView1.SelectedCells[0].RowIndex;
    if (this.i < this.dataGridView1.RowCount - 1)
    {
    this.dataGridView1.Rows[i].Selected = false;
    this.dataGridView1.Rows[this.dataGridView1.RowCount - 1].Selected = true;
    }
    else
    {
    MessageBox.Show("已经是最后一行了!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

  • 相关阅读:
    光纤网卡与HBA卡区别
    Windows远程桌面相关
    port bridge enable命令导致的环路
    堡垒机jumpserver测试记录--使用
    堡垒机jumpserver测试记录--安装
    Centos6.5升级openssh、OpenSSL和wget
    linux抓包工具tcpdump使用总结
    iOS -视频缩略图的制作
    Mac 上视图的坐标系统原点位于左下角
    Mac
  • 原文地址:https://www.cnblogs.com/you-zi/p/4351179.html
Copyright © 2011-2022 走看看