유용한 정보

[C#] DataGridView 에 수직스크롤바가 있는지의 여부 판단하기

DevReff 2025. 4. 16. 05:15
728x90
728x90
SMALL

1. 소스

/// <summary>
/// 주어진 DataGridView 컨트롤에 수직스크롤바가 있는지의 여부를 구한다.
/// </summary>
/// <param name="dgv"></param>
/// <returns>
/// - true : 수직스크롤바가 있다.
/// - false : 수직스크롤바가 없다.
/// </returns>
bool ExistsVScroll(DataGridView dgv)
{
try
{
int height;
if (dgv.Rows.Count > 0)
height = dgv.Rows[0].Height;
else
height = dgv.ColumnHeadersHeight;

return height * dgv.Rows.Count >= (dgv.Height - dgv.ColumnHeadersHeight);
}
catch (Exception ex)
{
KLog.Write(ex.ToString());
}

return false;
}

반응형

2. 사용예제

/// <summary>
/// 리스트의 칼럼의 크기를 조정한다.
/// </summary>
void ResizeColumn()
{
try
{
int width = PN_T.Width - (40 + 60 + 100 + 180);
int c = -1;

if (KssUtils.FuncsOfCtrl.ExistsVScroll(DGV_LIST))
width -= SystemInformation.VerticalScrollBarWidth;

DGV_LIST.Columns[++c].Width = 40;
DGV_LIST.Columns[++c].Width = 60;
DGV_LIST.Columns[++c].Width = 100;
DGV_LIST.Columns[++c].Width = width;
DGV_LIST.Columns[++c].Width = 180;
}
catch (Exception ex)
{
KLog.Write(ex.ToString());
}
}

728x90
728x90
LIST