你所要求的並不是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);
}
}
}
自己做一個快速的谷歌搜索如何使用ItemTemplate更新DataGridView你想有一個編輯按鈕作爲網格的一部分..有大量的工作示例如何做到這一點..請展示更多的努力你的部分......你說'最初我的datagridview看起來像''這意味着你還沒有做任何事情..?你可以告訴我們你迄今爲止嘗試過的嗎? – MethodMan
我正在C#winform模板中使用DataGridView。我想沒有像概念一樣的ItemTemplate。 ItemTemplate在asp.net webform的gridview中。 – Mou