2013-10-31 41 views
2

我有一個form與主窗口分開彈出。它有一個DataGridView它顯示用戶訪問過的所有網址。所有的網址都保存在一個字符串列表中,然後轉換爲DataTable,然後我將這些值放入DataGridView。我可以通過網址看到桌子。無法從DataGridView獲取到另一個表單的值

我想在單元上啓用雙擊,這樣如果用戶雙擊它,他將被轉換回該網址。

當選擇一行(點擊url)並將其解析爲字符串時,出現錯誤。好像是空的,因爲頁面沒有加載

private DataTable ConvertListToDataTable(List<string> l) 
    { 
     table.Columns.Add("urls"); 
     int i = 0; 
     foreach(string s in l) 
     { 
      table.Rows.Add(); 
      table.Rows[i].SetField("urls", s); 
      i++; 
     } 
     return table; 
    } 
public void pageload(List<string> list) 
    { 
     table = ConvertListToDataTable(list); 
     dataGridView1.DataSource = table; 
    } 

    private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e) 
    { 
     Form1 form = new Form1(); 
     form.pageInput.Text = dataGridView1.SelectedRows.ToString(); 
     string input = form.pageInput.Text; 
     form.loadPage(input); 
    } 

我試圖改變DataGridViewSelectionModeFullRowSelectCellSelectRowHeaderSelect,但它不會改變任何東西。試圖改變雙擊單元格和行。爲了進行調試,我還嘗試在表單上放置textBox。所以,當我分配:

textBox1.Text = dataGridView1.SelectedRows.ToString() 

我得到的是Windows.Forms.DataGridViewSelectedCellCollection,所以它肯定是空的。

問題是,如果ToString()不起作用,我怎麼才能得到那個url值?謝謝您的幫助。

+0

是不是'CellContentDoubleClick'會在'edit mode'中設置'雙擊單元格'? –

+0

你得到的錯誤是什麼? – Brian

+0

@King不清楚你在說什麼,但是目前編輯模式是* EditOnKeyStrokeOrF2 *。我應該將其更改爲EditProgrammatically? – Messerschmitt

回答

0

如果它是一個集合,則必須另外處理集合中的每個元素。否則.ToString()只會將元數據中的selectedRows的文本返回給DataGridViewSelectedCellCollection。

嘗試類似,

foreach(elementType i in dataGridView1.SelectedRows) 
{ 
    //do something you want here such as TextBox.Text = TextBox.Text + i.ToString(); 
} 
+0

謝謝。我試圖實現這一點,但現在我得到* DataGridViewRow {Index = 0} *顯示在文本框中,並且在加載頁面時仍然出現錯誤。那麼,因爲它不是一個有效的網址。 – Messerschmitt

+0

dataGridView是否包含網址?聽起來好像沒有包含任何網址的單行 –

+0

是的,dataGridView只是一個表格,顯示訪問過的網址。那麼,至少這些網址顯示在表格中,但是當我在任意一行雙擊時,它似乎沒有任何內容。 – Messerschmitt

0

我想你想要什麼/需要的是更多的東西是這樣的:

private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e) 
{  
    string URL = ""; 
    if (dataGridView1[e.ColumnIndex, e.RowIndex].Value != null) 
    { 
     //get the URL from the cell that you double-clicked on 
     URL = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString(); 
    } 
    else 
     return; 

    //put your code to launch the URL in a browser here   
} 

另一種選擇是使用MouseDoubleClick事件是這樣的:

private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e) 
{ 
    //this will allow us to find out where we clicked in the datagridview 
    DataGridView.HitTestInfo hti = dataGridView1.HitTest(e.X, e.Y); 

    if (e.Button == MouseButtons.Left && hti.Type == DataGridViewHitTestType.Cell) 
    { 
     string URL = ""; 
     if (dataGridView1[hti.ColumnIndex, hti.RowIndex].Value != null) 
     { 
      //get the URL from the cell that you double-clicked on 
      URL = dataGridView1[hti.ColumnIndex, hti.RowIndex].Value.ToString(); 
     } 
     else 

      return; 
     //put your code to launch the URL in a browser here 
    } 
} 
0

首先,大概你想要的事件是CellDoubleClick()而不是CellContentDoubleClick()

那麼做到這一點:

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) 
{ 
    //Makes sure that a valid cell was double clicked. 
    if (e.ColumnIndex > -1 && e.RowIndex > -1) 
    { 
     Form1 form = new Form1(); 
     form.pageInput.Text = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString(); 
     string input = form.pageInput.Text; 
     form.loadPage(input); 
    } 
} 

讓我知道這是否正常工作!

+0

不幸的是,它沒有任何區別 – Messerschmitt

相關問題