Add New Row in DataGridView Programmatically
Submitted by admin on Thursday, April 25, 2013 - 17:44.
In some cases, you want to add new rows to the DataGridView programmatically. If you are using a data-bound DataGridView control, you cannot easily use the cells properties of the DataGridView control.
All you need to do is use the dataset where the DataGridView control is connected. Say you want to get the record from employees table and put it in DataGridView.
Here’s how to do it:
Download the VB.NET Project on Adding Rows to Data-Bound DataGridView.
- Dim strSQL As String
- strSQL = "SELECT * FROM Employees"
- Dim cmd As OleDbCommand = New OleDbCommand(strSQL, conn)
- 'create data reader
- Dim rdr As OleDbDataReader = cmd.ExecuteReader
- While rdr.Read
- Dim newRow As PayrollDataSet.PayrollRow = PayrollDataSet.Payroll.NewPayrollRow
- newRow.EmployeeID = rdr("EmployeeID")
- newRow.MonthlySalary = rdr("MonthlySalary")
- newRow.SSSCont = rdr("SSSCont")
- newRow.SalaryLoan = rdr("SalaryLoan")
- newRow.NetIncome = rdr("MonthlySalary") - rdr("SSSCont") - rdr("SalaryLoan")
- PayrollDataSet.Payroll.Rows.Add(newRow)
- End While
Add new comment
- 145 views