2016-02-19 156 views
-3

最初我datagridview看起來像下面C#Winform的:內嵌的datagridview編輯

ID  Name  City  Action 
------ ------  ----  ------ 
1  Mitch  Kolkata  Edit 
2  Simon  Delhi  Edit 
3  Poly  Madras  Edit 

的所有數據將在只讀格式。所以用戶不能改變,但是當用戶點擊編輯按鈕時,文本框將被放置在名稱列中,並且城市dropdown將被放置在行中的城市列上,其編輯按鈕將被用戶點擊。

當用戶點擊編輯按鈕,然後編輯按鈕文本將被改爲保存,當用戶點擊保存按鈕,然後保存按鈕文本將更改爲編輯。所以請指導我在使用datagridview時如何實現在線編輯功能。謝謝

+0

自己做一個快速的谷歌搜索如何使用ItemTemplate更新DataGridView你想有一個編輯按鈕作爲網格的一部分..有大量的工作示例如何做到這一點..請展示更多的努力你的部分......你說'最初我的datagridview看起來像''這意味着你還沒有做任何事情..?你可以告訴我們你迄今爲止嘗試過的嗎? – MethodMan

+0

我正在C#winform模板中使用DataGridView。我想沒有像概念一樣的ItemTemplate。 ItemTemplate在asp.net webform的gridview中。 – Mou

回答

0

您可以將網格上的EditMode設置爲EditOnEnter,以便單擊時單元格處於編輯模式。

您仍然需要在DataGridView的外部處理您的保存功能。

+0

我不想將所有行都放入編輯模式,而是想編輯用戶單擊編輯按鈕的行。那麼如何以編程方式在編輯模式下設置特定行?可能嗎。你不介意給一些示例代碼。 – Mou

+0

然後編寫從按鈕單擊中獲取當前選定行的代碼...向我們展示您自己編寫或嘗試過的代碼 – MethodMan

+0

@Mou請向我展示您目前爲止的內容,並且我可以幫助您重構它你需要什麼。 – brana

2

你所要求的並不是DataGridView自然支持的,所以你需要通過維護一些狀態來實現整個邏輯,連接幾個事件並設置幾個屬性。

重要的部分是設置適當的列/單元格屬性。

(A)初始設置

var colName = new DataGridViewTextBoxColumn { HeaderText = "Name" }; 
var colCity = new DataGridViewComboBoxColumn { HeaderText = "City" }; 
var colAction = new DataGridViewButtonColumn { HeaderText = "Action" }; 
colName.ReadOnly = true; 
colCity.ReadOnly = true; 
colCity.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing; 
colAction.Text = "Edit"; 

(B)進入編輯模式

var cellName = (DataGridViewTextBoxCell)row.Cells[colName.Index]; 
var cellCity = (DataGridViewComboBoxCell)row.Cells[colCity.Index]; 
var cellAction = (DataGridViewButtonCell)row.Cells[colAction.Index]; 
cellName.ReadOnly = false; 
cellCity.ReadOnly = false; 
cellCity.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox; 
dg.CurrentCell = cellName; 
dg.BeginEdit(true); 
cellAction.Value = "Save"; 

(C)退出編輯模式

var cellName = (DataGridViewTextBoxCell)row.Cells[colName.Index]; 
var cellCity = (DataGridViewComboBoxCell)row.Cells[colCity.Index]; 
var cellAction = (DataGridViewButtonCell)row.Cells[colAction.Index]; 
cellName.ReadOnly = true; 
cellCity.ReadOnly = true; 
cellCity.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing; 
cellAction.Value = "Edit"; 

這裏是一個完全工作演示:

using System; 
using System.Linq; 
using System.Windows.Forms; 

namespace Demos 
{ 
    static class Program 
    { 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      var form = new Form(); 
      var dg = new DataGridView { Dock = DockStyle.Fill, Parent = form }; 
      dg.AllowUserToAddRows = dg.AllowUserToDeleteRows = false; 
      var colId = new DataGridViewTextBoxColumn { HeaderText = "Id", ReadOnly = true }; 
      var colName = new DataGridViewTextBoxColumn { HeaderText = "Name" }; 
      var colCity = new DataGridViewComboBoxColumn { HeaderText = "City" }; 
      var colAction = new DataGridViewButtonColumn { HeaderText = "Action" }; 
      colName.ReadOnly = true; 
      colCity.ReadOnly = true; 
      colCity.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing; 
      colAction.Text = "Edit"; 
      dg.Columns.AddRange(colId, colName, colCity, colAction); 
      var data = new[] 
      { 
       new { Id = 1, Name = "Mitch", City = "Kolkata" }, 
       new { Id = 2, Name = "Simon", City = "Delhi" }, 
       new { Id = 3, Name = "Poly", City = "Madras" }, 
      }; 
      colCity.Items.AddRange(data.Select(item => item.City).Distinct().ToArray()); 
      foreach (var item in data) 
       dg.Rows.Add(item.Id, item.Name, item.City, "Edit"); 
      Action<DataGridViewRow> enterEditMode = row => 
      { 
       var cellName = (DataGridViewTextBoxCell)row.Cells[colName.Index]; 
       var cellCity = (DataGridViewComboBoxCell)row.Cells[colCity.Index]; 
       var cellAction = (DataGridViewButtonCell)row.Cells[colAction.Index]; 
       cellName.ReadOnly = false; 
       cellCity.ReadOnly = false; 
       cellCity.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox; 
       dg.CurrentCell = cellName; 
       dg.BeginEdit(true); 
       cellAction.Value = "Save"; 
      }; 
      Action<DataGridViewRow> exitEditMode = row => 
      { 
       var cellName = (DataGridViewTextBoxCell)row.Cells[colName.Index]; 
       var cellCity = (DataGridViewComboBoxCell)row.Cells[colCity.Index]; 
       var cellAction = (DataGridViewButtonCell)row.Cells[colAction.Index]; 
       cellName.ReadOnly = true; 
       cellCity.ReadOnly = true; 
       cellCity.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing; 
       cellAction.Value = "Edit"; 
      }; 
      dg.CellContentClick += (sender, e) => 
      { 
       if (e.ColumnIndex == colAction.Index) 
       { 
        var row = dg.Rows[e.RowIndex]; 
        var cellAction = (DataGridViewButtonCell)row.Cells[colAction.Index]; 
        if ((string)cellAction.Value == "Edit") 
         enterEditMode(row); 
        else if (dg.EndEdit()) 
        { 
         // Save code goes here ... 
         exitEditMode(row); 
        } 
       } 
      }; 
      dg.RowValidated += (sender, e) => 
      { 
       var row = dg.Rows[e.RowIndex]; 
       var cellAction = (DataGridViewButtonCell)row.Cells[colAction.Index]; 
       if ((string)cellAction.Value == "Save") 
        exitEditMode(row); 
      }; 
      Application.Run(form); 
     } 
    } 
}