2015-02-10 12 views
7

Così come dice il titolo, sto cercando di aggiungere righe a un DataGrid a livello di codice usando C# ma non riesco a farlo funzionare. Questo è quello che ho finora.Come aggiungere a livello di codice le righe a DataGrid in C#?

// I have a DataGrid declared as dg in the XAML 

foreach (string s in array) { 
    int index = 0; 
    DataGridRow dgRow = new DataGridRow(); 
    foreach (DataGridColumn dgColumn in columns) 
    { 
     // Trying to add Text to dgRow here but IDK how 
     index++; 
    } 
} 

Sono stato Googling intorno e il più vicino ho avuto modo di aggiungere nulla è stato quello di utilizzare {column = value} ma appena mi tiri un errore. Davvero di idee ora però: \

+0

modo migliore per utilizzare DataGridView per legato con un DataSource (Datatable, set di dati, ecc) e fare cambiamenti nella origine dati stessa. Inoltre nel codice, devi aggiungere ** dgRow ** nella tua vista dati per renderlo visibile nell'interfaccia utente. –

+0

Come si "associa" a un DataTable esattamente? – Dilisqq

risposta

5

Ecco si può fare meglio legandosi una fonte di datagridview

 // Creating DataSource here as datatable having two columns 
     DataTable dt = new DataTable(); 
     dt.Columns.Add("ID", typeof(int)); 
     dt.Columns.Add("Name"); 

     // Adding the rows in datatable 
     for (int iCount = 1; iCount < 6; iCount++) 
     { 
      var row = dt.NewRow(); 
      row["ID"] = iCount; 
      row["Name"] = "Name " + iCount; 
      dt.Rows.AddRow(row); 
     } 

     DataGridView dgv = new DataGridView(); 
     // Set AutoGenerateColumns true to generate columns as per datasource. 
     dgv.AutoGenerateColumns = true; 
     // Finally bind the datasource to datagridview. 
     dgv.DataSource = dt; 

Nel caso si utilizzi WPF DataGrid si può legare come questo modo-

dgv.DataContext = employeeData.DefaultView; 

e in

XAML

<DataGrid Name="dgv" ItemsSource="{Binding}"> 
+0

Ciao Rohit! C'è un modo per farlo funzionare con 'DataGrid'? DataGridView non sembra essere disponibile nella mia build di C# (?) – Dilisqq

+0

@Dilisqq; Funzionerà anche per DataGrid. –

+0

Ciao, Rohit, ho appena realizzato che DataGridView non è disponibile in WPF. Sto usando WPF. C'è un modo per farlo funzionare con WPF? – Dilisqq

-1

Hai provato ?:

int n=5; // number of rows you want to add 
dataGridView1.Rows.Add(n); 

// you can add (names of the rows) if you have them in your array 
//for(int i=0; i<n; i++) 
//dataGridView1[0, i].Value = array[i]; 
2
//create datatable and columns, 
DataTable dtable = new DataTable(); 
dtable.Columns.Add(new DataColumn("Column 1")); 
dtable.Columns.Add(new DataColumn("Column 2")); 

//simple way create object for rowvalues here i have given only 2 add as per your requirement 
object[] RowValues = { "", "" }; 

//assign values into row object 
RowValues[0] = "your value 1"; 
RowValues[1] = "your value 2"; 

//create new data row 
DataRow dRow; 
dRow = dtable.Rows.Add(RowValues); 
dtable.AcceptChanges(); 

//now bind datatable to gridview... 
gridview.datasource=dbtable; 
gridview.databind(); 

saluti

Problemi correlati