winforms DataGridView列中的两种不同格式

tyky79it  于 2023-05-29  发布在  其他
关注(0)|答案(1)|浏览(117)

我在DataGridView中有一个名为Discount Value,它有两种不同的格式,分别是CurrencyPercentage
是否可以在DataGridView的一个上做不同的格式?谢谢。
我尝试使用下面的代码,但输出不是预期的。RegulerDiscount_DataGridView_CellFormatting出现问题

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            List<RegulerDiscountModel> regulerDiscountModelList = new List<RegulerDiscountModel>();

            RegulerDiscountModel regulerDiscountModel1 = new RegulerDiscountModel();
            regulerDiscountModel1.RegulerDiscountID = 1;
            regulerDiscountModel1.RegulerDiscountProductId = 1;
            regulerDiscountModel1.RegulerDiscountProductName = "Product 1";
            regulerDiscountModel1.RegulerDiscountName = "Disc1";
            regulerDiscountModel1.RegulerDiscountValue = 1000;
            regulerDiscountModel1.RegulerDiscountStrTypeId = "Rp";
            regulerDiscountModelList.Add(regulerDiscountModel1);

            RegulerDiscountModel regulerDiscountModel2 = new RegulerDiscountModel();
            regulerDiscountModel2.RegulerDiscountID = 1;
            regulerDiscountModel2.RegulerDiscountProductId = 1;
            regulerDiscountModel2.RegulerDiscountProductName = "Product 1";
            regulerDiscountModel2.RegulerDiscountName = "Disc2";
            regulerDiscountModel2.RegulerDiscountValue = 2000;
            regulerDiscountModel2.RegulerDiscountStrTypeId = "Rp";
            regulerDiscountModelList.Add(regulerDiscountModel2);

            RegulerDiscountModel regulerDiscountModel3 = new RegulerDiscountModel();
            regulerDiscountModel3.RegulerDiscountID = 1;
            regulerDiscountModel3.RegulerDiscountProductId = 1;
            regulerDiscountModel3.RegulerDiscountProductName = "Product 1";
            regulerDiscountModel3.RegulerDiscountName = "Disc3";
            regulerDiscountModel3.RegulerDiscountValue = 10.5;
            regulerDiscountModel3.RegulerDiscountStrTypeId = "%";
            regulerDiscountModelList.Add(regulerDiscountModel3);

            RegulerDiscountModel regulerDiscountModel4 = new RegulerDiscountModel();
            regulerDiscountModel4.RegulerDiscountID = 1;
            regulerDiscountModel4.RegulerDiscountProductId = 1;
            regulerDiscountModel4.RegulerDiscountProductName = "Product 1";
            regulerDiscountModel4.RegulerDiscountName = "Disc4";
            regulerDiscountModel4.RegulerDiscountValue = 20.1;
            regulerDiscountModel4.RegulerDiscountStrTypeId = "%";
            regulerDiscountModelList.Add(regulerDiscountModel4);

            CtechGroupedDataGridView.DataSource = regulerDiscountModelList;
            CtechGroupedDataGridView.CellFormatting += CtechGroupedDataGridView_CellFormatting;
        }

        private void CtechGroupedDataGridView_CellFormatting(object? sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {
                if (e.RowIndex >= 0 && e.RowIndex <= CtechGroupedDataGridView.Rows.Count - 1)
                {
                    DataGridViewRow row = CtechGroupedDataGridView.CurrentRow;
                    if (row != null)
                    {
                        // ID ProductId ProductName DiscountName DiscountValue DiscountType
                        // 0  1         2           3            4             5

                        if (row.Cells[4].Value != null && row.Cells[5].Value != null)
                        {
                            String? strDiscountValue = row.Cells[4].Value.ToString();
                            String? strDiscountType = row.Cells[5].Value.ToString();
                            if (!String.IsNullOrEmpty(strDiscountValue) && !String.IsNullOrEmpty(strDiscountType))
                            {
                                switch (strDiscountType)
                                {
                                    case "Rp":
                                        CtechGroupedDataGridView[4, e.RowIndex].Style.Format = "N0";
                                        break;

                                    case "%":
                                        CtechGroupedDataGridView[4, e.RowIndex].Style.Format = "P1";
                                        break;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

电流输出:

当我点击DataGridView时,我得到如下输出:

预期输出:

Grouped DataGridView(它的作品)

namespace WinFormsApp1
{
    public partial class CtechGroupedDataGridView : DataGridView
    {
        public CtechGroupedDataGridView()
        {
            InitializeComponent();

            DoubleBuffered = true;
            GridColor = Color.LightGray;
            BackgroundColor = SystemColors.Control;
            BorderStyle = BorderStyle.None;
            SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            ColumnGroupEnabled = new int[] { 1, 2};

            ClearSelection();
        }

        protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
        {
            base.OnDataBindingComplete(e);

            // Fit columns
            int columnsWidth = 0;
            foreach (DataGridViewColumn column in this.Columns)
            {
                if (column.Visible)
                {
                    columnsWidth += column.Width;
                }
            }

            if (columnsWidth < this.Width)
            {
                for (int i = 0; i < this.Columns.Count; i++)
                {
                    this.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                }
                this.Columns[this.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
            else
            {
                for (int i = 0; i < this.Columns.Count; i++)
                {
                    this.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                }
            }

            for (int i = 0; i < this.Columns.Count; i++)
            {
                int column = this.Columns[i].Width;
                this.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                this.Columns[i].Width = column;
                this.Columns[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            }

            ClearSelection();
        }

        protected override void OnCellFormatting(DataGridViewCellFormattingEventArgs args)
        {
            // Call home to base
            base.OnCellFormatting(args);

            // First row always displays
            if (args.RowIndex == 0)
            {
                return;
            }

            if (IsRepeatedCellValue(args.RowIndex, args.ColumnIndex))
            {
                args.Value = string.Empty;
                args.FormattingApplied = true;
            }
        }

        private bool IsRepeatedCellValue(int rowIndex, int colIndex)
        {
            if (ColumnGroupEnabled.Contains(colIndex))
            {
                DataGridViewCell currCell = Rows[rowIndex].Cells[colIndex];
                DataGridViewCell prevCell = Rows[rowIndex - 1].Cells[colIndex];

                if (currCell.Value != null && prevCell.Value != null)
                {
                    if ((currCell.Value == prevCell.Value) || (currCell.Value.ToString() == prevCell.Value.ToString()))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public int[] ColumnGroupEnabled { get; set; }

        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs args)
        {
            base.OnCellPainting(args);

            // Keep first and last row to visible
            if (args.RowIndex >= 0 && args.RowIndex < RowCount - 1)
            {
                args.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
            }

            // Ignore column and row headers and first row
            if (args.RowIndex < 1 || args.ColumnIndex < 0)
            {
                return;
            }

            if (IsRepeatedCellValue(args.RowIndex, args.ColumnIndex))
            {
                args.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
            }
            else
            {
                args.AdvancedBorderStyle.Top = AdvancedCellBorderStyle.Top;
            }
        }
    }
}

Reguler DiscountModel(它的作品)

public class RegulerDiscountModel
{
    public RegulerDiscountModel()
    {
    }

    [DisplayName("ID")]
    public int RegulerDiscountID
    {
        get; set;
    }

    [DisplayName("Product Id")]
    public int RegulerDiscountProductId
    {
        get; set;
    }

    [DisplayName("Product Name")]
    public String? RegulerDiscountProductName
    {
        get; set;
    }

    [DisplayName("Discount Name")]
    public String? RegulerDiscountName
    {
        get; set;
    }

    [DisplayName("Discount Value")]
    public double RegulerDiscountValue
    {
        get; set;
    }

    [DisplayName("Discount Type")]
    public String? RegulerDiscountStrTypeId
    {
        get; set;
    }
}
t8e9dugd

t8e9dugd1#

private void CtechGroupedDataGridView_CellFormatting(object? sender, DataGridViewCellFormattingEventArgs e)
{
    try
    {
        if (e.RowIndex >= 0 && e.RowIndex <= CtechGroupedDataGridView.Rows.Count - 1)
        {
            // Bug in here
            //DataGridViewRow row = CtechGroupedDataGridView.CurrentRow;
            DataGridViewRow row = CtechGroupedDataGridView[e.ColumnIndex, e.RowIndex].OwningRow;

            if (row != null)
            {
                // ID ProductId ProductName DiscountName DiscountValue DiscountType
                // 0  1         2           3            4             5

                if (row.Cells[4].Value != null && row.Cells[5].Value != null)
                {
                    String? strDiscountValue = row.Cells[4].Value.ToString();
                    String? strDiscountType = row.Cells[5].Value.ToString();
                    if (!String.IsNullOrEmpty(strDiscountValue) && !String.IsNullOrEmpty(strDiscountType))
                    {
                        switch (strDiscountType)
                        {
                            case "Rp":
                                CtechGroupedDataGridView[4, e.RowIndex].Style.Format = "N0";
                                break;

                            case "%":
                                // Bug in here
                                //CtechGroupedDataGridView[4, e.RowIndex].Style.Format = "P1";
                                CtechGroupedDataGridView[4, e.RowIndex].Style.Format = "F1";
                                break;
                        }
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

输出量

相关问题