如何在c#

时间:2018-04-23 04:01:22

标签: c#

我有这个未绑定的datagridview,它在表单加载期间添加列;

这是代码;

private void loadfields()
        {
            dgvbulkentries.ColumnCount = 15;
            dgvbulkentries.Columns[0].Name = "ID No";
            dgvbulkentries.Columns[0].Width = 80;
            dgvbulkentries.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvbulkentries.Columns[1].Name = "Surname";
            dgvbulkentries.Columns[1].Width = 150;
            dgvbulkentries.Columns[2].Name = "First Name";
            dgvbulkentries.Columns[2].Width = 150;
            dgvbulkentries.Columns[3].Name = "Name Extn";
            dgvbulkentries.Columns[3].Width = 40;
            dgvbulkentries.Columns[4].Name = "Middle Name";
            dgvbulkentries.Columns[4].Width = 150;
            dgvbulkentries.Columns[5].Name = "Course";
            dgvbulkentries.Columns[5].Width = 110;
            dgvbulkentries.Columns[6].Name = "Year";
            dgvbulkentries.Columns[6].Width = 40;
            dgvbulkentries.Columns[6].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvbulkentries.Columns[7].Name = "Street/Block";
            dgvbulkentries.Columns[7].Width = 150;
            dgvbulkentries.Columns[8].Name = "Subdivision";
            dgvbulkentries.Columns[8].Width = 150;
            dgvbulkentries.Columns[9].Name = "Barangay";
            dgvbulkentries.Columns[9].Width = 150;
            dgvbulkentries.Columns[10].Name = "Municipality/City";
            dgvbulkentries.Columns[10].Width = 150;
            dgvbulkentries.Columns[11].Name = "Province";
            dgvbulkentries.Columns[11].Width = 150;
            dgvbulkentries.Columns[12].Name = "GWA";
            dgvbulkentries.Columns[12].Width = 60;
            dgvbulkentries.Columns[12].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dgvbulkentries.Columns[13].Name = "Units";
            dgvbulkentries.Columns[13].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvbulkentries.Columns[13].Width = 50;
            dgvbulkentries.Columns[14].Name = "Total School Fees";
            dgvbulkentries.Columns[14].Width = 100;
            dgvbulkentries.Columns[14].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            DataGridViewComboBoxColumn sexes = new DataGridViewComboBoxColumn();
            sexes.HeaderText = "Sex";
            sexes.Name = "Sex";
            sexes.MaxDropDownItems = 4;
            sexes.Items.Add("Male");
            sexes.Items.Add("Female");
            dgvbulkentries.Columns.Add(sexes);
            dgvbulkentries.Columns[15].DisplayIndex = 5;
            dgvbulkentries.Columns[15].Width = 80;
            DataGridViewComboBoxColumn ssfapremarks = new DataGridViewComboBoxColumn();
            ssfapremarks.HeaderText = "Remarks";
            ssfapremarks.Name = "Remarks";
            ssfapremarks.MaxDropDownItems = 4;
            ssfapremarks.Items.Add("Enrolled");
            ssfapremarks.Items.Add("Not Enrolled");
            dgvbulkentries.Columns.Add(ssfapremarks);
            dgvbulkentries.Columns[16].Width = 120;
        }

我已经为datagridview中的特定列设置了货币文本框的代码。但问题是,当我在单元格中输入数字时,它根本不会改变。

这是我包含的代码;

private void dgvbulkentries_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        dgvbulkentries.Columns[14].DefaultCellStyle.Format = "n";
    }

例如,当我在datagridview中的特定列输入数字时,它不会将值格式化为货币格式。我已经做了你的建议,但它不起作用

我错过了什么?

1 个答案:

答案 0 :(得分:0)

正如this回答中提到的那样 在知道了这个问题后,我认为使用CellLeave事件的数据网格视图将解决你的问题

void dataGridView1_CellLeave(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
   dgvbulkentries.Columns[14].DefaultCellStyle.Format = "c2";
   dgvbulkentries.Columns[14].DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-GB");
   dgvbulkentries.Columns[14].ValueType = typeof(decimal);
}