C#机房重构--DataGridView用法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aimin_com/article/details/82731286

一:显示数据库传值数据

//判断数据库中是否有值,如果为0则表示没有
 if (table.Rows.Count==0)
            {
                MessageBox.Show("此日期没有人盈利,同志们再接再厉");
            }
            else
            {
                dataGV.DataSource = "";//清空DataGridView中的值
                dataGV.DataSource = table;//将数据库返回的DataTable类型的变量传给DataGridView显示
                dataGV.AllowUserToAddRows = false;//取消最后一行空白格
                
                //自动调整显示的大小
                dataGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                dataGV.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
                
				//设置标题为一下名称
                dataGV.Columns[0].HeaderText = "操作员账号";
                dataGV.Columns[1].HeaderText = "盈利金额";
                dataGV.Columns[2].HeaderText = "充值金额";
                dataGV.Columns[3].HeaderText = "注册金额";
                dataGV.Columns[4].HeaderText = "管理员";
                dataGV.Columns[5].HeaderText = "日期";
                dataGV.Columns[6].HeaderText = "时间";
            }

二:设置某一列为*号,比如密码列(在CellFormatting事件中)


        /// <summary>
        /// 设置密码列为六位*号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGVDeleteUser_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //把第三列显示*号,*号的个数为6位
            if (e.ColumnIndex==2) //如果第三列
            {
                if (e.Value!=null && e.Value.ToString().Length>0)  //如果不为空并且此列大于0有值
                {
                    e.Value = new string('*', 6);  //表示此列为*号并且为6位
                }
            }

        }

三:如果不想写上面的标题可以在这里面设置

在这里插入图片描述

四:判断

//判断是否选中:可以写在方法里面或者load里面都可以
 if (dataGVInquIryLineState.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选中一行");
                return;
            }

//判断如果为空,不能都点击
 if (dataGVInquIryLineState.Rows[dataGVInquIryLineState.CurrentRow.Index].Cells[0].Value.ToString() == null)
            {
                MessageBox.Show("没有上机人员,可以休息了");
            }

如果想知道更多的细节,可以参考一下此篇博客:dataGrideView

五:设置只读和不显示

			DataGridView1.ReadOnly = true; //全部只读

            DataGridView1.Columns[1].ReadOnly = true;// 设置 DataGridView1 的第2列整列单元格为只读

            DataGridView1.Rows[2].ReadOnly = true;// 设置 DataGridView1 的第3行整行单元格为只读

            DataGridView1[0, 0].ReadOnly = true;// 设置 DataGridView1 的[0,0]单元格为只读

			DataGridView1.Columns[6].Visible = false; //第六列不显示

心得
在机房重构中无非就是这几种的用法,感谢大家观看,期待给出更好的反馈哦!

猜你喜欢

转载自blog.csdn.net/aimin_com/article/details/82731286