0

在我的VS2015 Winform應用程序中,有一個DataGridView控件綁定到綁定到SQL數據庫的BindingSource。網格有四列:ID,URL,名稱,類型。 URL列是DataGridViewLinkColumn,其ReadOnly屬性默認設置爲False。我可以編輯名稱和類型列,但URL列顯示爲ReadOnly。爲什麼?我如何讓URL列可編輯?Winform DataGridViewLinkColumn ReadOnly屬性不起作用

+0

'DataGridViewLinkColumn'不可編輯。它僅將內容顯示爲鏈接。 –

+0

數據實際上是在數據庫中更改(readwrite)還是僅在表單上(可能仍是隻讀的,只是「戲弄」你)? –

回答

0

由於雷扎說:

DataGridViewLinkColumn不可編輯。

因此,要在這樣的列中編輯單元格,必須根據需要將其轉換爲DataGridViewTextBoxCell。舉例來說,如果我已經訂閱了DataGridView.CellContentClick處理點擊一個鏈接,那麼我會處理CellDoubleClick的電池轉換:

private void DataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) 
{ 
    if (this.dataGridView1.Columns[e.ColumnIndex] == this.dataGridView1.Columns["URL"]) 
    { 
     this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] = new DataGridViewTextBoxCell(); 
     this.dataGridView1.BeginEdit(true); 
    } 
} 

一旦你進入你的價值,並離開該小區,則應使用CellValidated驗證新值是一個URI的單元轉換回一個DataGridViewLinkCell之前:

private void DataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e) 
{ 
    if (this.dataGridView1.Columns[e.ColumnIndex] == this.dataGridView1.Columns["URL"]) 
    { 
     DataGridViewCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; 

     if (Uri.IsWellFormedUriString(cell.EditedFormattedValue.ToString(), UriKind.Absolute)) 
     { 
      cell = new DataGridViewLinkCell(); 
     } 
    } 
} 

買者

  • 這只是工作的我,當爲「URL」列中的數據是字符串,因此結合後,列默認爲一個DataGridViewTextBoxColumn - 迫使手動轉換鏈接細胞開始:

    private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) 
    { 
        foreach (DataGridViewRow r in dataGridView1.Rows) 
        { 
         if (Uri.IsWellFormedUriString(r.Cells["URL"].Value.ToString(), UriKind.Absolute)) 
         { 
          r.Cells["URL"] = new DataGridViewLinkCell(); 
         } 
        } 
    } 
    
  • 從一開始就將「URI」列設置爲DataGridViewLinkColumn,允許將單元格成功轉換爲TextBox類型。但是,當轉換回鏈接單元時,調試顯示轉換髮生,但單元格格式和行爲失敗。