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);
    }

  • 相关阅读:
    springmvc spring mybatis框架整合
    多线程bug修复
    OutOfMemory
    CSS中强悍的相对单位之em(em-and-elastic-layouts)学习小记
    css中line-height行高的深入学习
    HTML5的新语义化的标签
    关于Three.js基本几何形状
    【Google Chrome】 Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource问题解决
    模拟Bootstrap响应式网格系统
    关于我对写博客那些事儿的实用心得
  • 原文地址:https://www.cnblogs.com/you-zi/p/4351179.html
Copyright © 2011-2022 走看看