winforms 设置DataGridView单元格按钮的按钮文本

z6psavjg  于 2023-01-02  发布在  其他
关注(0)|答案(2)|浏览(396)

我尝试插入和更改按钮的文本,但它显示为

下面是我的代码

Button SaveButton  = new Button();
Button DeleteButton  = new Button()
SaveButton.Text = "Save";
DeleteButton.Text = "Delete";
object[] Data = 
{
  ProductList[i].ProductName,
  ProductList[i].ProductPrice.ToString(),
  ProductList[i].ProductCreationCost.ToString(),
  ProductList[i].ProductStock.ToString(),
  ProductList[i].ProductBarcode.ToString(),
  ProductList[i].ProductImageLoc.ToString(),
  SaveButton,
  DeleteButton,
};
ItemDataGrid.Rows.Add(Data);

我只希望按钮名称为"保存"和"删除"。

qni6mghb

qni6mghb1#

您正在向数组中插入SaveButton和DeleteButton对象。只需插入按钮的text属性,如下所示。

object[] Data = 
    {
        ProductList[i].ProductName,
        ProductList[i].ProductPrice.ToString(),
        ProductList[i].ProductCreationCost.ToString(),
        ProductList[i].ProductStock.ToString(),
        ProductList[i].ProductBarcode.ToString(),
        ProductList[i].ProductImageLoc.ToString(),
        SaveButton.Text,
        DeleteButton.Text,
    };
t9eec4r0

t9eec4r02#

您的问题是关于设置DataGridView单元格按钮的按钮文本
DataGridViewButtonColumnUseColumnTextForButtonValueText属性可用于此目的。下面显示了为按钮设置“保存”和“Delete”标签的一些初始化代码示例。

  • 此示例使用DataGridViewDataSource属性附加一个记录列表,其中Record类表示一行数据。这通常会使事情变得更容易,因为您可以直接使用基础数据。*

数据源是一个BindingList<Product>,其中包含定义为表示行的类的对象:

// Minimal product class for example
class Product
{
    // Read-only cells for this example.
    public string Name { get; internal set; }
    public double Price { get; internal set; }
    public string Barcode { get; internal set; } =
        $"*{Guid.NewGuid().ToString().Substring(0, 8).ToUpper()}*";
}

然后在主窗体中,可以使用Load事件设置列和格式,包括按钮文本

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    initBarcodeFont();
    dataGridView.AllowUserToAddRows = false;
    dataGridView.DataSource = Products;
    dataGridView.RowTemplate.Height = 100;

    #region F O R M A T    C O L U M N S
    DataGridViewColumn col;
    Products.Add(new Product());

    // Format existing Name column
    dataGridView.Columns[nameof(Product.Name)].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

    // Format existing Barcode column
    col = dataGridView.Columns[nameof(Product.Barcode)];
    col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
    col.DefaultCellStyle.Font = _barcodeFont;

    // Format existing Price column
    col = dataGridView.Columns[nameof(Product.Price)];
    col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
    col.DefaultCellStyle.Format = "N2";

    // Add the Save button column and format the button style.
    col = new DataGridViewButtonColumn{
        Text = "Save",
        UseColumnTextForButtonValue = true,
        Width = 90,
        FlatStyle = FlatStyle.Flat
    };
    col.DefaultCellStyle.BackColor = Color.AliceBlue;
    dataGridView.Columns.Add(col);

    // Add the Delete button column and format the button style.
    col = new DataGridViewButtonColumn
    {
        Text = "Delete",
        UseColumnTextForButtonValue = true,
        Width = 90,
        FlatStyle = FlatStyle.Flat
    };
    col.DefaultCellStyle.BackColor = Color.AliceBlue;
    dataGridView.Columns.Add(col);
    Products.Clear();
    #endregion F O R M A T    C O L U M N S

    // Add a few products
    Products.Add(new Product { Name = "Coffee", Price = 13.99 });
    Products.Add(new Product { Name = "Milk", Price = 5.49 });
    Products.Add(new Product { Name = "Eggs", Price = 4.29 });
}
BindingList<Product> Products = new BindingList<Product>();

相关问题