[英]Datagridview wont sort on column click
我使用winforms的代碼是:
ctxTripStops = new DatabaseContextx();
ctxTripStops.TripStops.Where(p => p.TripId == id).Load();
bsTripStops.DataSource = ctxTripStops.TripStops.Local;
dgvSecondary.DataSource = bsTripStops;
我還有綁定到Datagridview的bindingnavigator。 一切正常,在網格中添加,刪除和編輯都會更新SaveChanges()上的數據庫。 不起作用的是單擊列標題以對網格進行排序。 我知道您使用ToBindingList()進行排序,但是隨后我松開了添加和刪除功能。 是否可以添加排序功能?
如果要在本地存儲數據,請使用數據集。 將數據集拖放到winform,然后選擇untyped。 如果您已經在gridview中包含數據,則可以通過以下方式輕松添加該數據:
DataSet DataSet1 = new dataset
dataset1.Tables.Add("Main");
foreach(DataGridViewColumn col in GridView.Columns)
{
string name = col.Name.ToString();
DataSet1.Tables["Main"].Columns.Add(name);
DataSet1.AcceptChanges();
}
foreach(DataGridViewRow row in GridView.Rows)
{
string value1 = row.Cells[0].Value.ToString();
string value2 = row.Cells[1].Value.ToString();
DataSet1.Tables["Main"].Rows.Add(value1,value2);
DataSet1.AcceptChanges();
}
要保存數據集,請使用以下代碼:
string path = "path to where to save";
DataSet1.WriteXml(path,XmlWriteOption.WriteScheme);
將數據集讀入程序
string path = "path of saved xml";
Dataset1.ReadXml(path);
//Binds with DataGrid
DataGridView1.DataSource = DataSet1;
DataGridView1.DataMember = "Main";
DataGridView1.Update();
為了手動添加新行,如果在屬性中使用可視工作室,則需要進行“行添加事件”,單擊照明螺栓,然后向下滾動至“行添加”並雙擊。
DataGridView1_RowsAdded_Event(object sender, EventArgs e)
{
DataSet1.AcceptChanges();
}
您將對“已刪除的行”執行相同的操作。 通過調用DataSet1.AcceptChanges()
您告訴程序保留當前更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.