2015-02-10 27 views
7

Başlığı belirttiğim gibi, C# kullanarak programlı olarak DataGrid'e satır eklemeye çalışıyorum ama çalışmayı başaramıyorum. Şimdiye kadar sahip olduğum şey bu. C# içinde DataGrid'e program aracılığıyla satırlar nasıl eklenir?

// 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++; 
    } 
} 

Etrafa googling oldum ve bir şey eklemeden lazım en yakın {column = value} kullanmaktı ama sadece bana bir hata atar. Gerçekten fikirler dışında şimdi olsa:

+0

iyi yolu DataGridView DataSource (Datatable, veri kümesi vs) ile bağlı kullanmak ve veri kaynağı kendisinde değişiklik yapma. Ayrıca kodunuzda, datagridview'ınızda UI'de görünmesi için ** dgRow ** 'ı eklemelisiniz. –

+0

Bir DataTable'a tam olarak nasıl "bağlarım?" – Dilisqq

cevap

5

deneyin Burada Durumunda 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; 

bir kaynak bağlanarak bunu daha fazlasını yapabilir yolu var sen WPF kullanarak vardır Bu

dgv.DataContext = employeeData.DefaultView; 

ve

yılında yol-benzeri DataGrid bunu bağlayabilir

XAML

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

Merhaba Rohit! Bunu DataGrid ile çalışmak için herhangi bir yolu var mı? DataGridView C# (?) – Dilisqq

+0

@Dilisqq; Bu DataGrid için de çalışacaktır. –

+0

Merhaba, Rohit, DataGridView'ın WPF'de bulunmadığını fark ettim. WPF kullanıyorum. Bu işi WPF ile yapmanın bir yolu var mı? – Dilisqq

-1

yaptığımız \ Eğer ?:

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(); 

Selamlar

İlgili konular