Hi kana250688,
Please refer the below code.
Code
C#
public class Form1
{
private List<OrderDetail> OrderDetail;
private BindingSource bindingSource = null/* TODO Change to default(_) if this is not a reference type */;
private SortableBindingList<OrderDetail> _criteriasBindingList = new SortableBindingList<OrderDetail>();
public Form1()
{
InitializeComponent();
this.CenterToScreen();
DataGridViewCheckBoxColumn CheckedBoxColumn = new DataGridViewCheckBoxColumn();
CheckedBoxColumn.Width = 40;
CheckedBoxColumn.Name = "checkboxcolumn";
CheckedBoxColumn.HeaderText = "Check";
CheckedBoxColumn.ReadOnly = false;
DataGridView1.Columns.Insert(0, CheckedBoxColumn);
}
private void Form1_Load(object sender, EventArgs e)
{
this.LoadData();
}
private void LoadData()
{
OrderDetail = new List<OrderDetail>()
{
new OrderDetail()
{
Invono = "PI0001",
InvoDate = Convert.ToDateTime("06-05-2024"),
Days = Convert.ToDateTime("06-05-2024").ToString("dddd"),
ProductName = "TEST 1000",
UnitPrice = 15000,
Quantity = 20
},
new OrderDetail()
{
Invono = "PI0002",
InvoDate = Convert.ToDateTime("07-05-2024"),
Days = Convert.ToDateTime("07-05-2024").ToString("dddd"),
ProductName = "TEST 2000",
UnitPrice = 25000,
Quantity = 20
},
new OrderDetail()
{
Invono = "PI0003",
InvoDate = Convert.ToDateTime("06-05-2024"),
Days = Convert.ToDateTime("06-05-2024").ToString("dddd"),
ProductName = "TEST 3000",
UnitPrice = 17000,
Quantity = 20
},
new OrderDetail()
{
Invono = "PI0004",
InvoDate = Convert.ToDateTime("07-05-2024"),
Days = Convert.ToDateTime("07-05-2024").ToString("dddd"),
ProductName = "TEST 4000",
UnitPrice = 18000,
Quantity = 20
}
};
_criteriasBindingList = new SortableBindingList<OrderDetail>(OrderDetail);
bindingSource = new BindingSource() { DataSource = _criteriasBindingList };
DataGridView1.DataSource = bindingSource;
DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}
private void Btncheckforcheckboxdgvunchec_Click(object sender, EventArgs e)
{
bool isChecked = false;
foreach (DataGridViewRow row in DataGridView1.Rows)
{
DataGridViewCheckBoxCell cell = row.Cells("checkboxcolumn") as DataGridViewCheckBoxCell;
if (cell.Value)
{
isChecked = true;
break;
}
}
if (!isChecked)
MessageBox.Show("checkboxcolumn must be checked");
}
}
public class OrderDetail
{
public string Invono { get; set; }
public DateTime InvoDate { get; set; }
public string Days { get; set; }
public string ProductName { get; set; }
public int UnitPrice { get; set; }
public int Quantity { get; set; }
}
VB.Net
Public Class Form1
Private OrderDetail As List(Of OrderDetail)
Private bindingSource As BindingSource = Nothing
Private _criteriasBindingList As New SortableBindingList(Of OrderDetail)()
Public Sub New()
InitializeComponent()
Me.CenterToScreen()
Dim CheckedBoxColumn As New DataGridViewCheckBoxColumn
CheckedBoxColumn.Width = 40
CheckedBoxColumn.Name = "checkboxcolumn"
CheckedBoxColumn.HeaderText = "Check"
CheckedBoxColumn.ReadOnly = False
DataGridView1.Columns.Insert(0, CheckedBoxColumn)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.LoadData()
End Sub
Private Sub LoadData()
OrderDetail = New List(Of OrderDetail) From {
New OrderDetail() With {
.Invono = "PI0001",
.InvoDate = Convert.ToDateTime("06-05-2024"),
.Days = Convert.ToDateTime("06-05-2024").ToString("dddd"),
.ProductName = "TEST 1000",
.UnitPrice = 15000,
.Quantity = 20
},
New OrderDetail() With {
.Invono = "PI0002",
.InvoDate = Convert.ToDateTime("07-05-2024"),
.Days = Convert.ToDateTime("07-05-2024").ToString("dddd"),
.ProductName = "TEST 2000",
.UnitPrice = 25000,
.Quantity = 20
},
New OrderDetail() With {
.Invono = "PI0003",
.InvoDate = Convert.ToDateTime("06-05-2024"),
.Days = Convert.ToDateTime("06-05-2024").ToString("dddd"),
.ProductName = "TEST 3000",
.UnitPrice = 17000,
.Quantity = 20
},
New OrderDetail() With {
.Invono = "PI0004",
.InvoDate = Convert.ToDateTime("07-05-2024"),
.Days = Convert.ToDateTime("07-05-2024").ToString("dddd"),
.ProductName = "TEST 4000",
.UnitPrice = 18000,
.Quantity = 20
}
}
_criteriasBindingList = New SortableBindingList(Of OrderDetail)(OrderDetail)
bindingSource = New BindingSource With {.DataSource = _criteriasBindingList}
DataGridView1.DataSource = bindingSource
DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
End Sub
Private Sub Btncheckforcheckboxdgvunchec_Click(sender As Object, e As EventArgs) Handles Btncheckforcheckboxdgvunchec.Click
Dim isChecked As Boolean = False
For Each row As DataGridViewRow In DataGridView1.Rows
Dim cell As DataGridViewCheckBoxCell = TryCast(row.Cells("checkboxcolumn"), DataGridViewCheckBoxCell)
If cell.Value Then
isChecked = True
Exit For
End If
Next row
If Not isChecked Then
MessageBox.Show("checkboxcolumn must be checked")
End If
End Sub
End Class
Public Class OrderDetail
Public Property Invono As String
Public Property InvoDate As DateTime
Public Property Days As String
Public Property ProductName As String
Public Property UnitPrice As Integer
Public Property Quantity As Integer
End Class
Screenshot