2015-05-01 74 views
0

我試圖從datagridview中用鼠標拖動從文本文件中獲取信息來選擇一部分數據,然後使用此選定數據更新datagridview。從數據網格視圖中選擇一部分數據c#

UPDATE:

的是無法清除網格視圖初始誤差得到解決,現在當所選擇的數據正試圖重新進入這行文字出現在DataGrid應將數據網格是: 「的DataGridViewRow {指數= -1}」

private 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]); 
     } 

      dataset.Tables[0].Clear(); 

     foreach (DataGridViewRow row in rowCollection) 
     { 
      // dataGridView1.Rows.Add(row); 
      dataset.Tables[tableName].Rows.Add(row); 

     } 
    } 

下面是在DataGridView顯示信息的初始代碼:

foreach (string row in rows) 
      { 
       //Adds time to the array 
       var items = new List<string> { timeS }; 
       items.AddRange(row.Split(delimeter.ToArray())); 

       speed = Convert.ToDouble(items[2]); 
       //converts 'speed' to a double and divides by 10 to display the correct value 
       speed = speed/10; 
       items[2] = speed.ToString(); 
       dataset.Tables[tableName].Rows.Add(items.ToArray()); 
       //Adds the interval time to Dtime for each row 
       Dtime = Dtime.AddSeconds(inter); 

      } 
      //selects where to display the data 
      this.dataGridView1.DataSource = dataset.Tables[0].DefaultView; 
     } 

任何幫助,將不勝感激!

乾杯。

回答

1

您無法清除DataGridView.Rows,因爲您已指定數據源。 您必須清除DataGridView的DataSource。

而不是

dataGridView1.Rows.Clear(); 

寫:

dataset.Tables[0].Clear(); 

編輯: 你在你的代碼錯誤...你不能在一個表 該代碼添加的DataGridViewRow修復您的問題:

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

    } 
+0

這已修復了未清除列表的錯誤,但不是我正在努力用網格視圖中的選定數據重新填充它。 – Ben

+0

'List'正在拋出錯誤'使用泛型類型'Systems.Collections.Generic。列表'需要1個類型參數,我試圖將其設置爲但它沒有工作 – Ben

相關問題