0
我有一個名爲Result
的類,它有4個字段id, marks, total, avg
。我創建了List類並存儲了結果。在datagrid中顯示列表<t>的問題?
現在我想在數據網格中只顯示2列。他們是id
和total
。我成功地顯示的ID和總,但數據網格顯示4列,而不是2列id, total, id, total
這裏是我的代碼顯示數據網格:
private void Form1_Load(object sender, EventArgs e)
{
List<Result> result = new List<Result>();
PopulateResult();
dgresult.AutoGenerateColumns = false;
dgresult.Items.Clear();
dgresult.ItemsSource = result;
DataGridTextColumn col1 = new DataGridTextColumn();
col1.Header = "Id";
col1.Binding = new Binding("Id");
DataGridTextColumn col2 = new DataGridTextColumn();
col2.Header = "Total";
col2.Binding = new Binding("Total");
dgresult.Columns.Add(col1);
dgresult.Columns.Add(col2);
}
}
class Result
{
int id;
int total;
int marks;
int avg;
public int Id { get { return id; } set { id = value; } }
public int Total { get { return total; } set { total = value; } }
public int Marks { get { return marks; } set { marks = value; } }
public int Avg { get { return avg; } set { avg = value; } }
public Result(int ID, int TOTAL, int MARKS, int AVG)
{
id = ID;
total = TOTAL;
marks = MARKS;
avg = AVG;
}
}
我不明白爲什麼它正在發生喜歡這個。
在此先感謝。
嘗試建立'Columns'之後設置'ItemsSource'。但是看不到如何調用'PopulateResult()'來填充你的'result'列表。 – 2011-06-16 15:54:04
這是一個函數只是將結果添加到列表 – 2011-06-16 15:59:52
沒有辦法,該函數調用填充結果。我猜想有些事情我不理解。 – 2011-06-16 16:46:15