簡體   English   中英

接受datagridview上的更改作為列表框更改中的選定項

[英]Accept changes on datagridview as selected item in Listbox change

我正在努力接受對datagridview的更改。

我有一個列表框和一個datagridview。 我的datagridview會根據從列表框中選擇的選定索引進行更改。 但是,每次我選擇一個不同的項目時,datagridview項目都會返回到原始視圖/列表。

我的問題:每當我從列表框中選擇一個項目時,如何接受/將更改寫回我的數據表或阻止datagridview刷新?

我的列表框更改事件的代碼是:

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataRow[] result = ds.Tables["AssessmentItems"].Select("GroupId = " + listBox1.SelectedIndex);

        var newTable = result.CopyToDataTable();
        BindingSource bindSource = new BindingSource();
        bindSource.DataSource = newTable;
        dataGridView1.DataSource = bindSource;
    }
dataGridView1.databind();

我認為需要在gridview中進行更新以進行新更改。

只需設置bindingsource Filter屬性。 但是您需要聲明bindingsource,因為您可以在類中的任何方法中使用它。

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    bindSource.Filter = "GroupId = " + listBox1.SelectedIndex;
}

因此,如果要導出源,請不要刷新它,對其進行過濾。 BindingSource有一個.Filter 方法可以為您提供幫助。 將您的活動更改為此。 完成了!

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    BindingSource bindSource = new BindingSource();
    bindSource.DataSource = yourOrginalSource;
    dataGridView1.DataSource = bindSource;
    //set your bind source filter
    string myFilter = "GroupId = " + listBox1.SelectedIndex;
    source.Filter = myFilter;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM