Hello Community,
How to add a blank row to my datagridview when I click the button btnAdd? My project below is using an Entity Data Model. I've tried datagridview.Rows.Add(); but result with the following error... "Additional information: Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound".
I am new with C# and any help would be greatly appreciated. Thank you!
public partial class frmMain : Form { RTCTrialEntities db; public string currenttable { get; set; } public int currentindex { get; set; } public string rtcpntext { get; set; } public string descriptiontext { get; set; } public string keyvaluetext { get; set; } string SelectedTable; int MaxRows = 0; int Inc = 0; bool IndexStatus = true; public frmMain() { InitializeComponent(); } private void Main_Load(object sender, EventArgs e) { // Creates new instance of the database db = new RTCTrialEntities(); RecordUpdates(); } private void cbxTables_SelectedIndexChanged(object sender, EventArgs e) { // Selected table text from combobox SelectedTable = cbxTables.Text; // Populate datagridview TableSwitch(); } private void TableSwitch() { // Select table to view in the datagrid switch (SelectedTable) { case "Resistors": { dataGridView1.DataSource = db.Resistors.ToList(); break; } case "Capacitors": { dataGridView1.DataSource = db.Capacitors.ToList(); break; } case "Symbols": { dataGridView1.DataSource = db.Symbols.ToList(); break; } default: { break; } } // Record index status if (IndexStatus == true) { Inc = 0; } else { Inc = this.currentindex; } // Navigates record position NavigateRecords(); // Enable navigation buttons btnNext.Enabled = true; btnPrevious.Enabled = true; btnFirst.Enabled = true; btnLast.Enabled = true; } private void NavigateRecords() { // Clear selected record and select new record dataGridView1.ClearSelection(); dataGridView1.Rows[Inc].Selected = true; //Counts max records MaxRows = dataGridView1.RowCount; // Update record label lblCount.Text = "Record " + (Inc + 1) + " of" + MaxRows; } public void RecordUpdates() { // Current Index IndexStatus = false; // Sets current selected table cbxTables.Text = this.currenttable; // Sets values to datagridview dataGridView1.SelectedCells[1].Value = this.rtcpntext; dataGridView1.SelectedCells[2].Value = this.descriptiontext; dataGridView1.SelectedCells[3].Value = this.keyvaluetext; // Saves updates to database db.SaveChanges(); // Reset index IndexStatus = true; } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { // Updates record position upon cell click Inc = dataGridView1.CurrentCell.RowIndex; NavigateRecords(); } private void btnNext_Click(object sender, EventArgs e) { // Increment records if (Inc != MaxRows - 1) { Inc++; } else { if (Inc != 0) { Inc = 0; } } NavigateRecords(); } private void btnPrevious_Click(object sender, EventArgs e) { // Decrement records if (Inc > 0) { Inc--; } else { if (Inc != MaxRows - 1) { Inc = MaxRows - 1; } } NavigateRecords(); } private void btnFirst_Click(object sender, EventArgs e) { // Jump to first record if (Inc != 0) { Inc = 0; } NavigateRecords(); } private void btnLast_Click(object sender, EventArgs e) { // Jump to last record if (Inc != MaxRows - 1) { Inc = MaxRows - 1; } NavigateRecords(); } private void btnClose_Click(object sender, EventArgs e) { // Close Application this.Close(); Application.Exit(); } private void btnEdit_Click(object sender, EventArgs e) { // Creates new instance of Edit form frmEdit EditForm = new frmEdit(); // Send variables to Edit form EditForm.EditDB = db; EditForm.EditDGV = dataGridView1; EditForm.EditSelectedTable = SelectedTable; EditForm.RefToMainForm = this; EditForm.EditInc = Inc; // Hides form this.Visible = false; // Displays Edit form EditForm.ShowDialog(); } private void btnAdd_Click(object sender, EventArgs e) { } }