dataGridView属性、事件

 

///让所有列都显示出来

///
将最后一列的 AutoSizeMode 属性设置为 Fill,并对其他列使用其他调整大小选项。如果其他列使用了过多的可用空间,可设置最后一列的 MinimumWidth属性。
设定背景颜色          ------------交替行样式
 this.dataGridView2.RowsDefaultCellStyle.BackColor = Color.Bisque;              //默认样式
 this.dataGridView2.AlternatingRowsDefaultCellStyle.BackColor =Color.Beige;  //奇数样式    ---形成奇偶
冻结l列                    -----------效果移动横条时他不随着移动;
单击 DataGridView控件右上角的智能标记标志符号 ( ),然后选择“编辑列“;从“选定的列”列表中选择一列。 在“列属性”网格中,将 Frozen属性设置为 true。               
设定数据显示格式
在“列属性”网格中,单击 DefaultCellStyle属性旁的省略号按钮 ( )。“CellStyle 生成器”对话框出现。Format

///
///不让某列排序
///
NotSortable
dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
///
///转移到我们想要的哪一行
///
dataGridView1.CurrentCell=dataGridView1.Rows[1].Cells[1];

///
/// 常用事件处理
/// 

//数据解析           -----如数值解析文字  ;图片列;简单的密码显示

 private void dataGridView1_CellFormatting(object sender,

DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["id"].Index )        //数字解析;同时可以完成图片解析
            {
                int cc=Convert.ToInt32(e.Value);
                if (cc>2)
                {
                    e.Value = "可用";
                    e.CellStyle.ForeColor = Color.Red;
                }
                else
                {
                    e.Value = "不可用";
                    e.CellStyle.ForeColor = Color.Green;
                }
            }

///显示密码
///保存值的列隐藏,用一列显示****
///pwd列隐藏  realvalue显示*
            if (dataGridView1.Columns[e.ColumnIndex].Name == "realValue" && dataGridView1.Rows

[e.RowIndex].Cells["pwd"].Value != null && dataGridView1.Rows[e.RowIndex].Cells

["pwd"].Value.ToString().Length > 0)
            {
                e.Value = new string('*', dataGridView1.Rows[e.RowIndex].Cells

["pwd"].Value.ToString().Length);
            }
}

 ///
///在输入框中输入插叙条件  查找我们所要的数据,进一步检索
///dataGridView1.DataSource = nowData;
{
  string sql = textBox1.Text;
            //string sql = "sorting=3";
      
            DataTable newdt = new DataTable();
            newdt = nowData.Clone();
            DataRow[] drs = nowData.Select(sql);
            for (int i = 0; i < drs.Length; i++)
            {
                newdt.ImportRow((DataRow)drs[i]);          //这个很重要,不然一直提示该行已经存在表中
            }
            nowData.Clear();
            nowData = newdt;
          dataGridView1.DataSource = nowData;

}  

//将dataGridView里面的行锁定,即鼠标移到行与行交界处,不会出现双向箭头

 dataGridView1.AllowUserToResizeRows= false; 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值