首页 > 编程语言 >C# dataGridView 中怎么判断垂直滚动条是否出现,如何检测dataGridView控件中的垂直滚动条

C# dataGridView 中怎么判断垂直滚动条是否出现,如何检测dataGridView控件中的垂直滚动条

时间:2022-10-31 11:59:17浏览次数:51  
标签:控件 int dataGridView 滚动条 Width dataGridView1 行数 Columns

 

#region 【 触发检查可见行数和总行数的几种情况 】

private void todaySum_Load(object sender, EventArgs e)
{

loadSQL();  //加载数据到dgv

dgv1CheckCount_Click();  //检查可见行数和总行数

}

private void mainForm_Resize(object sender, EventArgs e)
{
dgv1CheckCount_Click();  //检查可见行数和总行数
}
private void dataGridView1_Resize(object sender, EventArgs e)
{
dgv1CheckCount_Click();  //检查可见行数和总行数
}

private void button1_Click(object sender, EventArgs e)
{
insertSQL_newBlankRow();  //在dgv插入空白行

dgv1CheckCount_Click();  //检查可见行数和总行数
}

#endregion 【 触发检查可见行数和总行数的几种情况 】

#region 【 检查可见行数和总行数,resize dgv  columns width 】

private void dgv1CheckCount_Click()  //检查可见行数和总行数
{
int m = dataGridView1.RowCount;  //总行数
int n = dataGridView1.DisplayedRowCount(true);  //可见行数
if (m > n)  //如果总行数>可见行数
{ reSizeDgv1col_2(); }  //按_2方案调整宽度
else
{ reSizeDgv1col_1(); }  //按_1方案调整宽度
}

void reSizeDgv1col_1()  //设置dgv列宽,未显示垂直滚动条
{
int rowHeaderWidth = 25;  //行标宽度
int fixedColWidth = this.dataGridView1.Columns[1].Width + this.dataGridView1.Columns[3].Width + this.dataGridView1.Columns[5].Width + this.dataGridView1.Columns[6].Width + this.dataGridView1.Columns[7].Width + this.dataGridView1.Columns[8].Width;  //加载SQL数据的固定列宽,已设置固定列宽
int addColWidth = 60 * 1;  //手动添加的行宽,固定宽度
int offsetWidth = 1;  //补偿宽度,根据实际效果调整,此为不显示垂直滚动条的宽度补偿值

dataGridView1.Columns[2].Width = (dataGridView1.Width - rowHeaderWidth - fixedColWidth - addColWidth - offsetWidth) / 2+2;  //不想显示横向滚动条,Columns[2]自适应调整,末尾+2为补偿值,按实际效果调整
dataGridView1.Columns[4].Width = (dataGridView1.Width - rowHeaderWidth - fixedColWidth - addColWidth - offsetWidth) / 2+2;  //不想显示横向滚动条,Columns[4]自适应调整,末尾+2为补偿值,按实际效果调整
}
void reSizeDgv1col_2()  //设置dgv列宽,显示垂直滚动条
{
int rowHeaderWidth = 25;  //行标宽度
int fixedColWidth = this.dataGridView1.Columns[1].Width + this.dataGridView1.Columns[3].Width + this.dataGridView1.Columns[5].Width + this.dataGridView1.Columns[6].Width + this.dataGridView1.Columns[7].Width + this.dataGridView1.Columns[8].Width;  //加载SQL数据的固定列宽,已设置固定列宽
int addColWidth = 60 * 1;  //手动添加的行宽,固定宽度
int offsetWidth = 18;  //补偿宽度,根据实际效果调整,此为显示垂直滚动条的宽度补偿值

dataGridView1.Columns[2].Width = (dataGridView1.Width - rowHeaderWidth - fixedColWidth - addColWidth - offsetWidth) / 2 + 2;  //不想显示横向滚动条,Columns[2]自适应调整,末尾+2为补偿值,按实际效果调整
dataGridView1.Columns[4].Width = (dataGridView1.Width - rowHeaderWidth - fixedColWidth - addColWidth - offsetWidth) / 2 + 2;  //不想显示横向滚动条,Columns[4]自适应调整,末尾+2为补偿值,按实际效果调整

}

#endregion 【 检查可见行数和总行数,resize dgv  columns width 】

标签:控件,int,dataGridView,滚动条,Width,dataGridView1,行数,Columns
From: https://www.cnblogs.com/kaijie/p/16843775.html

相关文章

  • DataGridView控件3——常用事件
    DataGridView的常用事件:1、CellClick,CellContentClick,CellDoubleClick2、CellValueChanged,CurrentCellDirtyStateChanged 1、CellChanged事件和CellContentClick事件......
  • wpf利用ReoGrid控件进行Excel表格展示及导出
    Nuget包安装Nuget包管理器直接搜索ReoGrid进行安装,如图   前端根据官方文档(https://reogrid.net/document/installation/)在前端引入命名空间:xmlns:rg="clr-na......
  • UI界面+treeviwe+dataGridView
    usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Runtime.Interop......
  • winform日历控件
     分享一个漂亮的winform自定义控件,做考勤、日程管理、计划最好的自定义控件了,能够添加备注等等。......
  • DatagridView2——绑定数据源,设置列类型为CheckBox,ComboBox,Button,Link
    DatagridView2实现内容:1、绑定数据源2、手动编辑DatagridView属性后并绑定数据源3、设置列类型为CheckBox4、设置列类型为ComboBox5、设置列类型为Button和Link实现......
  • 【黑客攻防技术宝典】04-客户端控件
    黑客攻防技术宝典——客户端控件阅读范围:《黑客攻防技术宝典:web实战篇》第五章内容5-客户端控件5.1传送用户数据表面隐藏手段隐藏表单字段HTTPcookieURL参数......
  • c#Winform自定义控件-信号灯(工业)-HZHControls UCSignalLamp 使用
     想要官网这个效果,自己琢磨了下,记录一下   一、拖一个UCSignalLamp控件   二、设置指示灯的属性//设置指示灯颜色(不闪烁只用写一个......
  • abap screen表格控件后续增加栏位
    当我们通过screen的表控件(含向导)创建完表格之后,后续想再增加栏位,要先把输入控件拖入表格控件内,再按钮控控件拖进去即可。注:输入控件的名称和格式要和程序中的一致,否则......
  • C# DataGridView Helper
    usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Linq;usingSystem.Reflection;usingSystem.Te......
  • datagridview 显示下拉框选择男女
    先看效果:下面公布代码:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSys......