■■■

2016年3月31日木曜日

SortCompareを利用してDataGridViewをソートする方法

SortCompareを利用してDataGridViewをソートする方法
VB.NET
Private Sub dgv1_SortCompare(ByVal sender As Object, _
ByVal e As DataGridViewSortCompareEventArgs) _
Handles dgv1.SortCompare
Dim strTxt1 As String
If
e.CellValue1 Is Nothing Then
strTxt1 = ""
Else
strTxt1 = e.CellValue1.ToString
End If
Dim
strTxt2 As String
If
e.CellValue2 Is Nothing Then
strTxt2 = ""
Else
strTxt2 = e.CellValue2.ToString
End If

e.SortResult =
strTxt1.Length - strTxt2.Length
e.Handled = True
End Sub
C#
private void dgv1_SortCompare(object sender,
DataGridViewSortCompareEventArgs e)
{
string
strTxt1 = (e.CellValue1 == null ? "" : e.CellValue1.ToString());
string strTxt2 = (e.CellValue2 == null ? "" : e.CellValue2.ToString());

e.SortResult =
strTxt1.Length - strTxt2.Length;
e.Handled = true;
}



■■■