拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 如何将价格放在DataGridView列的右侧?(c#.netcore)小问题

如何将价格放在DataGridView列的右侧?(c#.netcore)小问题

白鹭 - 2022-02-28 1961 0 0

显然,如果您在像这样的列的右侧有产品的价格,它看起来会更好:

当然,这是一个 OrderID,但外观应该是一样的。

如何将价格放在 DataGridView 列的右侧?(c#.net core) 小问题

但目前我的看起来更像这样。

如何将价格放在 DataGridView 列的右侧?(c#.net core) 小问题

感谢您的帮助;哟!

uj5u.com热心网友回复:

通过单击 V 形图示在设计器中为 DataGridView 设定每一列

如何将价格放在 DataGridView 列的右侧?(c#.net core) 小问题

选择edit columns并选择要设定对齐方式的列,然后选择 DefaultCellStyle 并将对齐方式设定为 MiddleRight。

使用模拟资料的示例(与数据库表的作业方式相同)。所有数字列在下方都有 MiddleRight 对齐方式。

private void OnShown(object sender, EventArgs e)
{
    
    dataGridView1.AutoGenerateColumns = false;

    DataTable table = new DataTable();
    table.Columns.Add("Item", typeof(string));
    table.Columns.Add("Qty", typeof(int));
    table.Columns.Add("Price", typeof(decimal));
    table.Columns.Add("Total", typeof(decimal));

    table.Columns["Total"].Expression = "Qty * Price";

    table.Rows.Add("First item", 2, 60);
    table.Rows.Add("Second item", 2, 50);
    table.Rows.Add("Third item", 1, 20);

    dataGridView1.DataSource = table;

}

如何将价格放在 DataGridView 列的右侧?(c#.net core) 小问题

手动设定对齐。

var dataGridViewCellStyle1 = new DataGridViewCellStyle { Alignment = DataGridViewContentAlignment.MiddleRight };
PriceColumn.DefaultCellStyle = dataGridViewCellStyle1;
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *