| mono-project.de · Artikel · Wiki |
In Zusammenarbeit mit ![]() |
Kontakt · Datenschutz · Impressum |
|
|||||||
| Registrieren | Hilfe | Benutzerliste | Interessengemeinschaften | RSS Feeds | Kalender | Suchen | Heutige Beiträge | Alle Foren als gelesen markieren |
| Code Snippets und User Projekte Hier können eigene Codefragmente, Projekte sowie Design- und Entwurfsmuster dieser diskutiert werden. |
![]() |
|
|
Themen-Optionen | Ansicht |
|
#1
|
|||
|
|||
|
Hi,
ich habe eine xml in ein DataGridView geladen und die erste spalte soll als ID Feld dienen. über ds.Tables[0].Columns[0].DefaultValue versuch ich ihn die aktuelle Zeile zu übergeben. Aber irgendwie funktioniert da nichts, hat jemand eine idee ? |
|
#2
|
||||
|
||||
|
Ohne deinen entsprechenden Code kann dir so keiner helfen.
|
|
#3
|
|||
|
|||
|
Hab es schon gelöst.
Es ging darum, ein XML ohne schema oder den klassischen XML-DTD schema ein ID feld zu geben. Wer die lösung gebrauchen: XML - Beispiel: Code:
<?xml version="1.0" standalone="yes"?>
<Client>
<allow>
<id>1</id>
<ip>127.0.0.1</ip>
</allow>
<allow>
<id>2</id>
<ip>xx.xxx.xxx.xxx</ip>
</allow>
</Client>
Code:
// XML lesen und ans DataGridView1 übergeben
DataSet ds = new DataSet();
ds.ReadXml("pfad/deine.xml",XmlReadMode.InferSchema);
// Hier alle Zeilen zählen
int totalRecords = ds.Tables[0].Rows.Count;
this.dataGridView1.DataSource = ds;
//xml node child "allow" von der Beispiel XML
this.dataGridView1.DataMember = "allow";
// ID aufsteigen sortieren
this.dataGridView1.Sort (this.dataGridView1.Columns[0], ListSortDirection.Ascending);
//ID neu anlegen um doppelten ID eintrag zu verhindern
for (int i=0;i<= totalRecords ;i++)
{
this.dataGridView1.Rows[i].Cells[0].Value = i;
log_msg += "Update client:> ID " + i + " successful\n";
}
// Colomns[0] ist das ID-Feld.
// Neuer datensatz bekommt bekommt Zeilen nummer als ID
ds.Tables[0].Columns[0].DefaultValue = totalRecords++;
//ID nur kann nur gelesen werden
this.dataGridView1.Columns[0].ReadOnly = true;
//Breite für ID-Feld zuweisen
this.dataGridView1.Columns[0].Width = 25;
//Nächste Spalte breite zuweisen
this.dataGridView1.Columns[1].Width = 90;
//Die ID darf nur einmal vorkommen
ds.Tables[0].Columns[0].Unique = true;
this.dataGridView1.Update();
ID werden neu vergeben. Code:
void DataGridView1CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if(e.RowIndex > -1)
{
for (int i=0;i<= e.RowIndex;i++)
{
this.dataGridView1.Rows[i].Cells[0].Value = i;
}
this.dataGridView1.Update();
}
}
Ihr braucht ein Button (BtnWriteXmlClick) Zitat:
Geändert von paranoid64 (12.11.2008 um 19:07:14 Uhr) |
|
#4
|
|||
|
|||
|
Wenn man die ID ab- / aufsteigend sortiert hat, werden die ID falsch angelegt.
Deshalb eine besser lösung: Code:
void DataGridView1CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if(e.RowIndex > -1)
{
for (int i=0;i<= e.RowIndex;i++)
{
this.dataGridView1.Rows[i].Cells[0].Value = i;
}
this.dataGridView1.Update();
}
}
Code:
private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
if( e.Row.Index > -1)
{
DataSet ds = (DataSet) dataGridView1.DataSource;
this.NewRowID = ds.Tables[0].Rows.Count;
this.OldRowID = 0;
for (int i=0;i<=this.NewRowID;i++)
{
if (this.dataGridView1.Rows[i].Cells[0].Value.ToString() == this.OldRowID.ToString())
{
this.OldRowID = i;
this.OldRowID = this.OldRowID +1;
i = 0;
if (this.dataGridView1.Rows[0].Cells[0].Value.ToString() == this.OldRowID.ToString())
{
this.OldRowID = i;
this.OldRowID = this.OldRowID +1;
i = 0;
}
ds.Tables[0].Columns[0].DefaultValue = this.OldRowID;
}
else
{
ds.Tables[0].Columns[0].DefaultValue = this.OldRowID;
}
}
this.dataGridView1.Update();
this.dataGridView1.Refresh();
}
}
Code:
//ID neu anlegen um doppelten ID eintrag zu verhindern
for (int i=0;i<= totalRecords ;i++)
{
this.dataGridView1.Rows[i].Cells[0].Value = i;
log_msg += "Update client:> ID " + i + " successful\n";
}
Code:
for (int i=0;i<=this.NewRowID;i++)
{
if (i < NewRowID )
{
if (this.dataGridView1.Rows[i].Cells[0].Value.ToString() == this.OldRowID.ToString())
{
this.OldRowID = i;
this.OldRowID = this.OldRowID +1;
i = 0;
if (this.dataGridView1.Rows[0].Cells[0].Value.ToString() == this.OldRowID.ToString())
{
this.OldRowID = i;
this.OldRowID = this.OldRowID +1;
i = 0;
}
ds.Tables[0].Columns[0].DefaultValue = this.OldRowID;
}
else
{
MessageBox.Show ( this.OldRowID.ToString() );
ds.Tables[0].Columns[0].DefaultValue = this.OldRowID;
}
}
}
Geändert von paranoid64 (15.11.2008 um 14:16:38 Uhr) |
|
#5
|
|||
|
|||
|
|
![]() |
| Lesezeichen |
| Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1) | |
| Themen-Optionen | |
| Ansicht | |
|
|