我似乎无法解决这个问题。 参见下面的代码,当在运行的程序中向datagridview添加第二行时,我得到下面引发的异常;
exception datagridview:System.IndexOutOfRangeException:System.Windows.Forms.CurrencyManager.get_Item(Int32index)bij System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowindex)上没有索引1的值。
已经在谷歌上寻找解决方案,但似乎找不到合适的解决方案。 有人知道如何解决这个问题吗?
(已经编写了其他类似的程序,但在数据集中有更多的数据表,但我没有遇到这个问题,所以我真的不明白为什么它不起作用,而且我最近才开始学习代码:))
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Garages
{
public partial class FrmGarages : Form
{
DataSet datasetGarages = new DataSet("Garages");
BindingSource bindingSourceGarages = new BindingSource();
public FrmGarages()
{
InitializeComponent();
datasetGarages.Tables.Add(createDataTableGarages());
bindingSourceGarages.DataSource = datasetGarages.Tables["Garages"];
txbChassisnummer.DataBindings.Add("Text", bindingSourceGarages, "Chassisnummer");
txbKenteken.DataBindings.Add("Text", bindingSourceGarages, "Kenteken");
dtpFabricagedatum.DataBindings.Add("Value", bindingSourceGarages, "Fabricagedatum");
dtpDatumOpKenteken.DataBindings.Add("Value", bindingSourceGarages, "DatumOpKenteken");
txbMerk.DataBindings.Add("Text", bindingSourceGarages, "Merk");
txbType.DataBindings.Add("Text", bindingSourceGarages, "Type");
txbKleur.DataBindings.Add("Text", bindingSourceGarages, "Kleur");
txbBrandstof.DataBindings.Add("Text", bindingSourceGarages, "Brandstof");
txbAantalKilowats.DataBindings.Add("Text", bindingSourceGarages, "AantalKilowats");
txbAantalPortieren.DataBindings.Add("Text", bindingSourceGarages, "AantalPortieren");
txbModel.DataBindings.Add("Text", bindingSourceGarages, "Model");
txbTrekhaak.DataBindings.Add("Text", bindingSourceGarages, "Trekhaak");
cmbMerk.DataSource = bindingSourceGarages;
cmbMerk.DisplayMember = "Merk";
cmbType.DataSource = bindingSourceGarages;
cmbType.DisplayMember = "Type";
cmbBrandstof.DataSource = bindingSourceGarages;
cmbBrandstof.DisplayMember = "Brandstof";
cmbAantalPortieren.DataSource = bindingSourceGarages;
cmbAantalPortieren.DisplayMember = "AantalPortieren";
dgvGarages.DataSource = bindingSourceGarages;
}
private void btnEinde_Click(object sender, EventArgs e)
{
this.Close();
}
private DataTable createDataTableGarages()
{
DataTable dtGarages = new DataTable("Garages");
DataColumn Chassisnummer = new DataColumn("Chassisnummer", typeof(string));
Chassisnummer.Unique = true;
dtGarages.Columns.Add(Chassisnummer);
//dtGarages.Columns.Add("Chassisnummer", typeof(string));
//dtGarages.Columns["Chassisnummer"].Unique = true;
dtGarages.Columns.Add("Kenteken", typeof(string));
dtGarages.Columns["Kenteken"].MaxLength = 8;
dtGarages.Columns["Kenteken"].Unique = true;
dtGarages.Columns["Kenteken"].AllowDBNull = true;
dtGarages.Columns.Add("FabricageDatum", typeof(DateTime));
dtGarages.Columns.Add("DatumOpKenteken", typeof(DateTime));
dtGarages.Columns["DatumOpKenteken"].AllowDBNull = true;
dtGarages.Columns.Add("Merk", typeof(string));
dtGarages.Columns["Merk"].MaxLength = 25;
dtGarages.Columns.Add("Type", typeof(string));
dtGarages.Columns["Type"].MaxLength = 25;
dtGarages.Columns.Add("Kleur", typeof(Color));
dtGarages.Columns.Add("Brandstof", typeof(string));
dtGarages.Columns["Brandstof"].MaxLength = 10;
dtGarages.Columns.Add("AantalKilowats", typeof(double));
dtGarages.Columns.Add("AantalPortieren", typeof(byte));
dtGarages.Columns.Add("Model", typeof(string));
dtGarages.Columns["Model"].MaxLength = 15;
dtGarages.Columns.Add("Trekhaak", typeof(bool));
dtGarages.PrimaryKey = new DataColumn[] { Chassisnummer };
return dtGarages;
}
private void dgvGarages_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (this.dgvGarages.Columns[e.ColumnIndex].Name.Equals("Kleur"))
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
this.dgvGarages.Rows[e.RowIndex].Cells[6].Value = colorDialog1.Color;
}
}
}
private void dgvGarages_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
dgvGarages.Refresh();
}
}
}