2013-09-23 41 views
-1

我有一些數據存在於某個文本框(例如10)上的一個winform。現在我想要的是顯示錶格中文本框中所有信息的摘要。 我曾嘗試使用datagrid視圖,但我不知道如何向它添加行。 我看過各種答案,但他們都沒有解決我的問題。 表的格式是這樣的:如何在使用datagrid視圖創建的表中添加行

TYPE    DESCRIPTION 
row1 
row2 

任何幫助,將不勝感激。

+0

你不能在數據綁定的DataGridView添加行.. – matzone

+0

那麼能否請您提出好的建議,以解決我的問題。 – Kratos

回答

0

如果您手動將數據輸入到DataGridView中,則可以從TextBoxes中獲取所有文本並在下面執行操作。

首先添加列

 DataGridView.Columns.Add(columnName, headerText); 

然後針對每個數據集創建一行。

 var newRow = new DataGridViewRow(); 
     newRow.CreateCells(DataGridView); 
     newRow.SetValues(valueArrayForRow); 
     DataGridView.Rows.Add(newRow); 
0

創建一個具有屬性的類表示您在表單中的字段。

假設您已創建類Student

  1. 創建列表students = new List();

  2. 現在,如果你想在 的datagridview顯示在文本框中的值,第一填充值的學生對象。(設置 值,以各自的財產使用文本框的值)

  3. 添加填充學生對象創建的Students列表。 students.Add(studentObject);

  4. Students列表設置爲DataGridView的數據源。 dataGridView1.DataSource = student;

編輯

如果要空行或新行添加到DataGrid查看,更新綁定Students列表,然後reset the datasource

//Assume Student list is bound as Dtaasource 
List<Student> students = new List<Student>(); 

//Add a new student object to the list 
students .Add(new Student()); 

//Reset the Datasource 
dataGridView1.DataSource = null; 
dataGridView1.DataSource = students; 
0
//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 you 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... 
grv.datasource=dbtable; 
grv.databind(); 
相關問題