[C#] 纯文本查看 复制代码 namespace WinFormTutorial
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dataGridView1.Columns.Add("ProductNo", "产品编号");
dataGridView1.Columns.Add("ProductName", "产品名称");
dataGridView1.Columns.Add("ProductSpecification", "产品规格");
dataGridView1.Columns.Add("StockQuantity", "现存数量");
dataGridView1.CellClick += DataGridView1_CellClick;
}
private DataGridViewRow editingRow;
private Button editingButton;
private void DataGridView1_CellClick(object? sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
if (cell.Tag == null)
{
Rectangle rect = dataGridView1.GetCellDisplayRectangle(
e.ColumnIndex,
e.RowIndex,
false
);
var btn = new Button();
btn.Size = new Size(rect.Height, rect.Height);
btn.Text = "?";
btn.Location = new Point(rect.X + rect.Width - rect.Height, rect.Y);
btn.Click += Btn_Click;
cell.Tag = btn;
dataGridView1.Controls.Add(btn);
editingButton = btn;
editingButton.Show();
}
else
{
Button btn = cell.Tag as Button;
if (btn != null)
{
editingButton = btn;
editingButton.Show();
}
}
}
editingRow = dataGridView1.Rows[e.RowIndex];
}
private void Btn_Click(object? sender, EventArgs e)
{
FrmStockProducts frmStockProducts = new FrmStockProducts();
frmStockProducts.OnRowDoubleClicked += FrmStockProducts_OnRowDoubleClicked;
frmStockProducts.FormClosed += FrmStockProducts_FormClosed;
frmStockProducts.ShowDialog();
}
private void FrmStockProducts_FormClosed(object? sender, FormClosedEventArgs e)
{
if (editingButton != null)
{
editingButton.Hide();
}
}
private void FrmStockProducts_OnRowDoubleClicked(Product product)
{
if (editingRow != null)
{
var row = editingRow;
row.Cells[0].Value = product.ProductNo;
row.Cells[1].Value = product.ProductName;
row.Cells[2].Value = product.ProductSpecification;
row.Cells[3].Value = product.StockQuantity;
}
}
}
}
namespace WinFormTutorial
{
public partial class FrmStockProducts : Form
{
public event Action<Product> OnRowDoubleClicked;
private List<Product> products = new List<Product>()
{
new Product
{
ProductNo = "A001",
ProductName = "数据线",
ProductSpecification = "100*20",
StockQuantity = 945
},
new Product
{
ProductNo = "A002",
ProductName = "数据线",
ProductSpecification = "100*30",
StockQuantity = 101
},
};
public FrmStockProducts()
{
InitializeComponent();
dataGridView1.DataSource = products;
dataGridView1.Columns["ProductNo"].HeaderText = "产品编号";
dataGridView1.Columns["ProductName"].HeaderText = "产品名称";
dataGridView1.Columns["ProductSpecification"].HeaderText = "产品规格";
dataGridView1.Columns["StockQuantity"].HeaderText = "现存数量";
dataGridView1.Columns["Remark"].HeaderText = "备注";
dataGridView1.CellDoubleClick += DataGridView1_CellDoubleClick;
}
private void DataGridView1_CellDoubleClick(object? sender, DataGridViewCellEventArgs e)
{
var product = products[e.RowIndex];
if (OnRowDoubleClicked != null)
{
OnRowDoubleClicked(product);
}
}
}
public class Product
{
public string ProductNo { get; set; } = string.Empty;
public string ProductName { get; set; } = string.Empty;
public string ProductSpecification { get; set; } = string.Empty;
public int StockQuantity { get; set; }
public string Remark { get; set; } = string.Empty;
}
}
|