簡體   English   中英

C#DATAGRIDVIEW選擇行並顯示選擇的行

[英]C# DATAGRIDVIEW select rows and display selected rows

我的datagrid視圖中填充了多行數據,我希望能夠在我的datagridview中選擇多行,然后更新datagridview以僅顯示選定的行,但出現錯誤

  public void btnUpdate_Click(object sender, EventArgs e)
    {


       List<DataGridViewRow> rowCollection = new List<DataGridViewRow>();
       foreach (DataGridViewRow row in dataGridView1.SelectedRows)
       {
           rowCollection.Add(dataGridView1.Rows[row.Index]);
       }

       //dataGridView1.Rows.Clear();




  data.Tables[0].Clear();


      foreach (DataGridViewRow row in rowCollection)
        {
           DataRow r = data.Tables[table].NewRow();
            dataGridView1.Rows.Add(row);
          //write the data in the DataRow and then add the datarow in your datatable
          data.Tables[table].Rows.Add(r);


      }
        CreateGraph(zedGraphControl1);





    }

當我選擇行並單擊更新按鈕時,出現錯誤“控件綁定數據時行無法以編程方式添加到DataGridView的行集合中”。

  • 行{DataGridViewRow {Index = -1}} System.Windows.Forms.DataGridViewRow

我曾嘗試查看異常幫助,但無法理解它。謝謝您的幫助

如果我沒記錯的話,不能將datagridview綁定到數據中以操縱行。

我相信幾個月前就解決了這個問題,但現在無法訪問我的項目。

我認為您可以通過從數據源中查找並刪除行,然后刷新datagridview來做到這一點

(個人建議:稍事休息10-15分鍾,然后以清晰的心態回到原處)

暫無
暫無

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

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