Ich habe Probleme mit DataGridView. Ich habe ein DataSet mit 5 Tabellen, die aus MySQL-Datenbank gefüllt wurden. Ich habe ein TreeView mit 5 Knoten. Auf jedem TreeNode binde ich jede Tabelle an DataGridView, um die Produktliste zu ändern.Kann .NET DataGridView Fehler nicht löschen - Index hat keinen Wert
Also, wenn ich bestehende DataGridView Zeile löschen oder aktualisieren und speichern Sie es SOMETIMES Ich bekomme Fehler unten, aber manchmal funktioniert es wie ein Charme. Was zum Teufel?
System.IndexOutOfRangeException: Index 'x' nicht über Wert bei System.Windows.Forms.CurrencyManager.get_Item (Int32 Index) bei System.Windows.Forms.DataGridViewViewDataConnection.GetError (Int32 RowIndex)
und nach diesem Fehler bekomme ich diesen Fehler:
nicht behandelte Ausnahme in dieser Anwendung aufgetreten ..: Objektverweis nicht auf eine Instanz eines Objekts festgelegt. So
, ich habe 2 Tasten:
1) Taste sparen auf klicken Sie erhalten Änderungen von Datagridview Änderungen und Aktualisierungen Datenbank:
try
{
/// Prisijungimas prie MySQL duombazes
var dbConnection = DBConnection.Instance();
dbConnection.DatabaseName = "amber";
/// Tikrinama, ar prisijungta
if (dbConnection.IsConnect())
{
/// Gaunami pakeitimai iš DataGridView
DataTable changes = ((DataTable)itemsDataGridView.DataSource).GetChanges();
if (changes != null)
{
DataTable columnsSchema = GetColumnNamesByTable(menuTreeView.SelectedNode.Tag.ToString());
/// Surašomi originalūs stulpelių pavadinimai
/// iš MySQL duombazės lentelės
int i = 0;
foreach (DataRow row in columnsSchema.Rows)
changes.Columns[i++].ColumnName = row.Field<String>("ColumnName");
/// Atliekami paketiimai MySQL duombazėje
if (itemsDataGridView.RowCount > 0 && CheckIfAllRowValuesExist(changes))
{
using (MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter("SELECT * FROM " + menuTreeView.SelectedNode.Tag.ToString(), dbConnection.Connection))
{
MySqlCommandBuilder mcb = new MySqlCommandBuilder(mySqlDataAdapter);
mySqlDataAdapter.UpdateCommand = mcb.GetUpdateCommand();
int affectedRows = mySqlDataAdapter.Update(changes);
((DataTable)itemsDataGridView.DataSource).AcceptChanges();
/// Jei yra paveiktų eilučių, tai sėkmingai atnaujinta
if (affectedRows > 0)
{
MessageBox.Show("Sėkmingai atnaujinta", "Pranešimas",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
else
{
MessageBox.Show("Prašome užpildyti visus laukus");
}
}
else
MessageBox.Show("Nebuvo ką atnaujinti", "Pranešimas",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
loadButton.PerformClick();
}
2) Last Button: es alle 5 Tische bekommt aus Datenbank:
loadButton.Enabled = false;
/// Jei BackGroundWorker neužsiėmės pradedame darbą
if (getItemsBackgroundWorker.IsBusy == false)
getItemsBackgroundWorker.RunWorkerAsync();
/// Keičiame statusą
statusLabel.Text = "Gaunama";
3)
private void getItemsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
GetKidsOrAdultsOrPetsAllRecords();
}
4)
public void GetKidsOrAdultsOrPetsAllRecords()
{
/// Jungiames prie MySQL duombazės
var dbConnection = DBConnection.Instance();
dbConnection.DatabaseName = "amber";
try
{
if (dbConnection.IsConnect())
{
string query = null;
query = "SELECT code as 'Kodas', quantity as 'Kiekis', color as 'Spalva', length as 'Ilgis', clasp as 'Užsegimas', thread as 'Siūlas', description as 'Aprašymas' FROM kids;" +
"SELECT code as 'Kodas', quantity as 'Kiekis', color as 'Spalva', length as 'Ilgis', clasp as 'Užsegimas', thread as 'Siūlas', description as 'Aprašymas' FROM adults;" +
"SELECT code as 'Kodas', quantity as 'Kiekis', color as 'Spalva', length as 'Ilgis', clasp as 'Užsegimas' FROM pets;" +
"SELECT code as 'Kodas', quantity as 'Kiekis', color as 'Spalva', image as 'Nuotrauka' FROM jewelry;" +
"SELECT code as 'Kodas', title as 'Pavadinimas', size as 'Dydis', weight as 'Svoris' FROM spareParts";
using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, dbConnection.Connection))
{
dataSet.Clear();
adapter.Fill(dataSet);
if (dataSet.Tables.Count > 0)
{
/// Keičiame apatinį statusą
statusLabel.Text = "Visi įrašai gauti";
}
}
BeginInvoke((MethodInvoker)delegate
{
menuTreeView.Enabled = true;
});
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
statusLabel.Text = "Nepavyko gauti įrašų iš MySQL bazės";
}
finally
{
dbConnection.Close();
}
5) Und das ist, wie ich Elemente zu binden Datagridview:
private void menuTreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
/// Tikriname, iš kurios MySQL lentelės imti duomenis
/// pagal TreeNode tag'ą
if (e.Node.Parent != null && (e.Node.Parent.Name == "wareHouseRoot"))
{
saveButton.Visible = true;
deleteButton.Visible = true;
addButton.Visible = true;
table = e.Node.Tag.ToString();
}
else
{
saveButton.Visible = false;
deleteButton.Visible = false;
addButton.Visible = false;
}
/// Padarome rezultatų GroupBox'ą nematomu
foreach (Control control in Controls)
{
if (control is GroupBox && (control.Name.ToString() != "searchGroupBox"))
control.Visible = false;
}
itemsDataGridView.DataSource = null;
/// Tikriname TreeNode pavadinimus ir
/// nustatome GroupBox'ams pavadinimus, matomumą
/// ir siunčiame užklausą gauti įrašams
switch (e.Node.Name)
{
case ("kidsNode"):
this.recordsGroupBox.Text = "Vaikų prekės";
this.recordsGroupBox.Visible = true;
itemsDataGridView.DataSource = dataSet.Tables[0];
break;
case ("adultsNode"):
this.recordsGroupBox.Text = "Suaugusiųjų prekės";
this.recordsGroupBox.Visible = true;
itemsDataGridView.DataSource = dataSet.Tables[1];
break;
case ("petsNode"):
this.recordsGroupBox.Text = "Gyvūnų prekės";
this.recordsGroupBox.Visible = true;
itemsDataGridView.DataSource = dataSet.Tables[2];
break;
case ("jewelryNode"):
this.recordsGroupBox.Text = "Juvelyrika";
this.recordsGroupBox.Visible = true;
itemsDataGridView.DataSource = dataSet.Tables[3];
itemsDataGridView.Columns[3].Visible = false;
jewelryGroupBox.Visible = true;
break;
case ("sparePartsNode"):
this.recordsGroupBox.Text = "Detalės";
this.recordsGroupBox.Visible = true;
this.itemsDataGridView.DataSource = dataSet.Tables[4];
break;
case ("amberRoot"):
this.controlPanelGroupBox.Visible = true;
break;
}
}
Ich bin auf diesem Fehler fest und kann nicht Lösung finden. Ich habe alle Lösungen ausprobiert, die ich in Google, Stackoverflow und anderen Foren gefunden habe, aber immer noch nicht los.
es ist Threading Problem, denke ich. Datenquelle wird vom Arbeitsthread geändert, Datenquelle ist an dgv gebunden, dgv versucht im UI-Thread zu aktualisieren, wenn sie nicht mehr synchron sind (was mehr oder weniger zufällig ist) ... boom, es gibt eine Ausnahme – ASh
Kann ich MySQL-Datensätze auffüllen (etwa 7000) ohne BackgroundWorker? Anwendung scheint nicht ohne BackgroundWorker zu verzögern. – Mantoshelis